authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-25 13:36:40-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-25 13:36:40-07:00
log6fb105fdd7798dc988de09a7b6709c5168355dfa
tree1c8489750384cd40b1e250be76d5c4595fbd4f63
parentea6a076065efb6de5450d945540f825523d5d6e3

std: GeneralPurposeAllocator: set freed bytes to undefined

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

1 files changed, 4 insertions(+), 2 deletions(-)

lib/std/heap/general_purpose_allocator.zig+4-2
......@@ -433,8 +433,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
433433 const bucket_slice = @ptrCast([*]align(@alignOf(BucketHeader)) u8, bucket)[0..bucket_size];
434434 self.backing_allocator.free(bucket_slice);
435435 } else {
436 // TODO Set the slot data to undefined.
437 // Related: https://github.com/ziglang/zig/issues/4298
436 @memset(bucket.page + slot_index * size_class, undefined, size_class);
438437 }
439438 }
440439
......@@ -567,6 +566,9 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
567566 const new_aligned_size = math.max(new_size, old_align);
568567 const new_size_class = math.ceilPowerOfTwoAssert(usize, new_aligned_size);
569568 if (new_size_class <= size_class) {
569 if (old_mem.len > new_size) {
570 @memset(old_mem.ptr + new_size, undefined, old_mem.len - new_size);
571 }
570572 return new_size;
571573 }
572574 return error.OutOfMemory;