authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-06-23 16:35:44-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-23 18:28:33-07:00
log5fc5e4fbe04ccbe5ea37b07c1153a7c5bd2b4346
tree1621f97c37307f64f7ef730384a209f8c52fbac5
parent9d66481e3df35b54275373a685e765a4bcebd712

sema: Fix overflow when analyzing an inline switch prong range that ends on the maximum value of the switched type


2 files changed, 14 insertions(+), 0 deletions(-)

src/Sema.zig+2
......@@ -11646,6 +11646,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1164611646 cases_extra.appendAssumeCapacity(@intCast(u32, case_block.instructions.items.len));
1164711647 cases_extra.appendAssumeCapacity(@intFromEnum(item_ref));
1164811648 cases_extra.appendSliceAssumeCapacity(case_block.instructions.items);
11649
11650 if (item.compareScalar(.eq, item_last, operand_ty, mod)) break;
1164911651 }
1165011652 }
1165111653
test/behavior/switch.zig+12
......@@ -785,3 +785,15 @@ test "switch pointer capture peer type resolution" {
785785 try expectEqual(U{ .a = 111 }, ua);
786786 try expectEqual(U{ .b = 222 }, ub);
787787}
788
789test "inline switch range that includes the maximum value of the switched type" {
790 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
791
792 const inputs: [3]u8 = .{ 0, 254, 255 };
793 for (inputs) |input| {
794 switch (input) {
795 inline 254...255 => |val| try expectEqual(input, val),
796 else => |val| try expectEqual(input, val),
797 }
798 }
799}