From 5be04ec2ceb1df77afbca4fcbf9e92a712612d6f Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 31 Oct 2024 16:27:12 +0100 Subject: Sync with the latest Gnulib code (d4ec02b3cc) --- gl/ialloc.h | 68 ++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 14 deletions(-) (limited to 'gl/ialloc.h') diff --git a/gl/ialloc.h b/gl/ialloc.h index 1d43faf3..2aa94ae7 100644 --- a/gl/ialloc.h +++ b/gl/ialloc.h @@ -1,6 +1,6 @@ /* ialloc.h -- malloc with idx_t rather than size_t - Copyright 2021-2023 Free Software Foundation, Inc. + Copyright 2021-2024 Free Software Foundation, Inc. This file is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -18,15 +18,21 @@ #ifndef IALLOC_H_ #define IALLOC_H_ +/* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE, _GL_ATTRIBUTE_COLD, + _GL_ATTRIBUTE_MALLOC. */ +#if !_GL_CONFIG_H_INCLUDED + #error "Please include config.h first." +#endif + #include "idx.h" #include #include #include - -#ifndef _GL_INLINE_HEADER_BEGIN - #error "Please include config.h first." +#if defined __CHERI_PURE_CAPABILITY__ +# include #endif + _GL_INLINE_HEADER_BEGIN #ifndef IALLOC_INLINE # define IALLOC_INLINE _GL_INLINE @@ -43,6 +49,9 @@ _gl_alloc_nomem (void) return NULL; } +/* imalloc (size) is like malloc (size). + It returns a non-NULL pointer to size bytes of memory. + Upon failure, it returns NULL with errno set. */ IALLOC_INLINE _GL_ATTRIBUTE_MALLOC /*_GL_ATTRIBUTE_DEALLOC_FREE*/ void * @@ -51,16 +60,32 @@ imalloc (idx_t s) return s <= SIZE_MAX ? malloc (s) : _gl_alloc_nomem (); } +/* irealloc (ptr, size) is like realloc (ptr, size). + It returns a non-NULL pointer to size bytes of memory. + Upon failure, it returns NULL with errno set. */ IALLOC_INLINE /*_GL_ATTRIBUTE_DEALLOC_FREE*/ void * irealloc (void *p, idx_t s) { - /* Work around GNU realloc glitch by treating a zero size as if it - were 1, so that returning NULL is equivalent to failing. */ - return s <= SIZE_MAX ? realloc (p, s | !s) : _gl_alloc_nomem (); + if (s <= SIZE_MAX) + { + /* Work around GNU realloc glitch by treating a zero size as if it + were 1, so that returning NULL is equivalent to failing. */ + p = realloc (p, s | !s); +#if defined __CHERI_PURE_CAPABILITY__ + if (p != NULL) + p = cheri_bounds_set (p, s); +#endif + return p; + } + else + return _gl_alloc_nomem (); } +/* icalloc (num, size) is like calloc (num, size). + It returns a non-NULL pointer to num * size bytes of memory. + Upon failure, it returns NULL with errno set. */ IALLOC_INLINE _GL_ATTRIBUTE_MALLOC /*_GL_ATTRIBUTE_DEALLOC_FREE*/ void * @@ -81,20 +106,35 @@ icalloc (idx_t n, idx_t s) return calloc (n, s); } +/* ireallocarray (ptr, num, size) is like reallocarray (ptr, num, size). + It returns a non-NULL pointer to num * size bytes of memory. + Upon failure, it returns NULL with errno set. */ IALLOC_INLINE void * ireallocarray (void *p, idx_t n, idx_t s) { - /* Work around GNU reallocarray glitch by treating a zero size as if - it were 1, so that returning NULL is equivalent to failing. */ - if (n == 0 || s == 0) - n = s = 1; - return (n <= SIZE_MAX && s <= SIZE_MAX - ? reallocarray (p, n, s) - : _gl_alloc_nomem ()); + if (n <= SIZE_MAX && s <= SIZE_MAX) + { + /* Work around GNU reallocarray glitch by treating a zero size as if + it were 1, so that returning NULL is equivalent to failing. */ + size_t nx = n; + size_t sx = s; + if (n == 0 || s == 0) + nx = sx = 1; + p = reallocarray (p, nx, sx); +#if defined __CHERI_PURE_CAPABILITY__ + if (p != NULL && (n == 0 || s == 0)) + p = cheri_bounds_set (p, 0); +#endif + return p; + } + else + return _gl_alloc_nomem (); } #ifdef __cplusplus } #endif +_GL_INLINE_HEADER_END + #endif -- cgit v1.2.3-74-g34f1