Mercurial > gnulib
annotate lib/xmalloc.c @ 40186:8964917f9574
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Mon, 18 Feb 2019 08:02:49 -0800 |
parents | b06060465f09 |
children |
rev | line source |
---|---|
9 | 1 /* xmalloc.c -- malloc with out of memory checking |
4397
c6450308f123
Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4000
diff
changeset
|
2 |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
3 Copyright (C) 1990-2000, 2002-2006, 2008-2019 Free Software Foundation, Inc. |
9 | 4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7619
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
9 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7619
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7619
diff
changeset
|
8 (at your option) any later version. |
9 | 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 | |
19190 | 16 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
9 | 17 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
18 #include <config.h> |
399 | 19 |
17177 | 20 #define XALLOC_INLINE _GL_EXTERN_INLINE |
21 | |
4691 | 22 #include "xalloc.h" |
399 | 23 |
4691 | 24 #include <stdlib.h> |
4793
43c03cffd8aa
Include <string.h>, for declarations of memset and memcpy.
Jim Meyering <jim@meyering.net>
parents:
4783
diff
changeset
|
25 #include <string.h> |
9 | 26 |
5930
d3f94dc03257
* xmalloc.c (HAVE_GNU_CALLOC): New constant.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
27 /* 1 if calloc is known to be compatible with GNU calloc. This |
d3f94dc03257
* xmalloc.c (HAVE_GNU_CALLOC): New constant.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
28 matters if we are not also using the calloc module, which defines |
13606
81875db181b0
Make the module 'calloc-gnu' work again on AIX and OSF/1.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
29 HAVE_CALLOC_GNU and supports the GNU API even on non-GNU platforms. */ |
13885 | 30 #if defined HAVE_CALLOC_GNU || (defined __GLIBC__ && !defined __UCLIBC__) |
5930
d3f94dc03257
* xmalloc.c (HAVE_GNU_CALLOC): New constant.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
31 enum { HAVE_GNU_CALLOC = 1 }; |
d3f94dc03257
* xmalloc.c (HAVE_GNU_CALLOC): New constant.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
32 #else |
d3f94dc03257
* xmalloc.c (HAVE_GNU_CALLOC): New constant.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
33 enum { HAVE_GNU_CALLOC = 0 }; |
d3f94dc03257
* xmalloc.c (HAVE_GNU_CALLOC): New constant.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
34 #endif |
d3f94dc03257
* xmalloc.c (HAVE_GNU_CALLOC): New constant.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
35 |
9 | 36 /* Allocate N bytes of memory dynamically, with error checking. */ |
37 | |
1071 | 38 void * |
1557 | 39 xmalloc (size_t n) |
9 | 40 { |
7608
060ba558f95d
* lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
41 void *p = malloc (n); |
060ba558f95d
* lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
42 if (!p && n != 0) |
1956
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
43 xalloc_die (); |
9 | 44 return p; |
45 } | |
46 | |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
47 /* Change the size of an allocated block of memory P to N bytes, |
2814
bae17cdae026
(xalloc_msg_memory_exhausted): Now char const[],
Jim Meyering <jim@meyering.net>
parents:
2793
diff
changeset
|
48 with error checking. */ |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
49 |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
50 void * |
1557 | 51 xrealloc (void *p, size_t n) |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
52 { |
14472
17beee3610b7
xmalloc: revert yesterday's regression
Eric Blake <eblake@redhat.com>
parents:
14468
diff
changeset
|
53 if (!n && p) |
14468
393c747ad672
xmalloc: Do not leak if underlying realloc is C99 compatible.
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
54 { |
393c747ad672
xmalloc: Do not leak if underlying realloc is C99 compatible.
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
55 /* The GNU and C99 realloc behaviors disagree here. Act like |
393c747ad672
xmalloc: Do not leak if underlying realloc is C99 compatible.
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
56 GNU, even if the underlying realloc is C99. */ |
393c747ad672
xmalloc: Do not leak if underlying realloc is C99 compatible.
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
57 free (p); |
393c747ad672
xmalloc: Do not leak if underlying realloc is C99 compatible.
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
58 return NULL; |
393c747ad672
xmalloc: Do not leak if underlying realloc is C99 compatible.
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
59 } |
393c747ad672
xmalloc: Do not leak if underlying realloc is C99 compatible.
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
60 |
7608
060ba558f95d
* lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
61 p = realloc (p, n); |
14472
17beee3610b7
xmalloc: revert yesterday's regression
Eric Blake <eblake@redhat.com>
parents:
14468
diff
changeset
|
62 if (!p && n) |
7608
060ba558f95d
* lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
63 xalloc_die (); |
060ba558f95d
* lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
64 return p; |
4817
7c67f04e1c19
Add x2realloc, x2nrealloc. Port to C99 inline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4793
diff
changeset
|
65 } |
7c67f04e1c19
Add x2realloc, x2nrealloc. Port to C99 inline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4793
diff
changeset
|
66 |
7c67f04e1c19
Add x2realloc, x2nrealloc. Port to C99 inline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4793
diff
changeset
|
67 /* If P is null, allocate a block of at least *PN bytes; otherwise, |
7c67f04e1c19
Add x2realloc, x2nrealloc. Port to C99 inline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4793
diff
changeset
|
68 reallocate P so that it contains more than *PN bytes. *PN must be |
7c67f04e1c19
Add x2realloc, x2nrealloc. Port to C99 inline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4793
diff
changeset
|
69 nonzero unless P is null. Set *PN to the new block's size, and |
7c67f04e1c19
Add x2realloc, x2nrealloc. Port to C99 inline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4793
diff
changeset
|
70 return the pointer to the new block. *PN is never set to zero, and |
7c67f04e1c19
Add x2realloc, x2nrealloc. Port to C99 inline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4793
diff
changeset
|
71 the returned pointer is never null. */ |
7c67f04e1c19
Add x2realloc, x2nrealloc. Port to C99 inline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4793
diff
changeset
|
72 |
7c67f04e1c19
Add x2realloc, x2nrealloc. Port to C99 inline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4793
diff
changeset
|
73 void * |
7c67f04e1c19
Add x2realloc, x2nrealloc. Port to C99 inline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4793
diff
changeset
|
74 x2realloc (void *p, size_t *pn) |
7c67f04e1c19
Add x2realloc, x2nrealloc. Port to C99 inline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4793
diff
changeset
|
75 { |
7608
060ba558f95d
* lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc): Now static
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
76 return x2nrealloc (p, pn, 1); |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
77 } |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
78 |
4783
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
79 /* Allocate S bytes of zeroed memory dynamically, with error checking. |
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
80 There's no need for xnzalloc (N, S), since it would be equivalent |
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
81 to xcalloc (N, S). */ |
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
82 |
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
83 void * |
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
84 xzalloc (size_t s) |
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
85 { |
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
86 return memset (xmalloc (s), 0, s); |
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
87 } |
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
88 |
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
89 /* Allocate zeroed memory for N elements of S bytes, with error |
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
90 checking. S must be nonzero. */ |
739 | 91 |
1071 | 92 void * |
1557 | 93 xcalloc (size_t n, size_t s) |
739 | 94 { |
1071 | 95 void *p; |
18564
2c0ed230603d
xalloc: do not exceed PTRDIFF_MAX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
96 /* Test for overflow, since objects with size greater than |
2c0ed230603d
xalloc: do not exceed PTRDIFF_MAX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
97 PTRDIFF_MAX cause pointer subtraction to go awry. Omit size-zero |
2c0ed230603d
xalloc: do not exceed PTRDIFF_MAX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
98 tests if HAVE_GNU_CALLOC, since GNU calloc never returns NULL if |
2c0ed230603d
xalloc: do not exceed PTRDIFF_MAX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
99 successful. */ |
2c0ed230603d
xalloc: do not exceed PTRDIFF_MAX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
100 if (xalloc_oversized (n, s) |
5930
d3f94dc03257
* xmalloc.c (HAVE_GNU_CALLOC): New constant.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
101 || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0))) |
1956
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
102 xalloc_die (); |
739 | 103 return p; |
104 } | |
4783
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
105 |
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
106 /* Clone an object P of size S, with error checking. There's no need |
5321
fe390d57473a
Rename xclone to xmemdup. Remove obsolete xalloc macros.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
107 for xnmemdup (P, N, S), since xmemdup (P, N * S) works without any |
4783
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
108 need for an arithmetic overflow check. */ |
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
109 |
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
110 void * |
5321
fe390d57473a
Rename xclone to xmemdup. Remove obsolete xalloc macros.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
111 xmemdup (void const *p, size_t s) |
4783
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
112 { |
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
113 return memcpy (xmalloc (s), p, s); |
d1dc5d9bf1ba
Revamp xalloc interface so that it can check for address arithmetic overflow.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
114 } |
5321
fe390d57473a
Rename xclone to xmemdup. Remove obsolete xalloc macros.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
115 |
fe390d57473a
Rename xclone to xmemdup. Remove obsolete xalloc macros.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
116 /* Clone STRING. */ |
fe390d57473a
Rename xclone to xmemdup. Remove obsolete xalloc macros.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
117 |
fe390d57473a
Rename xclone to xmemdup. Remove obsolete xalloc macros.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
118 char * |
fe390d57473a
Rename xclone to xmemdup. Remove obsolete xalloc macros.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
119 xstrdup (char const *string) |
fe390d57473a
Rename xclone to xmemdup. Remove obsolete xalloc macros.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
120 { |
fe390d57473a
Rename xclone to xmemdup. Remove obsolete xalloc macros.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
121 return xmemdup (string, strlen (string) + 1); |
fe390d57473a
Rename xclone to xmemdup. Remove obsolete xalloc macros.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
122 } |