Skip to content

Commit 0913d6a

Browse files
authored
Merge pull request #217 from goblint/overflow-mod
Check for division overflow in modulo operation `constFold`
2 parents abe64c5 + d4ff56f commit 0913d6a

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/cil.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2715,7 +2715,12 @@ and constFoldBinOp (machdep: bool) bop e1 e2 tres =
27152715
end
27162716
| Div, _, Some o when compare_cilint o one_cilint = 0 -> collapse e1'
27172717
| Mod, Some i1, Some i2 -> begin
2718-
try no_ov (rem_cilint i1 i2)
2718+
try
2719+
(* C11 6.5.5.6: if [i1/i2] is not representable (i.e. overflows), then [i1%i2] is undefined *)
2720+
if isSigned tk && snd (truncateCilint tk (div0_cilint i1 i2)) <> NoTruncation then
2721+
BinOp(bop, e1', e2', tres)
2722+
else
2723+
no_ov (rem_cilint i1 i2)
27192724
with Division_by_zero -> BinOp(bop, e1', e2', tres)
27202725
end
27212726
| Mod, _, Some o when compare_cilint o one_cilint = 0 -> collapse0 ()

0 commit comments

Comments
 (0)