annotate lib/safe-alloc.c @ 16040:b8acd8099b25

New module 'fmaf'. * lib/math.in.h (fmaf): New declaration. * lib/fmaf.c: New file. * m4/fmaf.m4: New file. * m4/math_h.m4 (gl_MATH_H): Test whethern fmaf is declared. (gl_MATH_H_DEFAULTS): Initialize GNULIB_FMAF, HAVE_FMAF, REPLACE_FMAF. * modules/math (Makefile.am): Substitute GNULIB_FMAF, HAVE_FMAF, REPLACE_FMAF. * modules/fmaf: New file. * doc/posix-functions/fmaf.texi: Mention the new module and the various bugs.
author Bruno Haible <bruno@clisp.org>
date Mon, 17 Oct 2011 23:51:21 +0200 (2011-10-17)
parents 97fc9a21a8fb
children 8250f2777afc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12023
e1d31e5feef2 Use the standard header with LGPL copyright.
Bruno Haible <bruno@clisp.org>
parents: 11204
diff changeset
1 /* safe-alloc.c: safer memory allocation
e1d31e5feef2 Use the standard header with LGPL copyright.
Bruno Haible <bruno@clisp.org>
parents: 11204
diff changeset
2
14079
97fc9a21a8fb maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents: 12559
diff changeset
3 Copyright (C) 2009-2011 Free Software Foundation, Inc.
12023
e1d31e5feef2 Use the standard header with LGPL copyright.
Bruno Haible <bruno@clisp.org>
parents: 11204
diff changeset
4
e1d31e5feef2 Use the standard header with LGPL copyright.
Bruno Haible <bruno@clisp.org>
parents: 11204
diff changeset
5 This program is free software: you can redistribute it and/or modify it
12024
200a0145c440 Use the standard header with GPL copyright.
Bruno Haible <bruno@clisp.org>
parents: 12023
diff changeset
6 under the terms of the GNU General Public License as published by the
200a0145c440 Use the standard header with GPL copyright.
Bruno Haible <bruno@clisp.org>
parents: 12023
diff changeset
7 Free Software Foundation; either version 3 of the License, or any
200a0145c440 Use the standard header with GPL copyright.
Bruno Haible <bruno@clisp.org>
parents: 12023
diff changeset
8 later version.
12023
e1d31e5feef2 Use the standard header with LGPL copyright.
Bruno Haible <bruno@clisp.org>
parents: 11204
diff changeset
9
e1d31e5feef2 Use the standard header with LGPL copyright.
Bruno Haible <bruno@clisp.org>
parents: 11204
diff changeset
10 This program is distributed in the hope that it will be useful,
e1d31e5feef2 Use the standard header with LGPL copyright.
Bruno Haible <bruno@clisp.org>
parents: 11204
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12024
200a0145c440 Use the standard header with GPL copyright.
Bruno Haible <bruno@clisp.org>
parents: 12023
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
200a0145c440 Use the standard header with GPL copyright.
Bruno Haible <bruno@clisp.org>
parents: 12023
diff changeset
13 GNU General Public License for more details.
12023
e1d31e5feef2 Use the standard header with LGPL copyright.
Bruno Haible <bruno@clisp.org>
parents: 11204
diff changeset
14
12024
200a0145c440 Use the standard header with GPL copyright.
Bruno Haible <bruno@clisp.org>
parents: 12023
diff changeset
15 You should have received a copy of the GNU General Public License
12023
e1d31e5feef2 Use the standard header with LGPL copyright.
Bruno Haible <bruno@clisp.org>
parents: 11204
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
11173
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
17
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
18 /* Written by Daniel Berrange <berrange@redhat.com>, 2008 */
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
19
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
20 #include <config.h>
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
21
11204
55b4123a6159 Cosmetic tweaks in the safe-alloc module.
Bruno Haible <bruno@clisp.org>
parents: 11173
diff changeset
22 /* Specification. */
55b4123a6159 Cosmetic tweaks in the safe-alloc module.
Bruno Haible <bruno@clisp.org>
parents: 11173
diff changeset
23 #include "safe-alloc.h"
55b4123a6159 Cosmetic tweaks in the safe-alloc module.
Bruno Haible <bruno@clisp.org>
parents: 11173
diff changeset
24
11173
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
25 #include <stdlib.h>
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
26 #include <stddef.h>
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
27 #include <errno.h>
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
28
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
29
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
30 /* Return 1 if an array of N objects, each of size S, cannot exist due
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
31 to size arithmetic overflow. S must be positive and N must be
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
32 nonnegative. This is a macro, not an inline function, so that it
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
33 works correctly even when SIZE_MAX < N.
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
34
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
35 By gnulib convention, SIZE_MAX represents overflow in size
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
36 calculations, so the conservative dividend to use here is
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
37 SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
38 However, malloc (SIZE_MAX) fails on all known hosts where
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
39 sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
40 exactly-SIZE_MAX allocations on such hosts; this avoids a test and
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
41 branch when S is known to be 1.
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
42
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
43 This is the same as xalloc_oversized from xalloc.h
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
44 */
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
45 #define safe_alloc_oversized(n, s) \
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
46 ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
47
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
48
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
49 /**
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
50 * safe_alloc_alloc_n:
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
51 * @ptrptr: pointer to pointer for address of allocated memory
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
52 * @size: number of bytes to allocate
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
53 * @count: number of elements to allocate
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
54 *
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
55 * Allocate an array of memory 'count' elements long,
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
56 * each with 'size' bytes. Return the address of the
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
57 * allocated memory in 'ptrptr'. The newly allocated
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
58 * memory is filled with zeros.
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
59 *
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
60 * Return -1 on failure to allocate, zero on success
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
61 */
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
62 int
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
63 safe_alloc_alloc_n (void *ptrptr, size_t size, size_t count, int zeroed)
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
64 {
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
65 if (size == 0 || count == 0)
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
66 {
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
67 *(void **) ptrptr = NULL;
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
68 return 0;
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
69 }
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
70
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
71 if (safe_alloc_oversized (count, size))
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
72 {
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
73 errno = ENOMEM;
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
74 return -1;
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
75 }
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
76
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
77 if (zeroed)
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
78 *(void **) ptrptr = calloc (count, size);
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
79 else
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
80 *(void **) ptrptr = malloc (count * size);
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
81
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
82 if (*(void **) ptrptr == NULL)
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
83 return -1;
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
84 return 0;
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
85 }
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
86
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
87 /**
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
88 * safe_alloc_realloc_n:
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
89 * @ptrptr: pointer to pointer for address of allocated memory
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
90 * @size: number of bytes to allocate
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
91 * @count: number of elements in array
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
92 *
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
93 * Resize the block of memory in 'ptrptr' to be an array of
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
94 * 'count' elements, each 'size' bytes in length. Update 'ptrptr'
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
95 * with the address of the newly allocated memory. On failure,
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
96 * 'ptrptr' is not changed and still points to the original memory
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
97 * block. The newly allocated memory is filled with zeros.
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
98 *
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
99 * Return -1 on failure to allocate, zero on success
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
100 */
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
101 int
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
102 safe_alloc_realloc_n (void *ptrptr, size_t size, size_t count)
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
103 {
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
104 void *tmp;
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
105 if (size == 0 || count == 0)
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
106 {
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
107 free (*(void **) ptrptr);
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
108 *(void **) ptrptr = NULL;
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
109 return 0;
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
110 }
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
111 if (safe_alloc_oversized (count, size))
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
112 {
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
113 errno = ENOMEM;
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
114 return -1;
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
115 }
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
116 tmp = realloc (*(void **) ptrptr, size * count);
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
117 if (!tmp)
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
118 return -1;
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
119 *(void **) ptrptr = tmp;
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
120 return 0;
af090b4101b4 New module 'safe-alloc'.
David Lutterkort <lutter@redhat.com>
parents:
diff changeset
121 }