lib/libc: add recallocarray()

This function from OpenBSD is a hybrid of reallocarray() and calloc().
It reallocates an array, clearing any newly allocated items.
reallocarray() ultimately originates from OpenBSD.

The source is taken from lib/libopenbsd, which now no longer has the
function unless when bootstrapping (needed for mandoc).

Reviewed by:	kib
Differential Revision:	https://reviews.freebsd.org/D52863
This commit is contained in:
Robert Clausecker 2025-10-02 16:33:55 +02:00
parent 969c119195
commit 4266461079
6 changed files with 50 additions and 4 deletions

View file

@ -314,6 +314,8 @@ int radixsort(const unsigned char **, int, const unsigned char *,
unsigned);
void *reallocarray(void *, size_t, size_t) __result_use_check
__alloc_size2(2, 3);
void *recallocarray(void *, size_t, size_t, size_t) __result_use_check
__alloc_size2(3, 4);
void *reallocf(void *, size_t) __result_use_check __alloc_size(2);
int rpmatch(const char *);
char *secure_getenv(const char *);

View file

@ -10,7 +10,7 @@ MISRCS+=C99_Exit.c a64l.c abort.c abs.c atexit.c atof.c atoi.c atol.c atoll.c \
insque.c l64a.c labs.c ldiv.c llabs.c lldiv.c lsearch.c \
merge.c mergesort_b.c ptsname.c qsort.c qsort_r.c qsort_r_compat.c \
qsort_s.c quick_exit.c radixsort.c rand.c \
random.c reallocarray.c reallocf.c realpath.c remque.c \
random.c reallocarray.c reallocf.c realpath.c recallocarray.c remque.c \
set_constraint_handler_s.c strfmon.c strtoimax.c \
strtol.c strtold.c strtoll.c strtoq.c strtoul.c strtonum.c strtoull.c \
strtoumax.c strtouq.c system.c tdelete.c tfind.c tsearch.c twalk.c
@ -76,6 +76,7 @@ MLINKS+=random.3 initstate.3 \
random.3 srandom.3 \
random.3 srandomdev.3
MLINKS+=radixsort.3 sradixsort.3
MLINKS+=reallocarray.3 recallocarray.3
MLINKS+=set_constraint_handler_s.3 abort_handler_s.3
MLINKS+=set_constraint_handler_s.3 ignore_handler_s.3
MLINKS+=strfmon.3 strfmon_l.3

View file

@ -131,6 +131,10 @@ FBSD_1.8 {
getenv_r;
};
FBSD_1.9 {
recallocarray;
};
FBSDprivate_1.0 {
__system;
_system;

View file

@ -26,7 +26,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd May 1, 2015
.Dd October 2, 2025
.Dt REALLOCARRAY 3
.Os
.Sh NAME
@ -38,6 +38,8 @@
.In stdlib.h
.Ft void *
.Fn reallocarray "void *ptr" "size_t nmemb" "size_t size"
.Ft void *
.Fn recallocarray "void *ptr" "size_t oldnmeb" "size_t nmemb" size_t size"
.Sh DESCRIPTION
The
.Fn reallocarray
@ -52,6 +54,33 @@ and checks for integer overflow in the calculation
.Fa nmemb
*
.Fa size .
.Pp
The
.Fn recallocarray
function is similar to the
.Fn reallocarray
function
except it ensures newly allocated memory is cleared similar to
.Fn calloc .
If
.Fa ptr
is
.Dv NULL ,
.Fa oldnmemb
is ignored and the call is equivalent to
.Fn calloc .
If
.Fa ptr
is not
.Dv NULL ,
.Fa oldnmemb
must be a value such that
.Fa oldnmemb
*
.Fa size
is the size of the earlier allocation that returned
.Fa ptr ,
otherwise the behaviour is undefined.
.Sh RETURN VALUES
The
.Fn reallocarray
@ -142,3 +171,9 @@ function first appeared in
.Ox 5.6
and
.Fx 11.0 .
The
.Fn recallocarray
function first appeared in
.Ox 6.1
and
.Fx 16.0 .

View file

@ -1,8 +1,12 @@
LIB= openbsd
SRCS= imsg-buffer.c \
imsg.c \
ohash.c \
recallocarray.c
ohash.c
.if defined(BOOTSTRAPPING)
.PATH: ${SRCTOP}/lib/libc/stdlib
SRCS+= recallocarray.c
.endif
.if !defined(BOOTSTRAPPING)
# Skip getdtablecount.c when bootstrapping since it doesn't compile for Linux
# and is not used by any of the bootstrap tools