diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:44:42 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:44:42 -0600 |
| commit | 06e1dd50e4c2e93150b4e14ee2cbed566900cd3a (patch) | |
| tree | 978732f3df500cc16a59e4890717db1d28662081 | |
| parent | 95bf87d4ed037e6d6786cc395d94dbe44ee88e32 (diff) | |
| download | zash-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.c | 4 |
1 files changed, 4 insertions, 0 deletions
@@ -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); |
