xref: /illumos-gate/usr/src/lib/libc/port/gen/recallocarray.c (revision 4c28a617e3922d92a58e813a5b955eb526b9c386)
1 /*
2  * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <errno.h>
18 #include <stdlib.h>
19 #include <stdint.h>
20 #include <string.h>
21 #include <unistd.h>
22 
23 /*
24  * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
25  * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
26  */
27 #define	MUL_NO_OVERFLOW ((size_t)1 << (sizeof (size_t) * 4))
28 
29 void *
30 recallocarray(void *ptr, size_t oldnelem, size_t newnelem, size_t elsize)
31 {
32 	size_t oldsize, newsize;
33 	void *newptr;
34 
35 	if (ptr == NULL)
36 		return (calloc(newnelem, elsize));
37 
38 	if ((newnelem >= MUL_NO_OVERFLOW || elsize >= MUL_NO_OVERFLOW) &&
39 	    newnelem > 0 && SIZE_MAX / newnelem < elsize) {
40 		errno = ENOMEM;
41 		return (NULL);
42 	}
43 	newsize = newnelem * elsize;
44 
45 	if ((oldnelem >= MUL_NO_OVERFLOW || elsize >= MUL_NO_OVERFLOW) &&
46 	    oldnelem > 0 && SIZE_MAX / oldnelem < elsize) {
47 		errno = EINVAL;
48 		return (NULL);
49 	}
50 	oldsize = oldnelem * elsize;
51 
52 	/*
53 	 * Don't bother too much if we're shrinking just a bit,
54 	 * we do not shrink for series of small steps, oh well.
55 	 */
56 	if (newsize <= oldsize) {
57 		size_t d = oldsize - newsize;
58 
59 		if (d < oldsize / 2 && d < getpagesize()) {
60 			(void) memset((char *)ptr + newsize, 0, d);
61 			return (ptr);
62 		}
63 	}
64 
65 	newptr = malloc(newsize);
66 	if (newptr == NULL)
67 		return (NULL);
68 
69 	if (newsize > oldsize) {
70 		(void) memcpy(newptr, ptr, oldsize);
71 		(void) memset((char *)newptr + oldsize, 0, newsize - oldsize);
72 	} else {
73 		(void) memcpy(newptr, ptr, newsize);
74 	}
75 
76 	explicit_bzero(ptr, oldsize);
77 	free(ptr);
78 
79 	return (newptr);
80 }
81