Blame


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