Blame


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