From 2f3baf40c0be5b345d2af19e75060f22a49f93f1 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 2 Sep 2026 13:01:58 +0100 Subject: [PATCH] Fix alignment issues with `Small_Stack` --- core/mem/allocators.odin | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/core/mem/allocators.odin b/core/mem/allocators.odin index 92e95ea2c..824b38fa7 100644 --- a/core/mem/allocators.odin +++ b/core/mem/allocators.odin @@ -1338,6 +1338,9 @@ small_stack_alloc_non_zeroed :: proc( return raw_data(bytes), err } +@(private="file") +SMALL_STACK_MAX_ALIGNMENT :: 1 << (8*size_of(Small_Stack_Allocation_Header{}.padding) - 1) + /* Allocate memory from a small stack allocator. @@ -1356,7 +1359,10 @@ small_stack_alloc_bytes_non_zeroed :: proc( panic("Allocation on an uninitialized Small Stack allocator.", loc) } alignment := alignment - alignment = clamp(alignment, 1, 8*size_of(Stack_Allocation_Header{}.padding)/2) + alignment = max(alignment, 1) + if alignment > SMALL_STACK_MAX_ALIGNMENT { + return nil, .Invalid_Argument + } curr_addr := uintptr(raw_data(s.data)) + uintptr(s.offset) padding := calc_padding_with_header(curr_addr, uintptr(alignment), size_of(Small_Stack_Allocation_Header)) if s.offset + padding + size > len(s.data) { @@ -1544,7 +1550,10 @@ small_stack_resize_bytes_non_zeroed :: proc( old_memory := raw_data(old_data) old_size := len(old_data) alignment := alignment - alignment = clamp(alignment, 1, 8*size_of(Stack_Allocation_Header{}.padding)/2) + alignment = max(alignment, 1) + if alignment > SMALL_STACK_MAX_ALIGNMENT { + return nil, .Invalid_Argument + } if old_memory == nil { return small_stack_alloc_bytes_non_zeroed(s, size, alignment, loc) }