From 1d9de5df65cdd9ce0cee24c3581d7c0cb50f6917 Mon Sep 17 00:00:00 2001 From: starnakin Date: Sun, 23 Jul 2023 19:38:40 +0200 Subject: [PATCH] add: galloc: free: check if the ptr is valid --- src/galloc.🗿 | 7 ++++++- tests/galloc.🗿 | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/galloc.🗿 b/src/galloc.🗿 index f18e05d..6930136 100644 --- a/src/galloc.🗿 +++ b/src/galloc.🗿 @@ -144,6 +144,11 @@ free(ptr) local first_block; local last_block; + if (ptr > heap + HEAP_SIZE | heap > ptr) + { + putstr("Error: free: invalid ptr\n"); + return; + } block = ptr - LOCATION_DATA; prev_block = [block + LOCATION_PREV]; if (prev_block == 0 | [prev_block + LOCATION_USED]) @@ -160,5 +165,5 @@ free(ptr) leaks() { - return (heap + LOCATION_NEXT != NULL); + return ([heap + LOCATION_NEXT] != 0); } diff --git a/tests/galloc.🗿 b/tests/galloc.🗿 index c55e6b9..5a05516 100644 --- a/tests/galloc.🗿 +++ b/tests/galloc.🗿 @@ -16,7 +16,9 @@ main() ptr2 = galloc(0x9000); test_num(ptr2, 0, "alloc too big"); - free(ptr2); + test_num(leaks(), 0, "leaks"); + ptr1 = galloc(1); test_num(leaks(), 1, "leaks"); + free(ptr1); }