authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-26 18:52:43-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-01-26 18:52:43-05:00
log518dbd30cb985cd9a5d89a8201becf1bd398330e
treeb35e5babff7c99c18b63d5d4ae23ee3b795109ff
parent51ac8eb08e7542c25c7d3a0d1ebeac040544e4c0
parent5cc49324611a78b9fad5376bae39aa2e38f98ea8
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4133 from daurnimator/4087-free-sets-undefined

Sets memory to undefined when freed from allocator

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

lib/std/http/headers.zig+1-1
...@@ -172,7 +172,7 @@ pub const Headers = struct {...@@ -172,7 +172,7 @@ pub const Headers = struct {
172 var dex = HeaderIndexList.init(self.allocator);172 var dex = HeaderIndexList.init(self.allocator);
173 try dex.append(n - 1);173 try dex.append(n - 1);
174 errdefer dex.deinit();174 errdefer dex.deinit();
175 _ = try self.index.put(name, dex);175 _ = try self.index.put(name_dup, dex);
176 }176 }
177 self.data.appendAssumeCapacity(entry);177 self.data.appendAssumeCapacity(entry);
178 }178 }
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 }