zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 6fb105fdd7798dc988de09a7b6709c5168355dfa (tree)
parent ea6a076065efb6de5450d945540f825523d5d6e3
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Tue, 25 Aug 2020 13:36:40 -0700

std: GeneralPurposeAllocator: set freed bytes to undefined

Helps catch use-after-free. Caught a couple issues in the self-hosted
compiler.

Diffstat:
Mlib/std/heap/general_purpose_allocator.zig | 6++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/lib/std/heap/general_purpose_allocator.zig b/lib/std/heap/general_purpose_allocator.zig @@ -433,8 +433,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type { const bucket_slice = @ptrCast([*]align(@alignOf(BucketHeader)) u8, bucket)[0..bucket_size]; self.backing_allocator.free(bucket_slice); } else { - // TODO Set the slot data to undefined. - // Related: https://github.com/ziglang/zig/issues/4298 + @memset(bucket.page + slot_index * size_class, undefined, size_class); } } @@ -567,6 +566,9 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type { const new_aligned_size = math.max(new_size, old_align); const new_size_class = math.ceilPowerOfTwoAssert(usize, new_aligned_size); if (new_size_class <= size_class) { + if (old_mem.len > new_size) { + @memset(old_mem.ptr + new_size, undefined, old_mem.len - new_size); + } return new_size; } return error.OutOfMemory;