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