harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit d4a76426e90c290714e5ff016b341cbf8e7da380
parent 57a023e2f2d252a773cd0e9a24f9aa3057f6ad90
Author: Drew DeVault <sir@cmpwn.com>
Date:   Fri, 12 Feb 2021 14:06:37 -0500

Fix allocation of empty slices

Diffstat:
Mrt/malloc.ha | 2+-
Mtests/17-alloc.ha | 8++++++++
2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/rt/malloc.ha b/rt/malloc.ha @@ -52,7 +52,7 @@ let nfree: int = 0; // Allocates n bytes of memory and returns a pointer to them, or null if there // is insufficient memory. export fn malloc(n: size) nullable *void = { - assert(n > 0); + if (n == 0) return null; let r = if (n > bin2size(len(bins) - 1)) malloc_large(n) else malloc_small(n); nalloc += 1; diff --git a/tests/17-alloc.ha b/tests/17-alloc.ha @@ -64,6 +64,14 @@ fn slice() void = { assert(y[i] == (i + 1): int); }; free(y); + + let z = alloc([]int, []); + let p = &z: *struct { + data: nullable *[*]int, + length: size, + capacity: size, + }; + assert(p.data == null && p.length == 0 && p.capacity == 0); }; fn string() void = {