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