Skip to content

Commit 132611a

Browse files
committed
malloc(0) now returns NULL with simple/standard allocator
1 parent 30a3ecc commit 132611a

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/libc/allocator_simple.src

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
public __simple_malloc
66
__simple_malloc:
7+
; returns NULL when size is zero
78
pop bc
89
ex (sp),hl
910
push bc
1011
ld de,(_heap_ptr)
12+
dec hl
1113
add hl,de
1214
jr c,.null
13-
ld bc,___heaptop
15+
ld bc,___heaptop-1
1416
sbc hl,bc
1517
jr nc,.null
1618
add hl,bc

src/libc/allocator_standard.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ void *_standard_malloc(size_t alloc_size)
2121

2222
/* add size of block header to real size */
2323
const size_t size = alloc_size + sizeof(block_t);
24-
if (size < alloc_size)
24+
/* abort if alloc_size is 0 or size overflowed */
25+
if (size <= alloc_size)
2526
{
2627
return NULL;
2728
}

0 commit comments

Comments
 (0)