Skip to content

Commit 622dc5d

Browse files
committed
Check for division overflow in modulo operations
1 parent 6ad40ab commit 622dc5d

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

src/cdomain/value/cdomains/int/intDomTuple.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,12 @@ module IntDomTupleImpl = struct
448448
{f2_ovc = (fun (type a) (module I : SOverflow with type t = a) ?no_ov -> I.div ?no_ov ik)}
449449

450450
let rem ik =
451-
map2 ik {f2= (fun (type a) (module I : SOverflow with type t = a) ?no_ov -> I.rem ik)}
451+
map2ovc ~op:(Binop Mod) ik
452+
{f2_ovc = (fun (type a) (module I : SOverflow with type t = a) ?no_ov x y ->
453+
(* C11 6.5.5.6: if [x/y] is not representable (i.e. overflows), then [x%y] is undefined (let's also call it overflow) *)
454+
let (_, div_ov) = I.div ?no_ov ik x y in
455+
(I.rem ik x y, div_ov) (* TODO: should [div] overflow check be moved into each [rem] in each int domain? *)
456+
)}
452457

453458
let lt ik =
454459
map2 ik {f2= (fun (type a) (module I : SOverflow with type t = a) ?no_ov -> I.lt ik)}

tests/regression/39-signed-overflows/17-mod-minus-1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ int main() {
66
int x, y;
77
bad = x % y; // WARN (div by zero and overflow, distinguished in cram test)
88
if (y != 0) {
9-
bad = x % y; // TODO WARN (overflow)
9+
bad = x % y; // WARN (overflow)
1010
}
1111
return 0;
1212
}

tests/regression/39-signed-overflows/17-mod-minus-1.t

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
TODO: should warn about overflow in all three %-s
22
$ goblint --enable warn.deterministic --enable ana.int.interval 17-mod-minus-1.c
3+
[Warning][Integer > Overflow][CWE-190] Signed integer overflow in % (17-mod-minus-1.c:7:5-7:16)
4+
[Warning][Integer > Overflow][CWE-190] Signed integer overflow in % (17-mod-minus-1.c:9:9-9:20)
35
[Warning][Integer > DivByZero][CWE-369] Second argument of modulo might be zero (17-mod-minus-1.c:7:5-7:16)
46
[Info][Deadcode] Logical lines of code (LLoC) summary:
57
live: 6
@@ -8,6 +10,8 @@ TODO: should warn about overflow in all three %-s
810

911
TODO: should warn about overflow in all three %-s
1012
$ goblint --enable warn.deterministic --enable ana.int.interval_set 17-mod-minus-1.c
13+
[Warning][Integer > Overflow][CWE-190] Signed integer overflow in % (17-mod-minus-1.c:7:5-7:16)
14+
[Warning][Integer > Overflow][CWE-190] Signed integer overflow in % (17-mod-minus-1.c:9:9-9:20)
1115
[Warning][Integer > DivByZero][CWE-369] Second argument of modulo might be zero (17-mod-minus-1.c:7:5-7:16)
1216
[Info][Deadcode] Logical lines of code (LLoC) summary:
1317
live: 6

0 commit comments

Comments
 (0)