Skip to content

Commit ff6f52c

Browse files
authored
Merge pull request #1764 from Robotechnic/division_by_zero_messages
Division by zero messages
2 parents 861891e + b797dd6 commit ff6f52c

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

src/analyses/base.ml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,24 @@ struct
200200
| PlusA -> ID.add
201201
| MinusA -> ID.sub
202202
| Mult -> ID.mul
203-
| Div -> ID.div
204-
| Mod -> ID.rem
203+
| Div ->
204+
fun x y ->
205+
(match ID.equal_to Z.zero y with
206+
| `Eq ->
207+
M.error ~category:M.Category.Integer.div_by_zero ~tags:[CWE 369] "Second argument of division is zero"
208+
| `Top ->
209+
M.warn ~category:M.Category.Integer.div_by_zero ~tags:[CWE 369] "Second argument of division might be zero"
210+
| `Neq -> ());
211+
ID.div x y
212+
| Mod ->
213+
fun x y ->
214+
(match ID.equal_to Z.zero y with
215+
| `Eq ->
216+
M.error ~category:M.Category.Integer.div_by_zero ~tags:[CWE 369] "Second argument of modulo is zero"
217+
| `Top ->
218+
M.warn ~category:M.Category.Integer.div_by_zero ~tags:[CWE 369] "Second argument of modulo might be zero"
219+
| `Neq -> ());
220+
ID.rem x y
205221
| Lt -> ID.lt
206222
| Gt -> ID.gt
207223
| Le -> ID.le

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ struct
296296
begin
297297
let is_zero v = Ints_t.compare v Ints_t.zero = 0 in
298298
match y1, y2 with
299-
| l, u when is_zero l && is_zero u -> (top_of ik,{underflow=false; overflow=false}) (* TODO warn about undefined behavior *)
299+
| l, u when is_zero l && is_zero u -> (top_of ik,{underflow=false; overflow=false})
300300
| l, _ when is_zero l -> div ik (Some (x1,x2)) (Some (Ints_t.one,y2))
301301
| _, u when is_zero u -> div ik (Some (x1,x2)) (Some (y1, Ints_t.(neg one)))
302302
| _ when leq (of_int ik (Ints_t.zero) |> fst) (Some (y1,y2)) -> (top_of ik,{underflow=false; overflow=false})

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ struct
362362
let top_of ik = top_of ik |> List.hd in
363363
let is_zero v = v =. Ints_t.zero in
364364
match y1, y2 with
365-
| l, u when is_zero l && is_zero u -> top_of ik (* TODO warn about undefined behavior *)
365+
| l, u when is_zero l && is_zero u -> top_of ik
366366
| l, _ when is_zero l -> interval_div x (Ints_t.one,y2)
367367
| _, u when is_zero u -> interval_div x (y1, Ints_t.(neg one))
368368
| _ when leq (of_int ik (Ints_t.zero) |> fst) ([(y1,y2)]) -> top_of ik
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// PARAM: --enable ana.int.interval
2+
3+
int main(void) {
4+
5+
int a = 0;
6+
for (int i = 0; i < 5; i++) {
7+
a += 100/i; //WARN
8+
}
9+
a = 5;
10+
a /= 0; //WARN
11+
12+
return 0;
13+
}

0 commit comments

Comments
 (0)