From c4a54377e3f07bbd2f9c54c1962c7941792c2c1f Mon Sep 17 00:00:00 2001 From: Marc Tiehuis Date: Wed, 20 Sep 2017 01:47:41 +1200 Subject: [PATCH] Stop debug allocator ever panicking (#492) --- std/debug.zig | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/std/debug.zig b/std/debug.zig index 0b80f4b104..08cc783b95 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -957,12 +957,17 @@ pub var global_allocator = mem.Allocator { var some_mem: [100 * 1024]u8 = undefined; var some_mem_index: usize = 0; +error OutOfMemory; + fn globalAlloc(self: &mem.Allocator, n: usize, alignment: usize) -> %[]u8 { const addr = @ptrToInt(&some_mem[some_mem_index]); const rem = @rem(addr, alignment); const march_forward_bytes = if (rem == 0) 0 else (alignment - rem); const adjusted_index = some_mem_index + march_forward_bytes; const end_index = adjusted_index + n; + if (end_index > some_mem.len) { + return error.OutOfMemory; + } const result = some_mem[adjusted_index .. end_index]; some_mem_index = end_index; return result;