浏览代码

Make sure realloc copied all the old data

Signed-off-by: Sean Young <sean@mess.org>
Sean Young 3 年之前
父节点
当前提交
93c7ecbecc
共有 2 个文件被更改,包括 12 次插入1 次删除
  1. 二进制
      stdlib/wasm/wasmheap.bc
  2. 12 1
      stdlib/wasmheap.c

二进制
stdlib/wasm/wasmheap.bc


+ 12 - 1
stdlib/wasmheap.c

@@ -117,8 +117,19 @@ void *__realloc(void *m, size_t size)
     }
     else
     {
+        // allocate new area and copy old data
+        uint32_t len = cur->length;
+
+        // if new size is smaller than the old data, only copy remaining data
+        if (size < len)
+            len = size;
+
         void *n = __malloc(size);
-        __memcpy8(n, m, size / 8);
+
+        // __memcpy8() copies 8 bytes at once; round up to the nearest 8 bytes
+        // this is permitted because allocations are always aligned on 8 byte
+        // boundaries anyway.
+        __memcpy8(n, m, (len + 7) / 8);
         __free(m);
         return n;
     }