Fix malloc and realloc overflow
If user requests a size >= 65532, adding the heap admin size overflows size. Fixes #2358.
This commit is contained in:
@@ -131,6 +131,7 @@ _malloc:
|
||||
sta ptr1
|
||||
bcc @L1
|
||||
inc ptr1+1
|
||||
beq OutOfHeapSpace ; if high byte's 0, we overflowed!
|
||||
@L1: ldx ptr1+1
|
||||
bne @L2
|
||||
cmp #HEAP_MIN_BLOCKSIZE+1
|
||||
@@ -336,4 +337,3 @@ RetUserPtr:
|
||||
bcc @L9
|
||||
inx
|
||||
@L9: rts
|
||||
|
||||
|
||||
@@ -59,6 +59,11 @@ void* __fastcall__ realloc (void* block, register size_t size)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Don't overflow! */
|
||||
if (size > 0xFFFF - HEAP_ADMIN_SPACE) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Make the internal used size from the given size */
|
||||
size += HEAP_ADMIN_SPACE;
|
||||
if (size < sizeof (struct freeblock)) {
|
||||
@@ -107,6 +112,3 @@ void* __fastcall__ realloc (void* block, register size_t size)
|
||||
}
|
||||
return newblock;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user