diff options
| author | Benjamin Feng <contact@fengb.me> | 2019-12-04 22:43:02 -0600 |
|---|---|---|
| committer | Benjamin Feng <contact@fengb.me> | 2019-12-04 22:43:02 -0600 |
| commit | 30da6d49f435b7ef317b059113ec1fab21d72d00 (patch) | |
| tree | 50a3cef70f3f0b3b331b77247cf9aedd489cd48b /lib/std/heap.zig | |
| parent | 86ae75363e3fbf6b3835b87c1cfb79fe4bf97790 (diff) | |
| download | zig-30da6d49f435b7ef317b059113ec1fab21d72d00.tar.gz zig-30da6d49f435b7ef317b059113ec1fab21d72d00.zip | |
Fix freeing memory across bounds
Diffstat (limited to 'lib/std/heap.zig')
| -rw-r--r-- | lib/std/heap.zig | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/lib/std/heap.zig b/lib/std/heap.zig index 25580a974b..fcc9420cf7 100644 --- a/lib/std/heap.zig +++ b/lib/std/heap.zig @@ -379,8 +379,11 @@ const WasmPageAllocator = struct { } if (free_start < extendedOffset()) { - conventional.recycle(free_start, free_end - free_start); - } else { + const clamped_end = std.math.min(extendedOffset(), free_end); + conventional.recycle(free_start, clamped_end - free_start); + } + + if (free_end > extendedOffset()) { if (extended.totalPages() == 0) { // Steal the last page from the memory currently being recycled // TODO: would it be better if we use the first page instead? @@ -390,7 +393,8 @@ const WasmPageAllocator = struct { // Since this is the first page being freed and we consume it, assume *nothing* is free. std.mem.set(u8, extended.bytes, FreeBlock.used); } - extended.recycle(free_start - extendedOffset(), free_end - free_start); + const clamped_start = std.math.max(extendedOffset(), free_start); + extended.recycle(clamped_start - extendedOffset(), free_end - clamped_start); } } |
