aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorPaul Eggert2011-04-10 09:44:27 -0700
committerPaul Eggert2011-04-10 09:44:27 -0700
commitb2ded58d7e0eb75f00071036d1c07bbd55313b60 (patch)
tree5546bb6d7214033a83d13ce87a15f65595f6ae74 /lib
parent37f1c9309eb0e6b3bc3dda1ffa7f99410c22355d (diff)
parent12020a9e6dcfc2213e8bbb0fec259c1ed1202f30 (diff)
downloademacs-b2ded58d7e0eb75f00071036d1c07bbd55313b60.tar.gz
emacs-b2ded58d7e0eb75f00071036d1c07bbd55313b60.zip
Fix more problems found by GCC 4.6.0's static checks.
Diffstat (limited to 'lib')
-rw-r--r--lib/allocator.c5
-rw-r--r--lib/allocator.h16
-rw-r--r--lib/careadlinkat.c40
-rw-r--r--lib/careadlinkat.h10
-rw-r--r--lib/gnulib.mk10
-rw-r--r--lib/stdlib.in.h14
6 files changed, 60 insertions, 35 deletions
diff --git a/lib/allocator.c b/lib/allocator.c
new file mode 100644
index 00000000000..2c1a3da03aa
--- /dev/null
+++ b/lib/allocator.c
@@ -0,0 +1,5 @@
1#define _GL_USE_STDLIB_ALLOC 1
2#include <config.h>
3#include "allocator.h"
4#include <stdlib.h>
5struct allocator const stdlib_allocator = { malloc, realloc, free, NULL };
diff --git a/lib/allocator.h b/lib/allocator.h
index 4ac863b224c..953117da83f 100644
--- a/lib/allocator.h
+++ b/lib/allocator.h
@@ -18,6 +18,7 @@
18/* Written by Paul Eggert. */ 18/* Written by Paul Eggert. */
19 19
20#ifndef _GL_ALLOCATOR_H 20#ifndef _GL_ALLOCATOR_H
21#define _GL_ALLOCATOR_H
21 22
22#include <stddef.h> 23#include <stddef.h>
23 24
@@ -30,16 +31,16 @@ struct allocator
30 attributes do not work with pointers to functions. See 31 attributes do not work with pointers to functions. See
31 <http://lists.gnu.org/archive/html/bug-gnulib/2011-04/msg00007.html>. */ 32 <http://lists.gnu.org/archive/html/bug-gnulib/2011-04/msg00007.html>. */
32 33
33 /* Call MALLOC to allocate memory, like 'malloc'. On failure MALLOC 34 /* Call ALLOCATE to allocate memory, like 'malloc'. On failure ALLOCATE
34 should return NULL, though not necessarily set errno. When given 35 should return NULL, though not necessarily set errno. When given
35 a zero size it may return NULL even if successful. */ 36 a zero size it may return NULL even if successful. */
36 void *(*malloc) (size_t); 37 void *(*allocate) (size_t);
37 38
38 /* If nonnull, call REALLOC to reallocate memory, like 'realloc'. 39 /* If nonnull, call REALLOCATE to reallocate memory, like 'realloc'.
39 On failure REALLOC should return NULL, though not necessarily set 40 On failure REALLOCATE should return NULL, though not necessarily set
40 errno. When given a zero size it may return NULL even if 41 errno. When given a zero size it may return NULL even if
41 successful. */ 42 successful. */
42 void *(*realloc) (void *, size_t); 43 void *(*reallocate) (void *, size_t);
43 44
44 /* Call FREE to free memory, like 'free'. */ 45 /* Call FREE to free memory, like 'free'. */
45 void (*free) (void *); 46 void (*free) (void *);
@@ -50,4 +51,7 @@ struct allocator
50 void (*die) (void); 51 void (*die) (void);
51}; 52};
52 53
53#endif 54/* An allocator using the stdlib functions and a null DIE function. */
55extern struct allocator const stdlib_allocator;
56
57#endif /* _GL_ALLOCATOR_H */
diff --git a/lib/careadlinkat.c b/lib/careadlinkat.c
index 15ffe24c0f4..e2909c766d5 100644
--- a/lib/careadlinkat.c
+++ b/lib/careadlinkat.c
@@ -22,19 +22,12 @@
22 22
23#include "careadlinkat.h" 23#include "careadlinkat.h"
24 24
25#include "allocator.h"
26
27#include <errno.h> 25#include <errno.h>
28#include <limits.h> 26#include <limits.h>
29#include <stdlib.h> 27#include <stdlib.h>
30#include <string.h> 28#include <string.h>
31#include <unistd.h> 29#include <unistd.h>
32 30
33/* Use the system functions, not the gnulib overrides, because this
34 module does not depend on GNU or POSIX semantics. */
35#undef malloc
36#undef realloc
37
38/* Define this independently so that stdint.h is not a prerequisite. */ 31/* Define this independently so that stdint.h is not a prerequisite. */
39#ifndef SIZE_MAX 32#ifndef SIZE_MAX
40# define SIZE_MAX ((size_t) -1) 33# define SIZE_MAX ((size_t) -1)
@@ -44,24 +37,24 @@
44# define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2)) 37# define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
45#endif 38#endif
46 39
40#include "allocator.h"
41
47#if ! HAVE_READLINKAT 42#if ! HAVE_READLINKAT
48/* Ignore FD. Get the symbolic link value of FILENAME and put it into 43/* Get the symbolic link value of FILENAME and put it into BUFFER, with
49 BUFFER, with size BUFFER_SIZE. This function acts like readlink 44 size BUFFER_SIZE. This function acts like readlink but has
50 but has readlinkat's signature. */ 45 readlinkat's signature. */
51ssize_t 46ssize_t
52careadlinkatcwd (int fd, char const *filename, char *buffer, 47careadlinkatcwd (int fd, char const *filename, char *buffer,
53 size_t buffer_size) 48 size_t buffer_size)
54{ 49{
55 (void) fd; 50 /* FD must be AT_FDCWD here, otherwise the caller is using this
51 function in contexts for which it was not meant for. */
52 if (fd != AT_FDCWD)
53 abort ();
56 return readlink (filename, buffer, buffer_size); 54 return readlink (filename, buffer, buffer_size);
57} 55}
58#endif 56#endif
59 57
60/* A standard allocator. For now, only careadlinkat needs this, but
61 perhaps it should be moved to the allocator module. */
62static struct allocator const standard_allocator =
63 { malloc, realloc, free, NULL };
64
65/* Assuming the current directory is FD, get the symbolic link value 58/* Assuming the current directory is FD, get the symbolic link value
66 of FILENAME as a null-terminated string and put it into a buffer. 59 of FILENAME as a null-terminated string and put it into a buffer.
67 If FD is AT_FDCWD, FILENAME is interpreted relative to the current 60 If FD is AT_FDCWD, FILENAME is interpreted relative to the current
@@ -76,7 +69,10 @@ static struct allocator const standard_allocator =
76 the returned value if it is nonnull and is not BUFFER. A null 69 the returned value if it is nonnull and is not BUFFER. A null
77 ALLOC stands for the standard allocator. 70 ALLOC stands for the standard allocator.
78 71
79 The PREADLINKAT function specifies how to read links. 72 The PREADLINKAT function specifies how to read links. It operates
73 like POSIX readlinkat()
74 <http://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html>
75 but can assume that its first argument is the same as FD.
80 76
81 If successful, return the buffer address; otherwise return NULL and 77 If successful, return the buffer address; otherwise return NULL and
82 set errno. */ 78 set errno. */
@@ -94,7 +90,7 @@ careadlinkat (int fd, char const *filename,
94 char stack_buf[1024]; 90 char stack_buf[1024];
95 91
96 if (! alloc) 92 if (! alloc)
97 alloc = &standard_allocator; 93 alloc = &stdlib_allocator;
98 94
99 if (! buffer_size) 95 if (! buffer_size)
100 { 96 {
@@ -138,16 +134,16 @@ careadlinkat (int fd, char const *filename,
138 134
139 if (buf == stack_buf) 135 if (buf == stack_buf)
140 { 136 {
141 char *b = (char *) alloc->malloc (link_size); 137 char *b = (char *) alloc->allocate (link_size);
142 if (! b) 138 if (! b)
143 break; 139 break;
144 memcpy (b, buf, link_size); 140 memcpy (b, buf, link_size);
145 buf = b; 141 buf = b;
146 } 142 }
147 else if (link_size < buf_size && buf != buffer && alloc->realloc) 143 else if (link_size < buf_size && buf != buffer && alloc->reallocate)
148 { 144 {
149 /* Shrink BUF before returning it. */ 145 /* Shrink BUF before returning it. */
150 char *b = (char *) alloc->realloc (buf, link_size); 146 char *b = (char *) alloc->reallocate (buf, link_size);
151 if (b) 147 if (b)
152 buf = b; 148 buf = b;
153 } 149 }
@@ -164,7 +160,7 @@ careadlinkat (int fd, char const *filename,
164 buf_size = buf_size_max; 160 buf_size = buf_size_max;
165 else 161 else
166 break; 162 break;
167 buf = (char *) alloc->malloc (buf_size); 163 buf = (char *) alloc->allocate (buf_size);
168 } 164 }
169 while (buf); 165 while (buf);
170 166
diff --git a/lib/careadlinkat.h b/lib/careadlinkat.h
index c5e4bcfc15f..4f0184bbc33 100644
--- a/lib/careadlinkat.h
+++ b/lib/careadlinkat.h
@@ -18,6 +18,7 @@
18/* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */ 18/* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */
19 19
20#ifndef _GL_CAREADLINKAT_H 20#ifndef _GL_CAREADLINKAT_H
21#define _GL_CAREADLINKAT_H
21 22
22#include <fcntl.h> 23#include <fcntl.h>
23#include <unistd.h> 24#include <unistd.h>
@@ -37,7 +38,10 @@ struct allocator;
37 buffer managed by ALLOC. It is the caller's responsibility to free 38 buffer managed by ALLOC. It is the caller's responsibility to free
38 the returned value if it is nonnull and is not BUFFER. 39 the returned value if it is nonnull and is not BUFFER.
39 40
40 The PREADLINKAT function specifies how to read links. 41 The PREADLINKAT function specifies how to read links. It operates
42 like POSIX readlinkat()
43 <http://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html>
44 but can assume that its first argument is the same as FD.
41 45
42 If successful, return the buffer address; otherwise return NULL and 46 If successful, return the buffer address; otherwise return NULL and
43 set errno. */ 47 set errno. */
@@ -49,8 +53,10 @@ char *careadlinkat (int fd, char const *filename,
49 char *, size_t)); 53 char *, size_t));
50 54
51/* Suitable values for careadlinkat's FD and PREADLINKAT arguments, 55/* Suitable values for careadlinkat's FD and PREADLINKAT arguments,
52 when doing a plain readlink. */ 56 when doing a plain readlink:
57 Pass FD = AT_FDCWD and PREADLINKAT = careadlinkatcwd. */
53#if HAVE_READLINKAT 58#if HAVE_READLINKAT
59/* AT_FDCWD is declared in <fcntl.h>, readlinkat in <unistd.h>. */
54# define careadlinkatcwd readlinkat 60# define careadlinkatcwd readlinkat
55#else 61#else
56/* Define AT_FDCWD independently, so that the careadlinkat module does 62/* Define AT_FDCWD independently, so that the careadlinkat module does
diff --git a/lib/gnulib.mk b/lib/gnulib.mk
index d2fd6698030..1938c6127a2 100644
--- a/lib/gnulib.mk
+++ b/lib/gnulib.mk
@@ -21,6 +21,14 @@ libgnu_a_LIBADD = $(gl_LIBOBJS)
21libgnu_a_DEPENDENCIES = $(gl_LIBOBJS) 21libgnu_a_DEPENDENCIES = $(gl_LIBOBJS)
22EXTRA_libgnu_a_SOURCES = 22EXTRA_libgnu_a_SOURCES =
23 23
24## begin gnulib module allocator
25
26libgnu_a_SOURCES += allocator.c
27
28EXTRA_DIST += allocator.h
29
30## end gnulib module allocator
31
24## begin gnulib module arg-nonnull 32## begin gnulib module arg-nonnull
25 33
26# The BUILT_SOURCES created by this Makefile snippet are not used via #include 34# The BUILT_SOURCES created by this Makefile snippet are not used via #include
@@ -73,7 +81,7 @@ EXTRA_DIST += $(top_srcdir)/./c++defs.h
73 81
74libgnu_a_SOURCES += careadlinkat.c 82libgnu_a_SOURCES += careadlinkat.c
75 83
76EXTRA_DIST += allocator.h careadlinkat.h 84EXTRA_DIST += careadlinkat.h
77 85
78## end gnulib module careadlinkat 86## end gnulib module careadlinkat
79 87
diff --git a/lib/stdlib.in.h b/lib/stdlib.in.h
index 2697a4bd1db..b9ada2cd1a8 100644
--- a/lib/stdlib.in.h
+++ b/lib/stdlib.in.h
@@ -255,9 +255,14 @@ _GL_WARN_ON_USE (ptsname, "grantpt is not portable - "
255# endif 255# endif
256#endif 256#endif
257 257
258/* If _GL_USE_STDLIB_ALLOC is nonzero, the including module does not
259 rely on GNU or POSIX semantics for malloc and realloc (for example,
260 by never specifying a zero size), so it does not need malloc or
261 realloc to be redefined. */
258#if @GNULIB_MALLOC_POSIX@ 262#if @GNULIB_MALLOC_POSIX@
259# if @REPLACE_MALLOC@ 263# if @REPLACE_MALLOC@
260# if !(defined __cplusplus && defined GNULIB_NAMESPACE) 264# if !((defined __cplusplus && defined GNULIB_NAMESPACE) \
265 || _GL_USE_STDLIB_ALLOC)
261# undef malloc 266# undef malloc
262# define malloc rpl_malloc 267# define malloc rpl_malloc
263# endif 268# endif
@@ -267,7 +272,7 @@ _GL_CXXALIAS_RPL (malloc, void *, (size_t size));
267_GL_CXXALIAS_SYS (malloc, void *, (size_t size)); 272_GL_CXXALIAS_SYS (malloc, void *, (size_t size));
268# endif 273# endif
269_GL_CXXALIASWARN (malloc); 274_GL_CXXALIASWARN (malloc);
270#elif defined GNULIB_POSIXCHECK 275#elif defined GNULIB_POSIXCHECK && !_GL_USE_STDLIB_ALLOC
271# undef malloc 276# undef malloc
272/* Assume malloc is always declared. */ 277/* Assume malloc is always declared. */
273_GL_WARN_ON_USE (malloc, "malloc is not POSIX compliant everywhere - " 278_GL_WARN_ON_USE (malloc, "malloc is not POSIX compliant everywhere - "
@@ -531,7 +536,8 @@ _GL_WARN_ON_USE (setstate_r, "setstate_r is unportable - "
531 536
532#if @GNULIB_REALLOC_POSIX@ 537#if @GNULIB_REALLOC_POSIX@
533# if @REPLACE_REALLOC@ 538# if @REPLACE_REALLOC@
534# if !(defined __cplusplus && defined GNULIB_NAMESPACE) 539# if !((defined __cplusplus && defined GNULIB_NAMESPACE) \
540 || _GL_USE_STDLIB_ALLOC)
535# undef realloc 541# undef realloc
536# define realloc rpl_realloc 542# define realloc rpl_realloc
537# endif 543# endif
@@ -541,7 +547,7 @@ _GL_CXXALIAS_RPL (realloc, void *, (void *ptr, size_t size));
541_GL_CXXALIAS_SYS (realloc, void *, (void *ptr, size_t size)); 547_GL_CXXALIAS_SYS (realloc, void *, (void *ptr, size_t size));
542# endif 548# endif
543_GL_CXXALIASWARN (realloc); 549_GL_CXXALIASWARN (realloc);
544#elif defined GNULIB_POSIXCHECK 550#elif defined GNULIB_POSIXCHECK && !_GL_USE_STDLIB_ALLOC
545# undef realloc 551# undef realloc
546/* Assume realloc is always declared. */ 552/* Assume realloc is always declared. */
547_GL_WARN_ON_USE (realloc, "realloc is not POSIX compliant everywhere - " 553_GL_WARN_ON_USE (realloc, "realloc is not POSIX compliant everywhere - "