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