Added to the repository by gnulib-sync
Showing
15 changed files
with
1153 additions
and
0 deletions
lib/allocsa.c
0 → 100644
1 | /* Safe automatic memory allocation. | ||
2 | Copyright (C) 2003 Free Software Foundation, Inc. | ||
3 | Written by Bruno Haible <bruno@clisp.org>, 2003. | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; if not, write to the Free Software Foundation, | ||
17 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | ||
18 | |||
19 | #ifdef HAVE_CONFIG_H | ||
20 | # include <config.h> | ||
21 | #endif | ||
22 | |||
23 | /* Specification. */ | ||
24 | #include "allocsa.h" | ||
25 | |||
26 | /* The speed critical point in this file is freesa() applied to an alloca() | ||
27 | result: it must be fast, to match the speed of alloca(). The speed of | ||
28 | mallocsa() and freesa() in the other case are not critical, because they | ||
29 | are only invoked for big memory sizes. */ | ||
30 | |||
31 | #if HAVE_ALLOCA | ||
32 | |||
33 | /* Store the mallocsa() results in a hash table. This is needed to reliably | ||
34 | distinguish a mallocsa() result and an alloca() result. | ||
35 | |||
36 | Although it is possible that the same pointer is returned by alloca() and | ||
37 | by mallocsa() at different times in the same application, it does not lead | ||
38 | to a bug in freesa(), because: | ||
39 | - Before a pointer returned by alloca() can point into malloc()ed memory, | ||
40 | the function must return, and once this has happened the programmer must | ||
41 | not call freesa() on it anyway. | ||
42 | - Before a pointer returned by mallocsa() can point into the stack, it | ||
43 | must be freed. The only function that can free it is freesa(), and | ||
44 | when freesa() frees it, it also removes it from the hash table. */ | ||
45 | |||
46 | #define MAGIC_NUMBER 0x1415fb4a | ||
47 | #define MAGIC_SIZE sizeof (int) | ||
48 | /* This is how the header info would look like without any alignment | ||
49 | considerations. */ | ||
50 | struct preliminary_header { void *next; char room[MAGIC_SIZE]; }; | ||
51 | /* But the header's size must be a multiple of sa_alignment_max. */ | ||
52 | #define HEADER_SIZE \ | ||
53 | (((sizeof (struct preliminary_header) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max) | ||
54 | struct header { void *next; char room[HEADER_SIZE - sizeof (struct preliminary_header) + MAGIC_SIZE]; }; | ||
55 | /* Verify that HEADER_SIZE == sizeof (struct header). */ | ||
56 | typedef int verify1[2 * (HEADER_SIZE == sizeof (struct header)) - 1]; | ||
57 | /* We make the hash table quite big, so that during lookups the probability | ||
58 | of empty hash buckets is quite high. There is no need to make the hash | ||
59 | table resizable, because when the hash table gets filled so much that the | ||
60 | lookup becomes slow, it means that the application has memory leaks. */ | ||
61 | #define HASH_TABLE_SIZE 257 | ||
62 | static void * mallocsa_results[HASH_TABLE_SIZE]; | ||
63 | |||
64 | #endif | ||
65 | |||
66 | void * | ||
67 | mallocsa (size_t n) | ||
68 | { | ||
69 | #if HAVE_ALLOCA | ||
70 | /* Allocate one more word, that serves as an indicator for malloc()ed | ||
71 | memory, so that freesa() of an alloca() result is fast. */ | ||
72 | size_t nplus = n + HEADER_SIZE; | ||
73 | |||
74 | if (nplus >= n) | ||
75 | { | ||
76 | char *p = (char *) malloc (nplus); | ||
77 | |||
78 | if (p != NULL) | ||
79 | { | ||
80 | size_t slot; | ||
81 | |||
82 | p += HEADER_SIZE; | ||
83 | |||
84 | /* Put a magic number into the indicator word. */ | ||
85 | ((int *) p)[-1] = MAGIC_NUMBER; | ||
86 | |||
87 | /* Enter p into the hash table. */ | ||
88 | slot = (unsigned long) p % HASH_TABLE_SIZE; | ||
89 | ((struct header *) (p - HEADER_SIZE))->next = mallocsa_results[slot]; | ||
90 | mallocsa_results[slot] = p; | ||
91 | |||
92 | return p; | ||
93 | } | ||
94 | } | ||
95 | /* Out of memory. */ | ||
96 | return NULL; | ||
97 | #else | ||
98 | # if !MALLOC_0_IS_NONNULL | ||
99 | if (n == 0) | ||
100 | n = 1; | ||
101 | # endif | ||
102 | return malloc (n); | ||
103 | #endif | ||
104 | } | ||
105 | |||
106 | #if HAVE_ALLOCA | ||
107 | void | ||
108 | freesa (void *p) | ||
109 | { | ||
110 | /* mallocsa() may have returned NULL. */ | ||
111 | if (p != NULL) | ||
112 | { | ||
113 | /* Attempt to quickly distinguish the mallocsa() result - which has | ||
114 | a magic indicator word - and the alloca() result - which has an | ||
115 | uninitialized indicator word. It is for this test that sa_increment | ||
116 | additional bytes are allocated in the alloca() case. */ | ||
117 | if (((int *) p)[-1] == MAGIC_NUMBER) | ||
118 | { | ||
119 | /* Looks like a mallocsa() result. To see whether it really is one, | ||
120 | perform a lookup in the hash table. */ | ||
121 | size_t slot = (unsigned long) p % HASH_TABLE_SIZE; | ||
122 | void **chain = &mallocsa_results[slot]; | ||
123 | for (; *chain != NULL;) | ||
124 | { | ||
125 | if (*chain == p) | ||
126 | { | ||
127 | /* Found it. Remove it from the hash table and free it. */ | ||
128 | char *p_begin = (char *) p - HEADER_SIZE; | ||
129 | *chain = ((struct header *) p_begin)->next; | ||
130 | free (p_begin); | ||
131 | return; | ||
132 | } | ||
133 | chain = &((struct header *) ((char *) *chain - HEADER_SIZE))->next; | ||
134 | } | ||
135 | } | ||
136 | /* At this point, we know it was not a mallocsa() result. */ | ||
137 | } | ||
138 | } | ||
139 | #endif |
lib/allocsa.h
0 → 100644
1 | /* Safe automatic memory allocation. | ||
2 | Copyright (C) 2003-2004 Free Software Foundation, Inc. | ||
3 | Written by Bruno Haible <bruno@clisp.org>, 2003. | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; if not, write to the Free Software Foundation, | ||
17 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | ||
18 | |||
19 | #ifndef _ALLOCSA_H | ||
20 | #define _ALLOCSA_H | ||
21 | |||
22 | #include <alloca.h> | ||
23 | #include <stddef.h> | ||
24 | #include <stdlib.h> | ||
25 | |||
26 | /* safe_alloca(N) is equivalent to alloca(N) when it is safe to call | ||
27 | alloca(N); otherwise it returns NULL. It either returns N bytes of | ||
28 | memory allocated on the stack, that lasts until the function returns, | ||
29 | or NULL. | ||
30 | Use of safe_alloca should be avoided: | ||
31 | - inside arguments of function calls - undefined behaviour, | ||
32 | - in inline functions - the allocation may actually last until the | ||
33 | calling function returns. | ||
34 | */ | ||
35 | #if HAVE_ALLOCA | ||
36 | /* The OS usually guarantees only one guard page at the bottom of the stack, | ||
37 | and a page size can be as small as 4096 bytes. So we cannot safely | ||
38 | allocate anything larger than 4096 bytes. Also care for the possibility | ||
39 | of a few compiler-allocated temporary stack slots. | ||
40 | This must be a macro, not an inline function. */ | ||
41 | # define safe_alloca(N) ((N) < 4032 ? alloca (N) : NULL) | ||
42 | #else | ||
43 | # define safe_alloca(N) ((N), NULL) | ||
44 | #endif | ||
45 | |||
46 | /* allocsa(N) is a safe variant of alloca(N). It allocates N bytes of | ||
47 | memory allocated on the stack, that must be freed using freesa() before | ||
48 | the function returns. Upon failure, it returns NULL. */ | ||
49 | #if HAVE_ALLOCA | ||
50 | # define allocsa(N) \ | ||
51 | ((N) < 4032 - sa_increment \ | ||
52 | ? (void *) ((char *) alloca ((N) + sa_increment) + sa_increment) \ | ||
53 | : mallocsa (N)) | ||
54 | #else | ||
55 | # define allocsa(N) \ | ||
56 | mallocsa (N) | ||
57 | #endif | ||
58 | extern void * mallocsa (size_t n); | ||
59 | |||
60 | /* Free a block of memory allocated through allocsa(). */ | ||
61 | #if HAVE_ALLOCA | ||
62 | extern void freesa (void *p); | ||
63 | #else | ||
64 | # define freesa free | ||
65 | #endif | ||
66 | |||
67 | /* Maybe we should also define a variant | ||
68 | nallocsa (size_t n, size_t s) - behaves like allocsa (n * s) | ||
69 | If this would be useful in your application. please speak up. */ | ||
70 | |||
71 | |||
72 | /* ------------------- Auxiliary, non-public definitions ------------------- */ | ||
73 | |||
74 | /* Determine the alignment of a type at compile time. */ | ||
75 | #if defined __GNUC__ | ||
76 | # define sa_alignof __alignof__ | ||
77 | #elif defined __cplusplus | ||
78 | template <class type> struct sa_alignof_helper { char __slot1; type __slot2; }; | ||
79 | # define sa_alignof(type) offsetof (sa_alignof_helper<type>, __slot2) | ||
80 | #elif defined __hpux | ||
81 | /* Work around a HP-UX 10.20 cc bug with enums constants defined as offsetof | ||
82 | values. */ | ||
83 | # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8) | ||
84 | #else | ||
85 | # define sa_alignof(type) offsetof (struct { char __slot1; type __slot2; }, __slot2) | ||
86 | #endif | ||
87 | |||
88 | enum | ||
89 | { | ||
90 | /* The desired alignment of memory allocations is the maximum alignment | ||
91 | among all elementary types. */ | ||
92 | sa_alignment_long = sa_alignof (long), | ||
93 | sa_alignment_double = sa_alignof (double), | ||
94 | #ifdef HAVE_LONG_LONG | ||
95 | sa_alignment_longlong = sa_alignof (long long), | ||
96 | #endif | ||
97 | #ifdef HAVE_LONG_DOUBLE | ||
98 | sa_alignment_longdouble = sa_alignof (long double), | ||
99 | #endif | ||
100 | sa_alignment_max = ((sa_alignment_long - 1) | (sa_alignment_double - 1) | ||
101 | #ifdef HAVE_LONG_LONG | ||
102 | | (sa_alignment_longlong - 1) | ||
103 | #endif | ||
104 | #ifdef HAVE_LONG_DOUBLE | ||
105 | | (sa_alignment_longdouble - 1) | ||
106 | #endif | ||
107 | ) + 1, | ||
108 | /* The increment that guarantees room for a magic word must be >= sizeof (int) | ||
109 | and a multiple of sa_alignment_max. */ | ||
110 | sa_increment = ((sizeof (int) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max | ||
111 | }; | ||
112 | |||
113 | #endif /* _ALLOCSA_H */ |
lib/allocsa.valgrind
0 → 100644
lib/error.c
0 → 100644
1 | /* Error handler for noninteractive utilities | ||
2 | Copyright (C) 1990-1998, 2000-2003, 2004 Free Software Foundation, Inc. | ||
3 | This file is part of the GNU C Library. | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License along | ||
16 | with this program; if not, write to the Free Software Foundation, | ||
17 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | ||
18 | |||
19 | /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */ | ||
20 | |||
21 | #ifdef HAVE_CONFIG_H | ||
22 | # include <config.h> | ||
23 | #endif | ||
24 | |||
25 | #include "error.h" | ||
26 | |||
27 | #include <stdarg.h> | ||
28 | #include <stdio.h> | ||
29 | #include <stdlib.h> | ||
30 | #include <string.h> | ||
31 | |||
32 | #if !_LIBC && ENABLE_NLS | ||
33 | # include "gettext.h" | ||
34 | #endif | ||
35 | |||
36 | #ifdef _LIBC | ||
37 | # include <wchar.h> | ||
38 | # define mbsrtowcs __mbsrtowcs | ||
39 | #endif | ||
40 | |||
41 | #if USE_UNLOCKED_IO | ||
42 | # include "unlocked-io.h" | ||
43 | #endif | ||
44 | |||
45 | #ifndef _ | ||
46 | # define _(String) String | ||
47 | #endif | ||
48 | |||
49 | /* If NULL, error will flush stdout, then print on stderr the program | ||
50 | name, a colon and a space. Otherwise, error will call this | ||
51 | function without parameters instead. */ | ||
52 | void (*error_print_progname) (void); | ||
53 | |||
54 | /* This variable is incremented each time `error' is called. */ | ||
55 | unsigned int error_message_count; | ||
56 | |||
57 | #ifdef _LIBC | ||
58 | /* In the GNU C library, there is a predefined variable for this. */ | ||
59 | |||
60 | # define program_name program_invocation_name | ||
61 | # include <errno.h> | ||
62 | # include <libio/libioP.h> | ||
63 | |||
64 | /* In GNU libc we want do not want to use the common name `error' directly. | ||
65 | Instead make it a weak alias. */ | ||
66 | extern void __error (int status, int errnum, const char *message, ...) | ||
67 | __attribute__ ((__format__ (__printf__, 3, 4))); | ||
68 | extern void __error_at_line (int status, int errnum, const char *file_name, | ||
69 | unsigned int line_number, const char *message, | ||
70 | ...) | ||
71 | __attribute__ ((__format__ (__printf__, 5, 6)));; | ||
72 | # define error __error | ||
73 | # define error_at_line __error_at_line | ||
74 | |||
75 | # include <libio/iolibio.h> | ||
76 | # define fflush(s) INTUSE(_IO_fflush) (s) | ||
77 | # undef putc | ||
78 | # define putc(c, fp) INTUSE(_IO_putc) (c, fp) | ||
79 | |||
80 | # include <bits/libc-lock.h> | ||
81 | |||
82 | #else /* not _LIBC */ | ||
83 | |||
84 | # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P | ||
85 | # ifndef HAVE_DECL_STRERROR_R | ||
86 | "this configure-time declaration test was not run" | ||
87 | # endif | ||
88 | char *strerror_r (); | ||
89 | # endif | ||
90 | |||
91 | # ifndef SIZE_MAX | ||
92 | # define SIZE_MAX ((size_t) -1) | ||
93 | # endif | ||
94 | |||
95 | /* The calling program should define program_name and set it to the | ||
96 | name of the executing program. */ | ||
97 | extern char *program_name; | ||
98 | |||
99 | # if HAVE_STRERROR_R || defined strerror_r | ||
100 | # define __strerror_r strerror_r | ||
101 | # endif | ||
102 | #endif /* not _LIBC */ | ||
103 | |||
104 | static void | ||
105 | print_errno_message (int errnum) | ||
106 | { | ||
107 | char const *s = NULL; | ||
108 | |||
109 | #if defined HAVE_STRERROR_R || _LIBC | ||
110 | char errbuf[1024]; | ||
111 | # if STRERROR_R_CHAR_P || _LIBC | ||
112 | s = __strerror_r (errnum, errbuf, sizeof errbuf); | ||
113 | # else | ||
114 | if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0) | ||
115 | s = errbuf; | ||
116 | # endif | ||
117 | #endif | ||
118 | |||
119 | #if !_LIBC | ||
120 | if (! s && ! (s = strerror (errnum))) | ||
121 | s = _("Unknown system error"); | ||
122 | #endif | ||
123 | |||
124 | #if _LIBC | ||
125 | if (_IO_fwide (stderr, 0) > 0) | ||
126 | { | ||
127 | __fwprintf (stderr, L": %s", s); | ||
128 | return; | ||
129 | } | ||
130 | #endif | ||
131 | |||
132 | fprintf (stderr, ": %s", s); | ||
133 | } | ||
134 | |||
135 | static void | ||
136 | error_tail (int status, int errnum, const char *message, va_list args) | ||
137 | { | ||
138 | #if _LIBC | ||
139 | if (_IO_fwide (stderr, 0) > 0) | ||
140 | { | ||
141 | # define ALLOCA_LIMIT 2000 | ||
142 | size_t len = strlen (message) + 1; | ||
143 | const wchar_t *wmessage = L"out of memory"; | ||
144 | wchar_t *wbuf = (len < ALLOCA_LIMIT | ||
145 | ? alloca (len * sizeof *wbuf) | ||
146 | : len <= SIZE_MAX / sizeof *wbuf | ||
147 | ? malloc (len * sizeof *wbuf) | ||
148 | : NULL); | ||
149 | |||
150 | if (wbuf) | ||
151 | { | ||
152 | size_t res; | ||
153 | mbstate_t st; | ||
154 | const char *tmp = message; | ||
155 | memset (&st, '\0', sizeof (st)); | ||
156 | res = mbsrtowcs (wbuf, &tmp, len, &st); | ||
157 | wmessage = res == (size_t) -1 ? L"???" : wbuf; | ||
158 | } | ||
159 | |||
160 | __vfwprintf (stderr, wmessage, args); | ||
161 | if (! (len < ALLOCA_LIMIT)) | ||
162 | free (wbuf); | ||
163 | } | ||
164 | else | ||
165 | #endif | ||
166 | vfprintf (stderr, message, args); | ||
167 | va_end (args); | ||
168 | |||
169 | ++error_message_count; | ||
170 | if (errnum) | ||
171 | print_errno_message (errnum); | ||
172 | #if _LIBC | ||
173 | if (_IO_fwide (stderr, 0) > 0) | ||
174 | putwc (L'\n', stderr); | ||
175 | else | ||
176 | #endif | ||
177 | putc ('\n', stderr); | ||
178 | fflush (stderr); | ||
179 | if (status) | ||
180 | exit (status); | ||
181 | } | ||
182 | |||
183 | |||
184 | /* Print the program name and error message MESSAGE, which is a printf-style | ||
185 | format string with optional args. | ||
186 | If ERRNUM is nonzero, print its corresponding system error message. | ||
187 | Exit with status STATUS if it is nonzero. */ | ||
188 | void | ||
189 | error (int status, int errnum, const char *message, ...) | ||
190 | { | ||
191 | va_list args; | ||
192 | |||
193 | #if defined _LIBC && defined __libc_ptf_call | ||
194 | /* We do not want this call to be cut short by a thread | ||
195 | cancellation. Therefore disable cancellation for now. */ | ||
196 | int state = PTHREAD_CANCEL_ENABLE; | ||
197 | __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state), | ||
198 | 0); | ||
199 | #endif | ||
200 | |||
201 | fflush (stdout); | ||
202 | #ifdef _LIBC | ||
203 | _IO_flockfile (stderr); | ||
204 | #endif | ||
205 | if (error_print_progname) | ||
206 | (*error_print_progname) (); | ||
207 | else | ||
208 | { | ||
209 | #if _LIBC | ||
210 | if (_IO_fwide (stderr, 0) > 0) | ||
211 | __fwprintf (stderr, L"%s: ", program_name); | ||
212 | else | ||
213 | #endif | ||
214 | fprintf (stderr, "%s: ", program_name); | ||
215 | } | ||
216 | |||
217 | va_start (args, message); | ||
218 | error_tail (status, errnum, message, args); | ||
219 | |||
220 | #ifdef _LIBC | ||
221 | _IO_funlockfile (stderr); | ||
222 | # ifdef __libc_ptf_call | ||
223 | __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0); | ||
224 | # endif | ||
225 | #endif | ||
226 | } | ||
227 | |||
228 | /* Sometimes we want to have at most one error per line. This | ||
229 | variable controls whether this mode is selected or not. */ | ||
230 | int error_one_per_line; | ||
231 | |||
232 | void | ||
233 | error_at_line (int status, int errnum, const char *file_name, | ||
234 | unsigned int line_number, const char *message, ...) | ||
235 | { | ||
236 | va_list args; | ||
237 | |||
238 | if (error_one_per_line) | ||
239 | { | ||
240 | static const char *old_file_name; | ||
241 | static unsigned int old_line_number; | ||
242 | |||
243 | if (old_line_number == line_number | ||
244 | && (file_name == old_file_name | ||
245 | || strcmp (old_file_name, file_name) == 0)) | ||
246 | /* Simply return and print nothing. */ | ||
247 | return; | ||
248 | |||
249 | old_file_name = file_name; | ||
250 | old_line_number = line_number; | ||
251 | } | ||
252 | |||
253 | #if defined _LIBC && defined __libc_ptf_call | ||
254 | /* We do not want this call to be cut short by a thread | ||
255 | cancellation. Therefore disable cancellation for now. */ | ||
256 | int state = PTHREAD_CANCEL_ENABLE; | ||
257 | __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state), | ||
258 | 0); | ||
259 | #endif | ||
260 | |||
261 | fflush (stdout); | ||
262 | #ifdef _LIBC | ||
263 | _IO_flockfile (stderr); | ||
264 | #endif | ||
265 | if (error_print_progname) | ||
266 | (*error_print_progname) (); | ||
267 | else | ||
268 | { | ||
269 | #if _LIBC | ||
270 | if (_IO_fwide (stderr, 0) > 0) | ||
271 | __fwprintf (stderr, L"%s: ", program_name); | ||
272 | else | ||
273 | #endif | ||
274 | fprintf (stderr, "%s:", program_name); | ||
275 | } | ||
276 | |||
277 | if (file_name != NULL) | ||
278 | { | ||
279 | #if _LIBC | ||
280 | if (_IO_fwide (stderr, 0) > 0) | ||
281 | __fwprintf (stderr, L"%s:%d: ", file_name, line_number); | ||
282 | else | ||
283 | #endif | ||
284 | fprintf (stderr, "%s:%d: ", file_name, line_number); | ||
285 | } | ||
286 | |||
287 | va_start (args, message); | ||
288 | error_tail (status, errnum, message, args); | ||
289 | |||
290 | #ifdef _LIBC | ||
291 | _IO_funlockfile (stderr); | ||
292 | # ifdef __libc_ptf_call | ||
293 | __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0); | ||
294 | # endif | ||
295 | #endif | ||
296 | } | ||
297 | |||
298 | #ifdef _LIBC | ||
299 | /* Make the weak alias. */ | ||
300 | # undef error | ||
301 | # undef error_at_line | ||
302 | weak_alias (__error, error) | ||
303 | weak_alias (__error_at_line, error_at_line) | ||
304 | #endif |
lib/exit.h
0 → 100644
1 | /* exit() function. | ||
2 | Copyright (C) 1995, 2001 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software Foundation, | ||
16 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | ||
17 | |||
18 | #ifndef _EXIT_H | ||
19 | #define _EXIT_H | ||
20 | |||
21 | /* Get exit() declaration. */ | ||
22 | #include <stdlib.h> | ||
23 | |||
24 | /* Some systems do not define EXIT_*, even with STDC_HEADERS. */ | ||
25 | #ifndef EXIT_SUCCESS | ||
26 | # define EXIT_SUCCESS 0 | ||
27 | #endif | ||
28 | #ifndef EXIT_FAILURE | ||
29 | # define EXIT_FAILURE 1 | ||
30 | #endif | ||
31 | |||
32 | #endif /* _EXIT_H */ |
lib/exitfail.c
0 → 100644
1 | /* Failure exit status | ||
2 | |||
3 | Copyright (C) 2002, 2003 Free Software Foundation, Inc. | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; see the file COPYING. | ||
17 | If not, write to the Free Software Foundation, | ||
18 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | ||
19 | |||
20 | #if HAVE_CONFIG_H | ||
21 | # include <config.h> | ||
22 | #endif | ||
23 | |||
24 | #include "exitfail.h" | ||
25 | #include "exit.h" | ||
26 | |||
27 | int volatile exit_failure = EXIT_FAILURE; |
lib/exitfail.h
0 → 100644
1 | /* Failure exit status | ||
2 | |||
3 | Copyright (C) 2002 Free Software Foundation, Inc. | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; see the file COPYING. | ||
17 | If not, write to the Free Software Foundation, | ||
18 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | ||
19 | |||
20 | extern int volatile exit_failure; |
lib/fnmatch_.h
0 → 100644
1 | /* Copyright (C) 1991, 1992, 1993, 1996, 1997, 1998, 1999, 2001, 2002, 2003 | ||
2 | Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software Foundation, | ||
16 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | ||
17 | |||
18 | #ifndef _FNMATCH_H | ||
19 | # define _FNMATCH_H 1 | ||
20 | |||
21 | # ifdef __cplusplus | ||
22 | extern "C" { | ||
23 | # endif | ||
24 | |||
25 | /* We #undef these before defining them because some losing systems | ||
26 | (HP-UX A.08.07 for example) define these in <unistd.h>. */ | ||
27 | # undef FNM_PATHNAME | ||
28 | # undef FNM_NOESCAPE | ||
29 | # undef FNM_PERIOD | ||
30 | |||
31 | /* Bits set in the FLAGS argument to `fnmatch'. */ | ||
32 | # define FNM_PATHNAME (1 << 0) /* No wildcard can ever match `/'. */ | ||
33 | # define FNM_NOESCAPE (1 << 1) /* Backslashes don't quote special chars. */ | ||
34 | # define FNM_PERIOD (1 << 2) /* Leading `.' is matched only explicitly. */ | ||
35 | |||
36 | # if !defined _POSIX_C_SOURCE || _POSIX_C_SOURCE < 2 || defined _GNU_SOURCE | ||
37 | # define FNM_FILE_NAME FNM_PATHNAME /* Preferred GNU name. */ | ||
38 | # define FNM_LEADING_DIR (1 << 3) /* Ignore `/...' after a match. */ | ||
39 | # define FNM_CASEFOLD (1 << 4) /* Compare without regard to case. */ | ||
40 | # define FNM_EXTMATCH (1 << 5) /* Use ksh-like extended matching. */ | ||
41 | # endif | ||
42 | |||
43 | /* Value returned by `fnmatch' if STRING does not match PATTERN. */ | ||
44 | # define FNM_NOMATCH 1 | ||
45 | |||
46 | /* This value is returned if the implementation does not support | ||
47 | `fnmatch'. Since this is not the case here it will never be | ||
48 | returned but the conformance test suites still require the symbol | ||
49 | to be defined. */ | ||
50 | # ifdef _XOPEN_SOURCE | ||
51 | # define FNM_NOSYS (-1) | ||
52 | # endif | ||
53 | |||
54 | /* Match NAME against the filename pattern PATTERN, | ||
55 | returning zero if it matches, FNM_NOMATCH if not. */ | ||
56 | extern int fnmatch (const char *__pattern, const char *__name, | ||
57 | int __flags); | ||
58 | |||
59 | # ifdef __cplusplus | ||
60 | } | ||
61 | # endif | ||
62 | |||
63 | #endif /* fnmatch.h */ |
lib/fnmatch_loop.c
0 → 100644
This diff is collapsed.
Click to expand it.
lib/getpass.h
0 → 100644
1 | /* getpass.h -- Read a password of arbitrary length from /dev/tty or stdin. | ||
2 | Copyright (C) 2004 Free Software Foundation, Inc. | ||
3 | Contributed by Simon Josefsson <jas@extundo.com>, 2004. | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; if not, write to the Free Software Foundation, | ||
17 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | ||
18 | |||
19 | #ifndef GETPASS_H | ||
20 | #define GETPASS_H | ||
21 | |||
22 | /* Get getpass declaration, if available. */ | ||
23 | #include <unistd.h> | ||
24 | |||
25 | #if defined HAVE_DECL_GETPASS && !HAVE_DECL_GETPASS | ||
26 | /* Read a password of arbitrary length from /dev/tty or stdin. */ | ||
27 | char *getpass (const char *prompt); | ||
28 | |||
29 | #endif | ||
30 | |||
31 | #endif /* GETPASS_H */ |
lib/setenv.h
0 → 100644
1 | /* Setting environment variables. | ||
2 | Copyright (C) 2001-2004 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software Foundation, | ||
16 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | ||
17 | |||
18 | #if HAVE_SETENV || HAVE_UNSETENV | ||
19 | |||
20 | /* Get setenv(), unsetenv() declarations. */ | ||
21 | # include <stdlib.h> | ||
22 | |||
23 | #endif | ||
24 | |||
25 | #ifdef __cplusplus | ||
26 | extern "C" { | ||
27 | #endif | ||
28 | |||
29 | #if !HAVE_SETENV | ||
30 | |||
31 | /* Set NAME to VALUE in the environment. | ||
32 | If REPLACE is nonzero, overwrite an existing value. */ | ||
33 | extern int setenv (const char *name, const char *value, int replace); | ||
34 | |||
35 | #endif | ||
36 | |||
37 | #if HAVE_UNSETENV | ||
38 | |||
39 | # if VOID_UNSETENV | ||
40 | /* On some systems, unsetenv() returns void. | ||
41 | This is the case for FreeBSD 4.8, NetBSD 1.6, OpenBSD 3.4. */ | ||
42 | # define unsetenv(name) ((unsetenv)(name), 0) | ||
43 | # endif | ||
44 | |||
45 | #else | ||
46 | |||
47 | /* Remove the variable NAME from the environment. */ | ||
48 | extern int unsetenv (const char *name); | ||
49 | |||
50 | #endif | ||
51 | |||
52 | #ifdef __cplusplus | ||
53 | } | ||
54 | #endif |
lib/stdbool_.h
0 → 100644
1 | /* Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc. | ||
2 | Written by Bruno Haible <haible@clisp.cons.org>, 2001. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software Foundation, | ||
16 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | ||
17 | |||
18 | #ifndef _STDBOOL_H | ||
19 | #define _STDBOOL_H | ||
20 | |||
21 | /* ISO C 99 <stdbool.h> for platforms that lack it. */ | ||
22 | |||
23 | /* Usage suggestions: | ||
24 | |||
25 | Programs that use <stdbool.h> should be aware of some limitations | ||
26 | and standards compliance issues. | ||
27 | |||
28 | Standards compliance: | ||
29 | |||
30 | - <stdbool.h> must be #included before 'bool', 'false', 'true' | ||
31 | can be used. | ||
32 | |||
33 | - You cannot assume that sizeof (bool) == 1. | ||
34 | |||
35 | - Programs should not undefine the macros bool, true, and false, | ||
36 | as C99 lists that as an "obsolescent feature". | ||
37 | |||
38 | Limitations of this substitute, when used in a C89 environment: | ||
39 | |||
40 | - <stdbool.h> must be #included before the '_Bool' type can be used. | ||
41 | |||
42 | - You cannot assume that _Bool is a typedef; it might be a macro. | ||
43 | |||
44 | - In C99, casts and automatic conversions to '_Bool' or 'bool' are | ||
45 | performed in such a way that every nonzero value gets converted | ||
46 | to 'true', and zero gets converted to 'false'. This doesn't work | ||
47 | with this substitute. With this substitute, only the values 0 and 1 | ||
48 | give the expected result when converted to _Bool' or 'bool'. | ||
49 | |||
50 | Also, it is suggested that programs use 'bool' rather than '_Bool'; | ||
51 | this isn't required, but 'bool' is more common. */ | ||
52 | |||
53 | |||
54 | /* 7.16. Boolean type and values */ | ||
55 | |||
56 | /* BeOS <sys/socket.h> already #defines false 0, true 1. We use the same | ||
57 | definitions below, but temporarily we have to #undef them. */ | ||
58 | #ifdef __BEOS__ | ||
59 | # include <OS.h> /* defines bool but not _Bool */ | ||
60 | # undef false | ||
61 | # undef true | ||
62 | #endif | ||
63 | |||
64 | /* For the sake of symbolic names in gdb, we define true and false as | ||
65 | enum constants, not only as macros. | ||
66 | It is tempting to write | ||
67 | typedef enum { false = 0, true = 1 } _Bool; | ||
68 | so that gdb prints values of type 'bool' symbolically. But if we do | ||
69 | this, values of type '_Bool' may promote to 'int' or 'unsigned int' | ||
70 | (see ISO C 99 6.7.2.2.(4)); however, '_Bool' must promote to 'int' | ||
71 | (see ISO C 99 6.3.1.1.(2)). So we add a negative value to the | ||
72 | enum; this ensures that '_Bool' promotes to 'int'. */ | ||
73 | #if !(defined __cplusplus || defined __BEOS__) | ||
74 | # if !@HAVE__BOOL@ | ||
75 | # if defined __SUNPRO_C && (__SUNPRO_C < 0x550 || __STDC__ == 1) | ||
76 | /* Avoid stupid "warning: _Bool is a keyword in ISO C99". */ | ||
77 | # define _Bool signed char | ||
78 | enum { false = 0, true = 1 }; | ||
79 | # else | ||
80 | typedef enum { _Bool_must_promote_to_int = -1, false = 0, true = 1 } _Bool; | ||
81 | # endif | ||
82 | # endif | ||
83 | #else | ||
84 | typedef bool _Bool; | ||
85 | #endif | ||
86 | #define bool _Bool | ||
87 | |||
88 | /* The other macros must be usable in preprocessor directives. */ | ||
89 | #define false 0 | ||
90 | #define true 1 | ||
91 | #define __bool_true_false_are_defined 1 | ||
92 | |||
93 | #endif /* _STDBOOL_H */ |
lib/unsetenv.c
0 → 100644
1 | /* Copyright (C) 1992,1995-1999,2000-2002 Free Software Foundation, Inc. | ||
2 | This file is part of the GNU C Library. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License along | ||
15 | with this program; if not, write to the Free Software Foundation, | ||
16 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | ||
17 | |||
18 | #if HAVE_CONFIG_H | ||
19 | # include <config.h> | ||
20 | #endif | ||
21 | |||
22 | #include <errno.h> | ||
23 | #if !_LIBC | ||
24 | # if !defined errno && !defined HAVE_ERRNO_DECL | ||
25 | extern int errno; | ||
26 | # endif | ||
27 | # define __set_errno(ev) ((errno) = (ev)) | ||
28 | #endif | ||
29 | |||
30 | #include <stdlib.h> | ||
31 | #include <string.h> | ||
32 | #if _LIBC || HAVE_UNISTD_H | ||
33 | # include <unistd.h> | ||
34 | #endif | ||
35 | |||
36 | #if !_LIBC | ||
37 | # define __environ environ | ||
38 | # ifndef HAVE_ENVIRON_DECL | ||
39 | extern char **environ; | ||
40 | # endif | ||
41 | #endif | ||
42 | |||
43 | #if _LIBC | ||
44 | /* This lock protects against simultaneous modifications of `environ'. */ | ||
45 | # include <bits/libc-lock.h> | ||
46 | __libc_lock_define_initialized (static, envlock) | ||
47 | # define LOCK __libc_lock_lock (envlock) | ||
48 | # define UNLOCK __libc_lock_unlock (envlock) | ||
49 | #else | ||
50 | # define LOCK | ||
51 | # define UNLOCK | ||
52 | #endif | ||
53 | |||
54 | /* In the GNU C library we must keep the namespace clean. */ | ||
55 | #ifdef _LIBC | ||
56 | # define unsetenv __unsetenv | ||
57 | #endif | ||
58 | |||
59 | |||
60 | int | ||
61 | unsetenv (const char *name) | ||
62 | { | ||
63 | size_t len; | ||
64 | char **ep; | ||
65 | |||
66 | if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) | ||
67 | { | ||
68 | __set_errno (EINVAL); | ||
69 | return -1; | ||
70 | } | ||
71 | |||
72 | len = strlen (name); | ||
73 | |||
74 | LOCK; | ||
75 | |||
76 | ep = __environ; | ||
77 | while (*ep != NULL) | ||
78 | if (!strncmp (*ep, name, len) && (*ep)[len] == '=') | ||
79 | { | ||
80 | /* Found it. Remove this pointer by moving later ones back. */ | ||
81 | char **dp = ep; | ||
82 | |||
83 | do | ||
84 | dp[0] = dp[1]; | ||
85 | while (*dp++); | ||
86 | /* Continue the loop in case NAME appears again. */ | ||
87 | } | ||
88 | else | ||
89 | ++ep; | ||
90 | |||
91 | UNLOCK; | ||
92 | |||
93 | return 0; | ||
94 | } | ||
95 | |||
96 | #ifdef _LIBC | ||
97 | # undef unsetenv | ||
98 | weak_alias (__unsetenv, unsetenv) | ||
99 | #endif |
lib/vasprintf.h
0 → 100644
1 | /* vsprintf with automatic memory allocation. | ||
2 | Copyright (C) 2002-2003 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License along | ||
15 | with this program; if not, write to the Free Software Foundation, | ||
16 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | ||
17 | |||
18 | #ifndef _VASPRINTF_H | ||
19 | #define _VASPRINTF_H | ||
20 | |||
21 | #if HAVE_VASPRINTF | ||
22 | |||
23 | /* Get asprintf(), vasprintf() declarations. */ | ||
24 | #include <stdio.h> | ||
25 | |||
26 | #else | ||
27 | |||
28 | /* Get va_list. */ | ||
29 | #include <stdarg.h> | ||
30 | |||
31 | #ifndef __attribute__ | ||
32 | /* This feature is available in gcc versions 2.5 and later. */ | ||
33 | # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__ | ||
34 | # define __attribute__(Spec) /* empty */ | ||
35 | # endif | ||
36 | /* The __-protected variants of `format' and `printf' attributes | ||
37 | are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */ | ||
38 | # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) | ||
39 | # define __format__ format | ||
40 | # define __printf__ printf | ||
41 | # endif | ||
42 | #endif | ||
43 | |||
44 | #ifdef __cplusplus | ||
45 | extern "C" { | ||
46 | #endif | ||
47 | |||
48 | /* Write formatted output to a string dynamically allocated with malloc(). | ||
49 | If the memory allocation succeeds, store the address of the string in | ||
50 | *RESULT and return the number of resulting bytes, excluding the trailing | ||
51 | NUL. Upon memory allocation error, or some other error, return -1. */ | ||
52 | extern int asprintf (char **result, const char *format, ...) | ||
53 | __attribute__ ((__format__ (__printf__, 2, 3))); | ||
54 | extern int vasprintf (char **result, const char *format, va_list args) | ||
55 | __attribute__ ((__format__ (__printf__, 2, 0))); | ||
56 | |||
57 | #ifdef __cplusplus | ||
58 | } | ||
59 | #endif | ||
60 | |||
61 | #endif | ||
62 | |||
63 | #endif /* _VASPRINTF_H */ |
lib/xsize.h
0 → 100644
1 | /* xsize.h -- Checked size_t computations. | ||
2 | |||
3 | Copyright (C) 2003 Free Software Foundation, Inc. | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; if not, write to the Free Software Foundation, | ||
17 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | ||
18 | |||
19 | #ifndef _XSIZE_H | ||
20 | #define _XSIZE_H | ||
21 | |||
22 | /* Get size_t. */ | ||
23 | #include <stddef.h> | ||
24 | |||
25 | /* Get SIZE_MAX. */ | ||
26 | #include <limits.h> | ||
27 | #if HAVE_STDINT_H | ||
28 | # include <stdint.h> | ||
29 | #endif | ||
30 | |||
31 | /* The size of memory objects is often computed through expressions of | ||
32 | type size_t. Example: | ||
33 | void* p = malloc (header_size + n * element_size). | ||
34 | These computations can lead to overflow. When this happens, malloc() | ||
35 | returns a piece of memory that is way too small, and the program then | ||
36 | crashes while attempting to fill the memory. | ||
37 | To avoid this, the functions and macros in this file check for overflow. | ||
38 | The convention is that SIZE_MAX represents overflow. | ||
39 | malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc | ||
40 | implementation that uses mmap --, it's recommended to use size_overflow_p() | ||
41 | or size_in_bounds_p() before invoking malloc(). | ||
42 | The example thus becomes: | ||
43 | size_t size = xsum (header_size, xtimes (n, element_size)); | ||
44 | void *p = (size_in_bounds_p (size) ? malloc (size) : NULL); | ||
45 | */ | ||
46 | |||
47 | /* Convert an arbitrary value >= 0 to type size_t. */ | ||
48 | #define xcast_size_t(N) \ | ||
49 | ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX) | ||
50 | |||
51 | /* Sum of two sizes, with overflow check. */ | ||
52 | static inline size_t | ||
53 | #if __GNUC__ >= 3 | ||
54 | __attribute__ ((__pure__)) | ||
55 | #endif | ||
56 | xsum (size_t size1, size_t size2) | ||
57 | { | ||
58 | size_t sum = size1 + size2; | ||
59 | return (sum >= size1 ? sum : SIZE_MAX); | ||
60 | } | ||
61 | |||
62 | /* Sum of three sizes, with overflow check. */ | ||
63 | static inline size_t | ||
64 | #if __GNUC__ >= 3 | ||
65 | __attribute__ ((__pure__)) | ||
66 | #endif | ||
67 | xsum3 (size_t size1, size_t size2, size_t size3) | ||
68 | { | ||
69 | return xsum (xsum (size1, size2), size3); | ||
70 | } | ||
71 | |||
72 | /* Sum of four sizes, with overflow check. */ | ||
73 | static inline size_t | ||
74 | #if __GNUC__ >= 3 | ||
75 | __attribute__ ((__pure__)) | ||
76 | #endif | ||
77 | xsum4 (size_t size1, size_t size2, size_t size3, size_t size4) | ||
78 | { | ||
79 | return xsum (xsum (xsum (size1, size2), size3), size4); | ||
80 | } | ||
81 | |||
82 | /* Maximum of two sizes, with overflow check. */ | ||
83 | static inline size_t | ||
84 | #if __GNUC__ >= 3 | ||
85 | __attribute__ ((__pure__)) | ||
86 | #endif | ||
87 | xmax (size_t size1, size_t size2) | ||
88 | { | ||
89 | /* No explicit check is needed here, because for any n: | ||
90 | max (SIZE_MAX, n) == SIZE_MAX and max (n, SIZE_MAX) == SIZE_MAX. */ | ||
91 | return (size1 >= size2 ? size1 : size2); | ||
92 | } | ||
93 | |||
94 | /* Multiplication of a count with an element size, with overflow check. | ||
95 | The count must be >= 0 and the element size must be > 0. | ||
96 | This is a macro, not an inline function, so that it works correctly even | ||
97 | when N is of a wider tupe and N > SIZE_MAX. */ | ||
98 | #define xtimes(N, ELSIZE) \ | ||
99 | ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX) | ||
100 | |||
101 | /* Check for overflow. */ | ||
102 | #define size_overflow_p(SIZE) \ | ||
103 | ((SIZE) == SIZE_MAX) | ||
104 | /* Check against overflow. */ | ||
105 | #define size_in_bounds_p(SIZE) \ | ||
106 | ((SIZE) != SIZE_MAX) | ||
107 | |||
108 | #endif /* _XSIZE_H */ |
-
Please register or sign in to post a comment