Blame


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