diff options
author | Ben Smith <binji@chromium.org> | 2019-08-29 17:52:04 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-29 17:52:04 -0400 |
commit | 3e938b7a10cc6738037dce5f18675cc5d267992d (patch) | |
tree | db94f7689d3078947b6af33988d011df3258c630 | |
parent | 97bd353df98dfea72f1d7d06b4d087b508a4c37a (diff) | |
download | wabt-3e938b7a10cc6738037dce5f18675cc5d267992d.tar.gz wabt-3e938b7a10cc6738037dce5f18675cc5d267992d.tar.bz2 wabt-3e938b7a10cc6738037dce5f18675cc5d267992d.zip |
[wasm2c] Return -1 if wasm_rt_grow_memory fails (#1148)
-rw-r--r-- | wasm2c/wasm-rt-impl.c | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/wasm2c/wasm-rt-impl.c b/wasm2c/wasm-rt-impl.c index 0fcbcf9d..b9fb3c40 100644 --- a/wasm2c/wasm-rt-impl.c +++ b/wasm2c/wasm-rt-impl.c @@ -103,12 +103,20 @@ void wasm_rt_allocate_memory(wasm_rt_memory_t* memory, uint32_t wasm_rt_grow_memory(wasm_rt_memory_t* memory, uint32_t delta) { uint32_t old_pages = memory->pages; uint32_t new_pages = memory->pages + delta; + if (new_pages == 0) { + return 0; + } if (new_pages < old_pages || new_pages > memory->max_pages) { return (uint32_t)-1; } + uint32_t new_size = new_pages * PAGE_SIZE; + uint8_t* new_data = realloc(memory->data, new_size); + if (memory->data == NULL) { + return (uint32_t)-1; + } memory->pages = new_pages; - memory->size = new_pages * PAGE_SIZE; - memory->data = realloc(memory->data, memory->size); + memory->size = new_size; + memory->data = new_data; memset(memory->data + old_pages * PAGE_SIZE, 0, delta * PAGE_SIZE); return old_pages; } |