diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-01-26 18:52:43 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-26 18:52:43 -0500 |
| commit | 518dbd30cb985cd9a5d89a8201becf1bd398330e (patch) | |
| tree | b35e5babff7c99c18b63d5d4ae23ee3b795109ff /lib/std/mem.zig | |
| parent | 51ac8eb08e7542c25c7d3a0d1ebeac040544e4c0 (diff) | |
| parent | 5cc49324611a78b9fad5376bae39aa2e38f98ea8 (diff) | |
| download | zig-518dbd30cb985cd9a5d89a8201becf1bd398330e.tar.gz zig-518dbd30cb985cd9a5d89a8201becf1bd398330e.zip | |
Merge pull request #4133 from daurnimator/4087-free-sets-undefined
Sets memory to undefined when freed from allocator
Diffstat (limited to 'lib/std/mem.zig')
| -rw-r--r-- | lib/std/mem.zig | 3 |
1 files changed, 3 insertions, 0 deletions
diff --git a/lib/std/mem.zig b/lib/std/mem.zig index f7b2bf261d..46f23c84fe 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -175,6 +175,7 @@ pub const Allocator = struct { const old_byte_slice = @sliceToBytes(old_mem); const byte_count = math.mul(usize, @sizeOf(T), new_n) catch return Error.OutOfMemory; + // Note: can't set shrunk memory to undefined as memory shouldn't be modified on realloc failure const byte_slice = try self.reallocFn(self, old_byte_slice, Slice.alignment, byte_count, new_alignment); assert(byte_slice.len == byte_count); if (new_n > old_mem.len) { @@ -221,6 +222,7 @@ pub const Allocator = struct { const byte_count = @sizeOf(T) * new_n; const old_byte_slice = @sliceToBytes(old_mem); + @memset(old_byte_slice.ptr + byte_count, undefined, old_byte_slice.len - byte_count); const byte_slice = self.shrinkFn(self, old_byte_slice, Slice.alignment, byte_count, new_alignment); assert(byte_slice.len == byte_count); return @bytesToSlice(T, @alignCast(new_alignment, byte_slice)); @@ -234,6 +236,7 @@ pub const Allocator = struct { const bytes_len = bytes.len + @boolToInt(Slice.sentinel != null); if (bytes_len == 0) return; const non_const_ptr = @intToPtr([*]u8, @ptrToInt(bytes.ptr)); + @memset(non_const_ptr, undefined, bytes_len); const shrink_result = self.shrinkFn(self, non_const_ptr[0..bytes_len], Slice.alignment, 0, 1); assert(shrink_result.len == 0); } |
