authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-01-11 00:09:29+11:00
committergravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-01-21 03:17:40+11:00
log5cc49324611a78b9fad5376bae39aa2e38f98ea8
tree17920cb1d07851c65aa66f26d45616fefbdab04c
parent65013d8599abdcec5344cd918f5a1c0e5a9ca136
signature Commit is signed but in an unrecognized format.

std: allocator interface sets freed memory to undefined


1 files changed, 3 insertions(+), 0 deletions(-)

lib/std/mem.zig+3
......@@ -175,6 +175,7 @@ pub const Allocator = struct {
175175
176176 const old_byte_slice = @sliceToBytes(old_mem);
177177 const byte_count = math.mul(usize, @sizeOf(T), new_n) catch return Error.OutOfMemory;
178 // Note: can't set shrunk memory to undefined as memory shouldn't be modified on realloc failure
178179 const byte_slice = try self.reallocFn(self, old_byte_slice, Slice.alignment, byte_count, new_alignment);
179180 assert(byte_slice.len == byte_count);
180181 if (new_n > old_mem.len) {
......@@ -221,6 +222,7 @@ pub const Allocator = struct {
221222 const byte_count = @sizeOf(T) * new_n;
222223
223224 const old_byte_slice = @sliceToBytes(old_mem);
225 @memset(old_byte_slice.ptr + byte_count, undefined, old_byte_slice.len - byte_count);
224226 const byte_slice = self.shrinkFn(self, old_byte_slice, Slice.alignment, byte_count, new_alignment);
225227 assert(byte_slice.len == byte_count);
226228 return @bytesToSlice(T, @alignCast(new_alignment, byte_slice));
......@@ -234,6 +236,7 @@ pub const Allocator = struct {
234236 const bytes_len = bytes.len + @boolToInt(Slice.sentinel != null);
235237 if (bytes_len == 0) return;
236238 const non_const_ptr = @intToPtr([*]u8, @ptrToInt(bytes.ptr));
239 @memset(non_const_ptr, undefined, bytes_len);
237240 const shrink_result = self.shrinkFn(self, non_const_ptr[0..bytes_len], Slice.alignment, 0, 1);
238241 assert(shrink_result.len == 0);
239242 }