From: Bruno Haible Date: Sat, 11 Nov 2023 18:36:36 +0000 (+0100) Subject: safe-alloc: Take advantage of CHERI bounds-checking. X-Git-Tag: v1.0~599 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2d9d3ddae5dde383bac8f133f0551ce511f05f2d;p=thirdparty%2Fgnulib.git safe-alloc: Take advantage of CHERI bounds-checking. * lib/safe-alloc.h: Include . (safe_alloc_realloc_n): When count or size is 0, return a pointer whose bounds are of size 0, not 1. --- diff --git a/ChangeLog b/ChangeLog index c166fae715..1fd39a4a44 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2023-11-11 Bruno Haible + + safe-alloc: Take advantage of CHERI bounds-checking. + * lib/safe-alloc.h: Include . + (safe_alloc_realloc_n): When count or size is 0, return a pointer whose + bounds are of size 0, not 1. + 2023-11-11 Bruno Haible ialloc: Take advantage of CHERI bounds-checking. diff --git a/lib/safe-alloc.h b/lib/safe-alloc.h index 46079d5c69..27049d3836 100644 --- a/lib/safe-alloc.h +++ b/lib/safe-alloc.h @@ -27,6 +27,9 @@ #endif #include +#if defined __CHERI__ +# include +#endif _GL_INLINE_HEADER_BEGIN #ifndef SAFE_ALLOC_INLINE @@ -37,9 +40,16 @@ _GL_INLINE_HEADER_BEGIN SAFE_ALLOC_INLINE void * safe_alloc_realloc_n (void *ptr, size_t count, size_t size) { + size_t countx = count; + size_t sizex = size; if (count == 0 || size == 0) - count = size = 1; - return reallocarray (ptr, count, size); + countx = sizex = 1; + ptr = reallocarray (ptr, countx, sizex); +#if defined __CHERI__ + if (ptr != NULL && (count == 0 || size == 0)) + ptr = cheri_bounds_set (ptr, 0); +#endif + return ptr; } _GL_ATTRIBUTE_NODISCARD SAFE_ALLOC_INLINE int safe_alloc_check (void *ptr)