summaryrefslogtreecommitdiffstats
path: root/gl/lib/ialloc.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 19:38:57 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 19:38:57 +0000
commitf5b6b735a731901f09d7f3cc153c1d869269ee83 (patch)
tree565a1b0f3c6a4094a5f2198879fb239053549f1e /gl/lib/ialloc.h
parentAdding upstream version 2.12.0. (diff)
downloadman-db-4f7a434e7e8e6c9e616306e3c452a812bc4b6019.tar.xz
man-db-4f7a434e7e8e6c9e616306e3c452a812bc4b6019.zip
Adding upstream version 2.12.1.upstream/2.12.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gl/lib/ialloc.h')
-rw-r--r--gl/lib/ialloc.h45
1 files changed, 34 insertions, 11 deletions
diff --git a/gl/lib/ialloc.h b/gl/lib/ialloc.h
index 22f57a4..2aa94ae 100644
--- a/gl/lib/ialloc.h
+++ b/gl/lib/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
@@ -29,6 +29,9 @@
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
+#if defined __CHERI_PURE_CAPABILITY__
+# include <cheri.h>
+#endif
_GL_INLINE_HEADER_BEGIN
#ifndef IALLOC_INLINE
@@ -65,9 +68,19 @@ IALLOC_INLINE
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).
@@ -99,13 +112,23 @@ icalloc (idx_t n, idx_t s)
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