Blame


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