diff --git a/libc/test/IntegrationTest/test.cpp b/libc/test/IntegrationTest/test.cpp --- a/libc/test/IntegrationTest/test.cpp +++ b/libc/test/IntegrationTest/test.cpp @@ -57,7 +57,8 @@ // which just hands out continuous blocks from a statically allocated chunk of // memory. -static uint8_t memory[16384]; +static constexpr uint64_t MEMORY_SIZE = 16384; +static uint8_t memory[MEMORY_SIZE]; static uint8_t *ptr = memory; extern "C" { @@ -65,7 +66,7 @@ void *malloc(size_t s) { void *mem = ptr; ptr += s; - return mem; + return static_cast(ptr - memory) >= MEMORY_SIZE ? nullptr : mem; } void free(void *) {} diff --git a/libc/test/UnitTest/HermeticTestUtils.cpp b/libc/test/UnitTest/HermeticTestUtils.cpp --- a/libc/test/UnitTest/HermeticTestUtils.cpp +++ b/libc/test/UnitTest/HermeticTestUtils.cpp @@ -29,7 +29,8 @@ // requires. Hence, as a work around for this problem, we use a simple allocator // which just hands out continuous blocks from a statically allocated chunk of // memory. -static uint8_t memory[16384]; +static constexpr uint64_t MEMORY_SIZE = 16384; +static uint8_t memory[MEMORY_SIZE]; static uint8_t *ptr = memory; } // anonymous namespace @@ -68,7 +69,7 @@ s = ((s + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT; void *mem = ptr; ptr += s; - return mem; + return static_cast(ptr - memory) >= MEMORY_SIZE ? nullptr : mem; } void free(void *) {}