From ff1966134d790f5c8056a676e78e46724aaf2f7c Mon Sep 17 00:00:00 2001 From: Daniel Thornburgh Date: Sun, 28 Jan 2024 12:11:23 -0800 Subject: [PATCH] Don't initialize in aligned_alloc until validating size --- mos-platform/common/c/malloc.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mos-platform/common/c/malloc.cc b/mos-platform/common/c/malloc.cc index aaffff94c..38483ff4c 100644 --- a/mos-platform/common/c/malloc.cc +++ b/mos-platform/common/c/malloc.cc @@ -296,9 +296,6 @@ void *aligned_alloc(size_t alignment, size_t size) { if (!size) return nullptr; - if (!initialized) - init(); - // The region before the aligned chunk needs to be large enough to fit a free // chunk. if (__builtin_add_overflow(size, MIN_CHUNK_SIZE, &size)) @@ -308,6 +305,9 @@ void *aligned_alloc(size_t alignment, size_t size) { if (__builtin_add_overflow(size, alignment - 1, &size)) return nullptr; + if (!initialized) + init(); + FreeChunk *chunk = find_fit(size); if (!chunk) return nullptr;