Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sema: panic on int add/sub/mul overflow #11174

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9588,6 +9588,36 @@ fn analyzeArithmetic(
};

try sema.requireRuntimeBlock(block, rs.src);
if (block.wantSafety()) {
if (scalar_tag == .Int) {
const maybe_op_of: ?Air.Inst.Tag = switch (rs.air_tag) {
.add => .add_with_overflow,
.sub => .sub_with_overflow,
.mul => .mul_with_overflow,
else => null,
};
if (maybe_op_of) |op_of_tag| {
const mut_ptr_type = try Type.ptr(sema.arena, target, .{
.pointee_type = resolved_type,
.@"addrspace" = target_util.defaultAddressSpace(target, .local),
});
const ptr = try block.addTy(.alloc, mut_ptr_type);

const op_of = try block.addInst(.{
.tag = op_of_tag,
.data = .{ .pl_op = .{
.operand = ptr,
.payload = try sema.addExtra(Air.Bin{
.lhs = casted_lhs,
.rhs = casted_rhs,
}),
} },
});
const no_of = try block.addTyOp(.not, Type.bool, op_of);
try sema.addSafetyCheck(block, no_of, .arithmetic_overflow);
}
}
}
return block.addBinOp(rs.air_tag, casted_lhs, casted_rhs);
}

Expand Down Expand Up @@ -15158,6 +15188,7 @@ pub const PanicId = enum {
cast_to_null,
incorrect_alignment,
invalid_error_code,
arithmetic_overflow,
};

fn addSafetyCheck(
Expand Down Expand Up @@ -15279,6 +15310,7 @@ fn safetyPanic(
.cast_to_null => "cast causes pointer to be null",
.incorrect_alignment => "incorrect alignment",
.invalid_error_code => "invalid error code",
.arithmetic_overflow => "arithmetic overflow occurred",
};

const msg_inst = msg_inst: {
Expand Down