From 30179dd32617ca4fbdd5aab7ba782b2474498a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Tue, 29 Oct 2024 18:12:22 +0100 Subject: [PATCH] libroot: add reallocarray() from POSIX.1-2024 Change-Id: I75d469ad9a01eb221ca607830589a7794eff71e8 Reviewed-on: https://review.haiku-os.org/c/haiku/+/8512 Reviewed-by: waddlesplash Tested-by: Commit checker robot --- headers/posix/stdlib.h | 1 + src/system/libroot/posix/stdlib/Jamfile | 1 + .../libroot/posix/stdlib/reallocarray.cpp | 21 +++++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 src/system/libroot/posix/stdlib/reallocarray.cpp diff --git a/headers/posix/stdlib.h b/headers/posix/stdlib.h index 5f5d586085..51a965f17a 100644 --- a/headers/posix/stdlib.h +++ b/headers/posix/stdlib.h @@ -51,6 +51,7 @@ extern void *malloc(size_t size); extern int posix_memalign(void **_pointer, size_t alignment, size_t size); extern void *aligned_alloc(size_t alignment, size_t size) _ALIGNED_BY_ARG(1); extern void *realloc(void *oldPointer, size_t newSize); +extern void *reallocarray(void *ptr, size_t nelem, size_t elsize); /* process termination */ extern void abort(void) __attribute__((noreturn)); diff --git a/src/system/libroot/posix/stdlib/Jamfile b/src/system/libroot/posix/stdlib/Jamfile index 6ba1421e7f..77aa08c154 100644 --- a/src/system/libroot/posix/stdlib/Jamfile +++ b/src/system/libroot/posix/stdlib/Jamfile @@ -28,6 +28,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { qsort.c radixsort.c random.c + reallocarray.cpp realpath.cpp strfmon.c strtol.c diff --git a/src/system/libroot/posix/stdlib/reallocarray.cpp b/src/system/libroot/posix/stdlib/reallocarray.cpp new file mode 100644 index 0000000000..4fb44ae5af --- /dev/null +++ b/src/system/libroot/posix/stdlib/reallocarray.cpp @@ -0,0 +1,21 @@ +/* + * Copyright 2024, Jérôme Duval, jerome.duval@gmail.com + * Distributed under the terms of the MIT License. + */ + + +#include + +#include +#include + + +extern "C" void* +reallocarray(void* ptr, size_t nelem, size_t elsize) +{ + if (elsize != 0 && nelem != 0 && nelem > SIZE_MAX / elsize) { + errno = ENOMEM; + return NULL; + } + return realloc(ptr, nelem * elsize); +}