Skip to content

Commit

Permalink
[MS-DOS] Better heap overflow detection in malloc()
Browse files Browse the repository at this point in the history
  • Loading branch information
lpereira committed Jun 27, 2022
1 parent 089dc8e commit 879e973
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions platforms/msdos/extras.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,21 @@ time_t time(time_t *t)

void *malloc(size_t v)
{
static char heap[4096];
static char heap[5000];
static char *heap_ptr = heap;
char *old_heap_ptr;

old_heap_ptr = heap_ptr;
heap_ptr += v;
if (sizeof(heap) - (heap_ptr - heap) < v) {
asm __volatile("int $16\n"
"jmp exit"
:
: "a"((uint16_t)0));

if (heap_ptr > heap + sizeof(heap))
return NULL;
}

old_heap_ptr = heap_ptr;
heap_ptr += v;

return old_heap_ptr;
}
Expand Down

0 comments on commit 879e973

Please sign in to comment.