authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-25 16:09:51+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-27 01:31:18+03:00
log5b79f42dc5220850b67ac461a326d5a6253da215
tree5c6bdc9a8bb430333ac8460d251305a349478c8e
parentd9fe5ba7f805c82f14c162945ac851ebb570ec89

std.mem.Allocator: do not return undefined pointers


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

lib/std/mem/Allocator.zig+6-3
...@@ -286,7 +286,8 @@ pub fn allocAdvancedWithRetAddr(...@@ -286,7 +286,8 @@ pub fn allocAdvancedWithRetAddr(
286 } else @alignOf(T);286 } else @alignOf(T);
287287
288 if (n == 0) {288 if (n == 0) {
289 return @as([*]align(a) T, undefined)[0..0];289 const ptr = comptime std.mem.alignBackward(std.math.maxInt(usize), a);
290 return @intToPtr([*]align(a) T, ptr)[0..0];
290 }291 }
291292
292 const byte_count = math.mul(usize, @sizeOf(T), n) catch return Error.OutOfMemory;293 const byte_count = math.mul(usize, @sizeOf(T), n) catch return Error.OutOfMemory;
...@@ -383,7 +384,8 @@ pub fn reallocAdvancedWithRetAddr(...@@ -383,7 +384,8 @@ pub fn reallocAdvancedWithRetAddr(
383 }384 }
384 if (new_n == 0) {385 if (new_n == 0) {
385 self.free(old_mem);386 self.free(old_mem);
386 return @as([*]align(new_alignment) T, undefined)[0..0];387 const ptr = comptime std.mem.alignBackward(std.math.maxInt(usize), new_alignment);
388 return @intToPtr([*]align(new_alignment) T, ptr)[0..0];
387 }389 }
388390
389 const old_byte_slice = mem.sliceAsBytes(old_mem);391 const old_byte_slice = mem.sliceAsBytes(old_mem);
...@@ -462,7 +464,8 @@ pub fn alignedShrinkWithRetAddr(...@@ -462,7 +464,8 @@ pub fn alignedShrinkWithRetAddr(
462 return old_mem;464 return old_mem;
463 if (new_n == 0) {465 if (new_n == 0) {
464 self.free(old_mem);466 self.free(old_mem);
465 return @as([*]align(new_alignment) T, undefined)[0..0];467 const ptr = comptime std.mem.alignBackward(std.math.maxInt(usize), new_alignment);
468 return @intToPtr([*]align(new_alignment) T, ptr)[0..0];
466 }469 }
467470
468 assert(new_n < old_mem.len);471 assert(new_n < old_mem.len);