authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-29 20:25:40-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-29 20:25:40-07:00
log4b5fc5239c37fa3f49965188256a781c7b1277a3
tree81cbec51863eae93904e7718f5de0f344f990984
parent40ba4d4a89cc30ac5f5cbb560a2f8d75f0b3145e

std.heap.raw_c_allocator: fix illegal alignment cast

See the comment added in this commit for more details. closes #14090

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

lib/std/heap.zig+7-3
......@@ -181,9 +181,13 @@ fn rawCAlloc(
181181) ?[*]u8 {
182182 _ = ret_addr;
183183 assert(log2_ptr_align <= comptime std.math.log2_int(usize, @alignOf(std.c.max_align_t)));
184 // TODO: change the language to make @ptrCast also do alignment cast
185 const ptr = @alignCast(@alignOf(std.c.max_align_t), c.malloc(len));
186 return @ptrCast(?[*]align(@alignOf(std.c.max_align_t)) u8, ptr);
184 // Note that this pointer cannot be aligncasted to max_align_t because if
185 // len is < max_align_t then the alignment can be smaller. For example, if
186 // max_align_t is 16, but the user requests 8 bytes, there is no built-in
187 // type in C that is size 8 and has 16 byte alignment, so the alignment may
188 // be 8 bytes rather than 16. Similarly if only 1 byte is requested, malloc
189 // is allowed to return a 1-byte aligned pointer.
190 return @ptrCast(?[*]u8, c.malloc(len));
187191}
188192
189193fn rawCResize(