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
signaturelock-open 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 {...@@ -175,6 +175,7 @@ pub const Allocator = struct {
175175
176 const old_byte_slice = @sliceToBytes(old_mem);176 const old_byte_slice = @sliceToBytes(old_mem);
177 const byte_count = math.mul(usize, @sizeOf(T), new_n) catch return Error.OutOfMemory;177 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
178 const byte_slice = try self.reallocFn(self, old_byte_slice, Slice.alignment, byte_count, new_alignment);179 const byte_slice = try self.reallocFn(self, old_byte_slice, Slice.alignment, byte_count, new_alignment);
179 assert(byte_slice.len == byte_count);180 assert(byte_slice.len == byte_count);
180 if (new_n > old_mem.len) {181 if (new_n > old_mem.len) {
...@@ -221,6 +222,7 @@ pub const Allocator = struct {...@@ -221,6 +222,7 @@ pub const Allocator = struct {
221 const byte_count = @sizeOf(T) * new_n;222 const byte_count = @sizeOf(T) * new_n;
222223
223 const old_byte_slice = @sliceToBytes(old_mem);224 const old_byte_slice = @sliceToBytes(old_mem);
225 @memset(old_byte_slice.ptr + byte_count, undefined, old_byte_slice.len - byte_count);
224 const byte_slice = self.shrinkFn(self, old_byte_slice, Slice.alignment, byte_count, new_alignment);226 const byte_slice = self.shrinkFn(self, old_byte_slice, Slice.alignment, byte_count, new_alignment);
225 assert(byte_slice.len == byte_count);227 assert(byte_slice.len == byte_count);
226 return @bytesToSlice(T, @alignCast(new_alignment, byte_slice));228 return @bytesToSlice(T, @alignCast(new_alignment, byte_slice));
...@@ -234,6 +236,7 @@ pub const Allocator = struct {...@@ -234,6 +236,7 @@ pub const Allocator = struct {
234 const bytes_len = bytes.len + @boolToInt(Slice.sentinel != null);236 const bytes_len = bytes.len + @boolToInt(Slice.sentinel != null);
235 if (bytes_len == 0) return;237 if (bytes_len == 0) return;
236 const non_const_ptr = @intToPtr([*]u8, @ptrToInt(bytes.ptr));238 const non_const_ptr = @intToPtr([*]u8, @ptrToInt(bytes.ptr));
239 @memset(non_const_ptr, undefined, bytes_len);
237 const shrink_result = self.shrinkFn(self, non_const_ptr[0..bytes_len], Slice.alignment, 0, 1);240 const shrink_result = self.shrinkFn(self, non_const_ptr[0..bytes_len], Slice.alignment, 0, 1);
238 assert(shrink_result.len == 0);241 assert(shrink_result.len == 0);
239 }242 }