From 2d9d3ddae5dde383bac8f133f0551ce511f05f2d Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 11 Nov 2023 19:36:36 +0100 Subject: [PATCH] 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. --- ChangeLog | 7 +++++++ lib/safe-alloc.h | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) 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) -- 2.39.5