1 cb6053ee 2021-05-11 martijn /* $OpenBSD: recallocarray.c,v 1.1 2017/03/06 18:44:21 otto Exp $ */
3 cb6053ee 2021-05-11 martijn * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
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.
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.
18 cb6053ee 2021-05-11 martijn /* OPENBSD ORIGINAL: lib/libc/stdlib/recallocarray.c */
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>
26 cb6053ee 2021-05-11 martijn #include "openbsd-compat.h"
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
32 cb6053ee 2021-05-11 martijn #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
35 cb6053ee 2021-05-11 martijn recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
37 cb6053ee 2021-05-11 martijn size_t oldsize, newsize;
38 cb6053ee 2021-05-11 martijn void *newptr;
40 cb6053ee 2021-05-11 martijn if (ptr == NULL)
41 cb6053ee 2021-05-11 martijn return calloc(newnmemb, size);
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;
48 cb6053ee 2021-05-11 martijn newsize = newnmemb * size;
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;
55 cb6053ee 2021-05-11 martijn oldsize = oldnmemb * size;
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.
61 cb6053ee 2021-05-11 martijn if (newsize <= oldsize) {
62 cb6053ee 2021-05-11 martijn size_t d = oldsize - newsize;
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;
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;
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);
78 cb6053ee 2021-05-11 martijn memcpy(newptr, ptr, newsize);
80 cb6053ee 2021-05-11 martijn explicit_bzero(ptr, oldsize);
81 cb6053ee 2021-05-11 martijn free(ptr);
83 cb6053ee 2021-05-11 martijn return newptr;