From 2706495668df22413e61ac4d915ff2c43e18c5dc Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 11 Aug 2024 01:17:03 +0200 Subject: [PATCH] Add temp allocator scribble. Make bufferstream safer. --- lib/std/core/allocators/temp_allocator.c3 | 10 ++++++++++ lib/std/io/stream.c3 | 10 ++-------- test/unit/stdlib/io/bufferstream.c3 | 8 ++++---- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/std/core/allocators/temp_allocator.c3 b/lib/std/core/allocators/temp_allocator.c3 index a7216a97b..eee505931 100644 --- a/lib/std/core/allocators/temp_allocator.c3 +++ b/lib/std/core/allocators/temp_allocator.c3 @@ -65,6 +65,16 @@ fn void TempAllocator.release(&self, void* old_pointer, bool) @dynamic fn void TempAllocator.reset(&self, usz mark) @dynamic { TempAllocatorPage *last_page = self.last_page; + $if env::COMPILER_SAFE_MODE: + if (!last_page) + { + usz cleaned = self.used - mark; + if (cleaned > 0) + { + self.data[mark : cleaned] = 0xAA; + } + } + $endif while (last_page && last_page.mark > mark) { TempAllocatorPage *to_free = last_page; diff --git a/lib/std/io/stream.c3 b/lib/std/io/stream.c3 index 61ba728ae..9191a1092 100644 --- a/lib/std/io/stream.c3 +++ b/lib/std/io/stream.c3 @@ -141,15 +141,9 @@ fn usz! copy_to(InStream in, OutStream dst, char[] buffer = {}) if (&dst.read_to) return dst.read_to(in); $switch (env::MEMORY_ENV) $case NORMAL: - @pool() - { - return copy_through_buffer(in, dst, mem::temp_alloc_array(char, 4096)); - }; + return copy_through_buffer(in, dst, &&char[4096]{}); $case SMALL: - @pool() - { - return copy_through_buffer(in, dst, mem::temp_alloc_array(char, 1024)); - }; + return copy_through_buffer(in, dst, &&char[1024]{}); $case TINY: $case NONE: return copy_through_buffer(in, dst, &&(char[256]{})); diff --git a/test/unit/stdlib/io/bufferstream.c3 b/test/unit/stdlib/io/bufferstream.c3 index bf818eb9f..a40234c20 100644 --- a/test/unit/stdlib/io/bufferstream.c3 +++ b/test/unit/stdlib/io/bufferstream.c3 @@ -15,7 +15,7 @@ fn void! readbuffer_large() assert(n == DATA.len, "large read failed: got %d; want %d", n, DATA.len); String got = (String)bytes[..]; - assert(got == DATA, "large read failed: got %d; want %d", got, DATA); + assert(got == DATA, "large read failed: got %s; want %s", got, DATA); } fn void! readbuffer() @@ -33,7 +33,7 @@ fn void! readbuffer() assert(n == DATA.len, "got %d; want %d", n, DATA.len); String got = bw.str_view(); - assert(got == DATA, "got %d; want %d", got, DATA); + assert(got == DATA, "got %s; want %s", got, DATA); } fn void! writebuffer_large() @@ -48,7 +48,7 @@ fn void! writebuffer_large() assert(n == DATA.len, "large write failed: got %d; want %d", n, DATA.len); String got = out.str_view(); - assert(got == DATA, "large write failed: got %d; want %d", got, DATA); + assert(got == DATA, "large write failed: got %s; want %s", got, DATA); } fn void! writebuffer() @@ -65,5 +65,5 @@ fn void! writebuffer() assert(n == DATA.len, "got %d; want %d", n, DATA.len); String got = out.str_view(); - assert(got == DATA, "got %d; want %d", got, DATA); + assert(got == DATA, "got %s; want %s", got, DATA); } \ No newline at end of file