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

fix: spurious unsigned overflows #590

Merged
merged 4 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions src/aro/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6443,9 +6443,12 @@ fn addExpr(p: *Parser) Error!Result {
try rhs.expect(p);

const lhs_ty = lhs.ty;
const rhs_ty = rhs.ty;
if (try lhs.adjustTypes(minus.?, &rhs, p, if (plus != null) .add else .sub)) {
if (plus != null) {
if (try lhs.val.add(lhs.val, rhs.val, lhs.ty, p.comp)) try p.errOverflow(plus.?, lhs);
if (try lhs.val.add(lhs.val, rhs.val, lhs.ty, p.comp) and
rhs_ty.signedness(p.comp) != .unsigned and
lhs_ty.signedness(p.comp) != .unsigned) try p.errOverflow(plus.?, lhs);
Vexu marked this conversation as resolved.
Show resolved Hide resolved
} else {
if (try lhs.val.sub(lhs.val, rhs.val, lhs.ty, p.comp)) try p.errOverflow(minus.?, lhs);
}
Expand Down Expand Up @@ -6484,7 +6487,9 @@ fn mulExpr(p: *Parser) Error!Result {

if (try lhs.adjustTypes(percent.?, &rhs, p, if (tag == .mod_expr) .integer else .arithmetic)) {
if (mul != null) {
if (try lhs.val.mul(lhs.val, rhs.val, lhs.ty, p.comp)) try p.errOverflow(mul.?, lhs);
if (try lhs.val.mul(lhs.val, rhs.val, lhs.ty, p.comp) and
rhs.ty.signedness(p.comp) != .unsigned and
lhs.ty.signedness(p.comp) != .unsigned) try p.errOverflow(mul.?, lhs);
Vexu marked this conversation as resolved.
Show resolved Hide resolved
} else if (div != null) {
if (try lhs.val.div(lhs.val, rhs.val, lhs.ty, p.comp)) try p.errOverflow(mul.?, lhs);
} else {
Expand Down
5 changes: 5 additions & 0 deletions test/cases/arithmetic overflow.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_Static_assert(0xFFFFFFFF + 1 == 0, "'");
_Static_assert(1 + 0xFFFFFFFF == 0, "'");
_Static_assert(0 - 0x00000001 == 0xFFFFFFFF, "'");
_Static_assert(0x00000000 - 0x00000001 == 0xFFFFFFFF, "'");
_Static_assert(0x80000000 * 2 == 0, "'");
Loading