Blame


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