| ... | @@ -175,6 +175,7 @@ pub const Allocator = struct { | ... | @@ -175,6 +175,7 @@ pub const Allocator = struct { |
| 175 | | 175 | |
| 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; |
| 222 | | 223 | |
| 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 | } |