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