| 1 |
/* realloc() function that is glibc compatible. |
| 2 |
|
| 3 |
Copyright (C) 1997, 2003-2004, 2006-2007, 2009-2024 Free Software |
| 4 |
Foundation, Inc. |
| 5 |
|
| 6 |
This file is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU Lesser General Public License as |
| 8 |
published by the Free Software Foundation; either version 2.1 of the |
| 9 |
License, or (at your option) any later version. |
| 10 |
|
| 11 |
This file is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU Lesser General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Lesser General Public License |
| 17 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 18 |
|
| 19 |
/* written by Jim Meyering and Bruno Haible */ |
| 20 |
|
| 21 |
#define _GL_USE_STDLIB_ALLOC 1 |
| 22 |
#include <config.h> |
| 23 |
|
| 24 |
#include <stdlib.h> |
| 25 |
|
| 26 |
#include <errno.h> |
| 27 |
#include <stdckdint.h> |
| 28 |
|
| 29 |
#ifdef __CHERI_PURE_CAPABILITY__ |
| 30 |
# include <cheri.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
#ifndef _GL_INLINE_RPL_REALLOC |
| 34 |
|
| 35 |
/* Change the size of an allocated block of memory P to N bytes, |
| 36 |
with error checking. If P is NULL, use malloc. Otherwise if N is zero, |
| 37 |
free P and return NULL. */ |
| 38 |
|
| 39 |
void * |
| 40 |
rpl_realloc (void *p, size_t n) |
| 41 |
{ |
| 42 |
size_t n1 = n; |
| 43 |
|
| 44 |
if (n == 0) |
| 45 |
{ |
| 46 |
# if NEED_SANITIZED_REALLOC |
| 47 |
/* When P is non-null, ISO C23 ยง7.24.3.7.(3) says realloc (P, 0) has |
| 48 |
undefined behavior even though C17 and earlier partially defined |
| 49 |
the behavior. Let the programmer know. |
| 50 |
When the undefined-behaviour sanitizers report this case, i.e. when |
| 51 |
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117233> and |
| 52 |
<https://github.com/llvm/llvm-project/issues/113065> |
| 53 |
have been closed and new releases of GCC and clang have been made, |
| 54 |
we can revisit this code. */ |
| 55 |
if (p != NULL) |
| 56 |
abort (); |
| 57 |
# endif |
| 58 |
|
| 59 |
/* realloc (NULL, 0) acts like glibc malloc (0), i.e., like malloc (1) |
| 60 |
except the caller cannot dereference any non-null return. |
| 61 |
|
| 62 |
realloc (P, 0) with non-null P is a messier situation. |
| 63 |
As mentioned above, C23 says behavior is undefined. |
| 64 |
POSIX.1-2024 extends C17 to say realloc (P, 0) |
| 65 |
either fails by setting errno and returning a null pointer, |
| 66 |
or succeeds by freeing P and then either: |
| 67 |
(a) setting errno=EINVAL and returning a null pointer; or |
| 68 |
(b) acting like a successful malloc (0). |
| 69 |
glibc 1 through 2.1 realloc acted like (b), |
| 70 |
which conforms to C17, to C23 and to POSIX.1-2024. |
| 71 |
glibc 2.1.1+ realloc acts like (a) except it does not set errno; |
| 72 |
this conforms to C17 and to C23 but not to POSIX.1-2024. |
| 73 |
Quite possibly future versions of POSIX will change, |
| 74 |
due either to C23 or to (a)'s semantics being messy. |
| 75 |
Act like (b), as that's easy, matches GNU, BSD and V7 malloc, |
| 76 |
matches BSD and V7 realloc, and requires no extra code at |
| 77 |
caller sites. */ |
| 78 |
|
| 79 |
# if !HAVE_REALLOC_0_NONNULL |
| 80 |
n1 = 1; |
| 81 |
# endif |
| 82 |
} |
| 83 |
|
| 84 |
# if !HAVE_MALLOC_PTRDIFF |
| 85 |
ptrdiff_t signed_n; |
| 86 |
if (ckd_add (&signed_n, n, 0)) |
| 87 |
{ |
| 88 |
errno = ENOMEM; |
| 89 |
return NULL; |
| 90 |
} |
| 91 |
# endif |
| 92 |
|
| 93 |
void *result = realloc (p, n1); |
| 94 |
|
| 95 |
# if !HAVE_MALLOC_POSIX |
| 96 |
if (result == NULL) |
| 97 |
errno = ENOMEM; |
| 98 |
# endif |
| 99 |
|
| 100 |
# ifdef __CHERI_PURE_CAPABILITY__ |
| 101 |
if (result != NULL) |
| 102 |
result = cheri_bounds_set (result, n); |
| 103 |
# endif |
| 104 |
|
| 105 |
return result; |
| 106 |
} |
| 107 |
|
| 108 |
#endif |