summaryrefslogtreecommitdiff
path: root/test/Analysis/test-after-div-zero.c
blob: 159c80c11d86b403e828e5815b0a391f7bfa3cf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// RUN: %clang_analyze_cc1 -std=c99 -Dbool=_Bool -analyzer-checker=core,alpha.core.TestAfterDivZero -analyzer-output=text -verify %s
// RUN: %clang_analyze_cc1 -x c++ -analyzer-checker=core,alpha.core.TestAfterDivZero -analyzer-output=text -verify %s

int var;

void err_eq(int x) {
  var = 77 / x; // expected-note {{Division with compared value made here}}
  if (x == 0) { } // expected-warning {{Value being compared against zero has already been used for division}}
} // expected-note@-1 {{Value being compared against zero has already been used for division}}

void err_eq2(int x) {
  var = 77 / x; // expected-note {{Division with compared value made here}}
  if (0 == x) { } // expected-warning {{Value being compared against zero has already been used for division}}
} // expected-note@-1 {{Value being compared against zero has already been used for division}}

void err_ne(int x) {
  var = 77 / x; // expected-note {{Division with compared value made here}}
  if (x != 0) { } // expected-warning {{Value being compared against zero has already been used for division}}
} // expected-note@-1 {{Value being compared against zero has already been used for division}}

void err_ge(int x) {
  var = 77 / x; // expected-note {{Division with compared value made here}}
  if (x >= 0) { } // expected-warning {{Value being compared against zero has already been used for division}}
} // expected-note@-1 {{Value being compared against zero has already been used for division}}

void err_le(int x) {
  var = 77 / x; // expected-note {{Division with compared value made here}}
  if (x <= 0) {} // expected-warning {{Value being compared against zero has already been used for division}}
} // expected-note@-1 {{Value being compared against zero has already been used for division}}

void err_yes(int x) {
  var = 77 / x; // expected-note {{Division with compared value made here}}
  if (x) {} // expected-warning {{Value being compared against zero has already been used for division}}
} // expected-note@-1 {{Value being compared against zero has already been used for division}}
void err_not(int x) {
  var = 77 / x; // expected-note {{Division with compared value made here}}
  if (!x) {} // expected-warning {{Value being compared against zero has already been used for division}}
} // expected-note@-1 {{Value being compared against zero has already been used for division}}

void err_pnot(int x) {
  int *y = &x;
  var = 77 / *y; // expected-note {{Division with compared value made here}}
  if (!x) {} // expected-warning {{Value being compared against zero has already been used for division}}
} // expected-note@-1 {{Value being compared against zero has already been used for division}}

void err_pnot2(int x) {
  int *y = &x;
  var = 77 / x; // expected-note {{Division with compared value made here}}
  if (!*y) {} // expected-warning {{Value being compared against zero has already been used for division}}
} // expected-note@-1 {{Value being compared against zero has already been used for division}}

void err_ppnot(int x) {
  int *y = &x;
  int **z = &y;
  var = 77 / **z; // expected-note {{Division with compared value made here}}
  if (!x) {} // expected-warning {{Value being compared against zero has already been used for division}}
} // expected-note@-1 {{Value being compared against zero has already been used for division}}

void err_orig_checker(int x) {
  if (x != 0) // expected-note {{Assuming 'x' is equal to 0}} expected-note {{Taking false branch}}
    return;
  var = 77 / x; // expected-warning {{Division by zero}} expected-note {{Division by zero}}
  if (!x) {} // no-warning
}

void ok_other(int x, int y) {
  var = 77 / y;
  if (x == 0) {
  }
}

void ok_assign(int x) {
  var = 77 / x;
  x = var / 77; // <- assignment => don't warn
  if (x == 0) {
  }
}

void ok_assign2(int x) {
  var = 77 / x;
  x = var / 77; // <- assignment => don't warn
  if (0 == x) {
  }
}

void ok_dec(int x) {
  var = 77 / x;
  x--; // <- assignment => don't warn
  if (x == 0) {
  }
}

void ok_inc(int x) {
  var = 77 / x;
  x++; // <- assignment => don't warn
  if (x == 0) {
  }
}

void do_something_ptr(int *x);
void ok_callfunc_ptr(int x) {
  var = 77 / x;
  do_something_ptr(&x); // <- pass address of x to function => don't warn
  if (x == 0) {
  }
}

void do_something(int x);
void nok_callfunc(int x) {
  var = 77 / x; // expected-note {{Division with compared value made here}}
  do_something(x);
  if (x == 0) {} // expected-warning {{Value being compared against zero has already been used for division}}
} // expected-note@-1 {{Value being compared against zero has already been used for division}}

void ok_if(int x) {
  if (x > 3)
    var = 77 / x;
  if (x == 0) {
  }
}

void ok_if2(int x) {
  if (x < 3)
    var = 77 / x;
  if (x == 0) {
  } // TODO warn here
}

void ok_pif(int x) {
  int *y = &x;
  if (x < 3)
    var = 77 / *y;
  if (x == 0) {
  } // TODO warn here
}

int getValue(bool *isPositive);
void use(int a);
void foo() {
  bool isPositive;
  int x = getValue(&isPositive);
  if (isPositive) {
    use(5 / x);
  }

  if (x == 0) {
  }
}

int getValue2();
void foo2() {
  int x = getValue2();
  int y = x;

  use(5 / x); // expected-note {{Division with compared value made here}}
  if (y == 0) {} // expected-warning {{Value being compared against zero has already been used for division}}
} // expected-note@-1 {{Value being compared against zero has already been used for division}}

void ok_while(int x) {
  int n = 100 / x;
  while (x != 0) { // <- do not warn
    x--;
  }
}

void err_not2(int x, int y) {
  int v;
  var = 77 / x;

  if (y)
    v = 0;

  if (!x) {
  } // TODO warn here
}

inline void inline_func(int x) {
  var = 77 / x; // expected-note {{Division with compared value made here}}
  if (x == 0) {} // expected-warning {{Value being compared against zero has already been used for division}}
} // expected-note@-1 {{Value being compared against zero has already been used for division}}

void err_inline(int x) {
  var = 77 / x;
  inline_func(x); // expected-note {{Calling 'inline_func'}}
  if (x == 0) {
  }
}

inline void inline_func2(int x) {}

void err_inline2(int x) {
  var = 77 / x; // expected-note {{Division with compared value made here}}
  inline_func2(x);
  if (x == 0) {} // expected-warning {{Value being compared against zero has already been used for division}}
} // expected-note@-1 {{Value being compared against zero has already been used for division}}

inline void inline_func3(int x) {
  var = 77 / x;
}
void ok_inline(int x) {
  var = 77 / x; // expected-note {{Division with compared value made here}}
  inline_func3(x);
  if (x == 0) {} // expected-warning {{Value being compared against zero has already been used for division}}
} // expected-note@-1 {{Value being compared against zero has already been used for division}}