Skip to content

Commit c74c04a

Browse files
committed
Add next block adjacency check to validate_block()
The allocator previously only validated the size and bounds of a memory block. It did not check whether the block's successor was physically adjacent in memory. Extend validate_block() to verify that a block's next pointer matches the expected location based on its size. This ensures corruption in the linked list of blocks is detected early and consistently.
1 parent 9e55af4 commit c74c04a

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

lib/malloc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ static inline bool validate_block(memblock_t *block)
5656
if ((uint8_t *) block + sizeof(memblock_t) + size > (uint8_t *) heap_end)
5757
return false;
5858

59+
if (block->next &&
60+
(uint8_t *) block + sizeof(memblock_t) + GET_SIZE(block) !=
61+
(uint8_t *) block->next)
62+
return false;
63+
5964
return true;
6065
}
6166

0 commit comments

Comments
 (0)