From 4daae72c47a59e20f5312c1febb84e4c187e6acc Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 11 Nov 2023 19:31:56 +0100 Subject: [PATCH] eealloc: Take advantage of CHERI bounds-checking. * lib/eealloc.h: Include . (eemalloc): When n is 0, return a pointer whose bounds are of size 0, not 1. (eerealloc): Likewise. --- ChangeLog | 8 ++++++++ lib/eealloc.h | 23 +++++++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index b0f6f9f404..60aea347a3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2023-11-11 Bruno Haible + + eealloc: Take advantage of CHERI bounds-checking. + * lib/eealloc.h: Include . + (eemalloc): When n is 0, return a pointer whose bounds are of size 0, + not 1. + (eerealloc): Likewise. + 2023-11-11 Bruno Haible alignalloc: Take advantage of CHERI bounds-checking. diff --git a/lib/eealloc.h b/lib/eealloc.h index 6666f172c6..bae3915146 100644 --- a/lib/eealloc.h +++ b/lib/eealloc.h @@ -36,6 +36,9 @@ #endif #include +#if defined __CHERI__ +# include +#endif _GL_INLINE_HEADER_BEGIN #ifndef EEALLOC_INLINE @@ -52,9 +55,15 @@ EEALLOC_INLINE void * eemalloc (size_t n) { /* If n is zero, allocate a 1-byte block. */ + size_t nx = n; if (n == 0) - n = 1; - return malloc (n); + nx = 1; + void *ptr = malloc (nx); +# if defined __CHERI__ + if (ptr != NULL) + ptr = cheri_bounds_set (ptr, n); +# endif + return ptr; } #endif @@ -67,9 +76,15 @@ EEALLOC_INLINE void * eerealloc (void *p, size_t n) { /* If n is zero, allocate or keep a 1-byte block. */ + size_t nx = n; if (n == 0) - n = 1; - return realloc (p, n); + nx = 1; + void *ptr = realloc (p, nx); +# if defined __CHERI__ + if (ptr != NULL) + ptr = cheri_bounds_set (ptr, n); +# endif + return ptr; } #endif -- 2.39.5