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