2 * safe-alloc.c: safer memory allocation
4 * Copyright (C) 2009 Free Software Foundation, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* Written by Daniel Berrange <berrange@redhat.com>, 2008 */
27 #include "safe-alloc.h"
34 /* Return 1 if an array of N objects, each of size S, cannot exist due
35 to size arithmetic overflow. S must be positive and N must be
36 nonnegative. This is a macro, not an inline function, so that it
37 works correctly even when SIZE_MAX < N.
39 By gnulib convention, SIZE_MAX represents overflow in size
40 calculations, so the conservative dividend to use here is
41 SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
42 However, malloc (SIZE_MAX) fails on all known hosts where
43 sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
44 exactly-SIZE_MAX allocations on such hosts; this avoids a test and
45 branch when S is known to be 1.
47 This is the same as xalloc_oversized from xalloc.h
49 #define safe_alloc_oversized(n, s) \
50 ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
55 * @ptrptr: pointer to pointer for address of allocated memory
56 * @size: number of bytes to allocate
57 * @count: number of elements to allocate
59 * Allocate an array of memory 'count' elements long,
60 * each with 'size' bytes. Return the address of the
61 * allocated memory in 'ptrptr'. The newly allocated
62 * memory is filled with zeros.
64 * Return -1 on failure to allocate, zero on success
67 safe_alloc_alloc_n (void *ptrptr, size_t size, size_t count, int zeroed)
69 if (size == 0 || count == 0)
71 *(void **) ptrptr = NULL;
75 if (safe_alloc_oversized (count, size))
82 *(void **) ptrptr = calloc (count, size);
84 *(void **) ptrptr = malloc (count * size);
86 if (*(void **) ptrptr == NULL)
92 * safe_alloc_realloc_n:
93 * @ptrptr: pointer to pointer for address of allocated memory
94 * @size: number of bytes to allocate
95 * @count: number of elements in array
97 * Resize the block of memory in 'ptrptr' to be an array of
98 * 'count' elements, each 'size' bytes in length. Update 'ptrptr'
99 * with the address of the newly allocated memory. On failure,
100 * 'ptrptr' is not changed and still points to the original memory
101 * block. The newly allocated memory is filled with zeros.
103 * Return -1 on failure to allocate, zero on success
106 safe_alloc_realloc_n (void *ptrptr, size_t size, size_t count)
109 if (size == 0 || count == 0)
111 free (*(void **) ptrptr);
112 *(void **) ptrptr = NULL;
115 if (safe_alloc_oversized (count, size))
120 tmp = realloc (*(void **) ptrptr, size * count);
123 *(void **) ptrptr = tmp;