authorgravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2022-03-17 22:29:39+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-17 18:00:48-07:00
logd7d2ccb7af7349617fa76eef3bdead7c259654a5
treed5585a7215f338a5c22cbbbbe4199a0b1ea58543
parentadfcc8851b6bb47b085cfe2526f0797b1f414996

Avoid index out of bounds for one-valued types in zirValidateArrayInit

Previously, the code assumed that `ptr_elem_ptr` was always followed by a `store`, but this is not true for types with one value (such as `u0`).

2 files changed, 6 insertions(+), 11 deletions(-)

src/Sema.zig+6-8
...@@ -3205,14 +3205,11 @@ fn zirValidateArrayInit(...@@ -3205,14 +3205,11 @@ fn zirValidateArrayInit(
3205 // instruction after it within the same block.3205 // instruction after it within the same block.
3206 // Possible performance enhancement: save the `block_index` between iterations3206 // Possible performance enhancement: save the `block_index` between iterations
3207 // of the for loop.3207 // of the for loop.
3208 const next_air_inst = inst: {3208 var block_index = block.instructions.items.len - 1;
3209 var block_index = block.instructions.items.len - 1;3209 while (block.instructions.items[block_index] != elem_ptr_air_inst) {
3210 while (block.instructions.items[block_index] != elem_ptr_air_inst) {3210 block_index -= 1;
3211 block_index -= 1;3211 }
3212 }3212 first_block_index = @minimum(first_block_index, block_index);
3213 first_block_index = @minimum(first_block_index, block_index);
3214 break :inst block.instructions.items[block_index + 1];
3215 };
32163213
3217 // Array has one possible value, so value is always comptime-known3214 // Array has one possible value, so value is always comptime-known
3218 if (opt_opv) |opv| {3215 if (opt_opv) |opv| {
...@@ -3222,6 +3219,7 @@ fn zirValidateArrayInit(...@@ -3222,6 +3219,7 @@ fn zirValidateArrayInit(
32223219
3223 // If the next instructon is a store with a comptime operand, this element3220 // If the next instructon is a store with a comptime operand, this element
3224 // is comptime.3221 // is comptime.
3222 const next_air_inst = block.instructions.items[block_index + 1];
3225 switch (air_tags[next_air_inst]) {3223 switch (air_tags[next_air_inst]) {
3226 .store => {3224 .store => {
3227 const bin_op = air_datas[next_air_inst].bin_op;3225 const bin_op = air_datas[next_air_inst].bin_op;
test/behavior/byteswap.zig-3
...@@ -114,9 +114,6 @@ fn vector0() !void {...@@ -114,9 +114,6 @@ fn vector0() !void {
114}114}
115115
116test "@byteSwap vectors u0" {116test "@byteSwap vectors u0" {
117 // TODO: vector initialization for @Vector(x, u0) currently fails.
118 if (builtin.zig_backend == .stage2_llvm) return error.SkipZigTest;
119
120 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;117 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
121 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;118 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
122 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;119 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;