New module 'safe-alloc'.
[gnulib.git] / lib / safe-alloc.c
1 /*
2  * safe-alloc.c: safer memory allocation
3  *
4  * Copyright (C) 2009 Free Software Foundation, Inc.
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 /* Written by Daniel Berrange <berrange@redhat.com>, 2008 */
23
24 #include <config.h>
25
26 #include <stdlib.h>
27 #include <stddef.h>
28 #include <errno.h>
29
30 #include "safe-alloc.h"
31
32
33 /* Return 1 if an array of N objects, each of size S, cannot exist due
34    to size arithmetic overflow.  S must be positive and N must be
35    nonnegative.  This is a macro, not an inline function, so that it
36    works correctly even when SIZE_MAX < N.
37
38    By gnulib convention, SIZE_MAX represents overflow in size
39    calculations, so the conservative dividend to use here is
40    SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
41    However, malloc (SIZE_MAX) fails on all known hosts where
42    sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
43    exactly-SIZE_MAX allocations on such hosts; this avoids a test and
44    branch when S is known to be 1.
45
46    This is the same as xalloc_oversized from xalloc.h
47 */
48 #define safe_alloc_oversized(n, s)                                      \
49   ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
50
51
52 /**
53  * safe_alloc_alloc_n:
54  * @ptrptr: pointer to pointer for address of allocated memory
55  * @size: number of bytes to allocate
56  * @count: number of elements to allocate
57  *
58  * Allocate an array of memory 'count' elements long,
59  * each with 'size' bytes. Return the address of the
60  * allocated memory in 'ptrptr'.  The newly allocated
61  * memory is filled with zeros.
62  *
63  * Return -1 on failure to allocate, zero on success
64  */
65 int
66 safe_alloc_alloc_n (void *ptrptr, size_t size, size_t count, int zeroed)
67 {
68   if (size == 0 || count == 0)
69     {
70       *(void **) ptrptr = NULL;
71       return 0;
72     }
73
74   if (safe_alloc_oversized (count, size))
75     {
76       errno = ENOMEM;
77       return -1;
78     }
79
80   if (zeroed)
81     *(void **) ptrptr = calloc (count, size);
82   else
83     *(void **) ptrptr = malloc (count * size);
84
85   if (*(void **) ptrptr == NULL)
86     return -1;
87   return 0;
88 }
89
90 /**
91  * safe_alloc_realloc_n:
92  * @ptrptr: pointer to pointer for address of allocated memory
93  * @size: number of bytes to allocate
94  * @count: number of elements in array
95  *
96  * Resize the block of memory in 'ptrptr' to be an array of
97  * 'count' elements, each 'size' bytes in length. Update 'ptrptr'
98  * with the address of the newly allocated memory. On failure,
99  * 'ptrptr' is not changed and still points to the original memory
100  * block. The newly allocated memory is filled with zeros.
101  *
102  * Return -1 on failure to allocate, zero on success
103  */
104 int
105 safe_alloc_realloc_n (void *ptrptr, size_t size, size_t count)
106 {
107   void *tmp;
108   if (size == 0 || count == 0)
109     {
110       free (*(void **) ptrptr);
111       *(void **) ptrptr = NULL;
112       return 0;
113     }
114   if (safe_alloc_oversized (count, size))
115     {
116       errno = ENOMEM;
117       return -1;
118     }
119   tmp = realloc (*(void **) ptrptr, size * count);
120   if (!tmp)
121     return -1;
122   *(void **) ptrptr = tmp;
123   return 0;
124 }