aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/runtime/arch_tinygowasm_malloc.go
diff options
context:
space:
mode:
authorAnuraag Agrawal <[email protected]>2022-11-21 13:25:24 +0900
committerRon Evans <[email protected]>2022-11-21 21:00:36 +0100
commitb731919f97512e27a492f44942238c21f442c8d0 (patch)
tree56ad08b34b426d89f4e00014f4b26e9dc79baa1c /src/runtime/arch_tinygowasm_malloc.go
parentc759e6fc2db8caf20098805275f045da97b1fca4 (diff)
downloadtinygo-b731919f97512e27a492f44942238c21f442c8d0.tar.gz
tinygo-b731919f97512e27a492f44942238c21f442c8d0.zip
Fix panic when size 0 passed to malloc
Diffstat (limited to 'src/runtime/arch_tinygowasm_malloc.go')
-rw-r--r--src/runtime/arch_tinygowasm_malloc.go8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/runtime/arch_tinygowasm_malloc.go b/src/runtime/arch_tinygowasm_malloc.go
index f7f0c5a2c..06ae96cac 100644
--- a/src/runtime/arch_tinygowasm_malloc.go
+++ b/src/runtime/arch_tinygowasm_malloc.go
@@ -13,6 +13,9 @@ var allocs = make(map[uintptr][]byte)
//export malloc
func libc_malloc(size uintptr) unsafe.Pointer {
+ if size == 0 {
+ return nil
+ }
buf := make([]byte, size)
ptr := unsafe.Pointer(&buf[0])
allocs[uintptr(ptr)] = buf
@@ -39,6 +42,11 @@ func libc_calloc(nmemb, size uintptr) unsafe.Pointer {
//export realloc
func libc_realloc(oldPtr unsafe.Pointer, size uintptr) unsafe.Pointer {
+ if size == 0 {
+ libc_free(oldPtr)
+ return nil
+ }
+
// It's hard to optimize this to expand the current buffer with our GC, but
// it is theoretically possible. For now, just always allocate fresh.
buf := make([]byte, size)