summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-28 00:44:42 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-28 00:44:42 -0600
commit06e1dd50e4c2e93150b4e14ee2cbed566900cd3a (patch)
tree978732f3df500cc16a59e4890717db1d28662081
parent95bf87d4ed037e6d6786cc395d94dbe44ee88e32 (diff)
downloadzash-06e1dd50e4c2e93150b4e14ee2cbed566900cd3a.tar.gz
fix(util): refuse an arena size that would overflow
arena_alloc() aligned the size and arena_add_chunk() added sizeof (arena_chunk) to it before malloc(), so a size near SIZE_MAX wrapped and handed back a short block. Both now go through out_of_memory() above SIZE_MAX / 2, the same way xmalloc() treats a failed malloc.
-rw-r--r--src/util.c4
1 files changed, 4 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
index c671d77..bdb5431 100644
--- a/src/util.c
+++ b/src/util.c
@@ -156,6 +156,8 @@ static void arena_drop_chunk(arena *a, arena_chunk *c) {
static void arena_add_chunk(arena *a, size_t need) {
size_t size = ARENA_CHUNK - sizeof(arena_chunk);
arena_chunk *c;
+ if (need > SIZE_MAX / 2) /* sizeof *c + size must not wrap */
+ out_of_memory();
if (need <= size && a->spare) {
c = a->spare;
a->spare = nullptr;
@@ -183,6 +185,8 @@ static void arena_add_chunk(arena *a, size_t need) {
* @return Uninitialised memory aligned for any object.
*/
void *arena_alloc(arena *a, size_t n) {
+ if (n > SIZE_MAX / 2) /* align_up() must not wrap */
+ out_of_memory();
n = align_up(n);
if ((size_t)(a->end - a->next) < n)
arena_add_chunk(a, n);