Skip to content

Commit 2669acc

Browse files
committed
malloc(0) now returns NULL with simple/standard allocator
1 parent 02e378d commit 2669acc

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

src/libc/allocator_simple.src

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,28 @@
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
1719
ld (_heap_ptr),hl
1820
ex de,hl
1921
ret
2022
.null:
21-
or a,a
22-
sbc hl,hl
23-
ret
24-
25-
public __simple_free
26-
__simple_free:
27-
ret
28-
2923
public __simple_realloc
3024
__simple_realloc:
3125
or a,a
3226
sbc hl,hl
27+
public __simple_free
28+
__simple_free:
3329
ret
3430

3531
section .data

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)