From 4697a16206583dd29377c6590c86b869316d22ff Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 14 Feb 2023 20:17:31 +0100 Subject: [PATCH] Heap: Avoid accessing a block after it's potentially deleted --- luna/src/Heap.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/luna/src/Heap.cpp b/luna/src/Heap.cpp index 25623a71..12507913 100644 --- a/luna/src/Heap.cpp +++ b/luna/src/Heap.cpp @@ -130,6 +130,8 @@ static Result combine_forward(HeapBlock* block) heap.remove(next); next->magic = BLOCK_DEAD; + block->full_size += next->full_size + sizeof(HeapBlock); + if (next->status & BLOCK_END_MEM) { if (next->status & BLOCK_START_MEM) @@ -142,8 +144,6 @@ static Result combine_forward(HeapBlock* block) block->status |= BLOCK_END_MEM; } - block->full_size += next->full_size + sizeof(HeapBlock); - return {}; } @@ -159,6 +159,8 @@ static Result combine_backward(HeapBlock* block) heap.remove(block); block->magic = BLOCK_DEAD; + last->full_size += block->full_size + sizeof(HeapBlock); + if (block->status & BLOCK_END_MEM) { if (block->status & BLOCK_START_MEM) @@ -171,8 +173,6 @@ static Result combine_backward(HeapBlock* block) last->status |= BLOCK_END_MEM; } - last->full_size += block->full_size + sizeof(HeapBlock); - return last; }