Skip to content

Commit

Permalink
Don't initialize in aligned_alloc until validating size
Browse files Browse the repository at this point in the history
  • Loading branch information
mysterymath committed Jan 28, 2024
1 parent 5202c81 commit ff19661
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions mos-platform/common/c/malloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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;
Expand Down

0 comments on commit ff19661

Please sign in to comment.