Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/taint-divisor-1.c

raw
1 #include "analyzer-decls.h"
2 #include <stdio.h>
3
4 struct st1
5 {
6 int a;
7 int b;
8 };
9
10
11 int test_1 (FILE *f)
12 {
13 struct st1 s;
14 fread (&s, sizeof (s), 1, f);
15 return s.a / s.b; /* { dg-warning "use of attacker-controlled value 's\\.b' as divisor without checking for zero" } */
16 }
17
18 int test_2 (FILE *f)
19 {
20 struct st1 s;
21 fread (&s, sizeof (s), 1, f);
22 return s.a % s.b; /* { dg-warning "use of attacker-controlled value 's\\.b' as divisor without checking for zero" } */
23 }
24
25 /* We shouldn't complain if the divisor has been checked for zero. */
26
27 int test_checked_ne_zero (FILE *f)
28 {
29 struct st1 s;
30 fread (&s, sizeof (s), 1, f);
31 if (s.b)
32 return s.a / s.b; /* { dg-bogus "divisor" } */
33 else
34 return 0;
35 }
36
37 int test_checked_gt_zero (FILE *f)
38 {
39 struct st1 s;
40 fread (&s, sizeof (s), 1, f);
41 if (s.b > 0)
42 return s.a / s.b; /* { dg-bogus "divisor" } */
43 else
44 return 0;
45 }
46
47 int test_checked_lt_zero (FILE *f)
48 {
49 struct st1 s;
50 fread (&s, sizeof (s), 1, f);
51 if (s.b < 0)
52 return s.a / s.b; /* { dg-bogus "divisor" } */
53 else
54 return 0;
55 }
56
57 /* We should complain if the check on the divisor still allows it to be
58 zero. */
59
60 int test_checked_ge_zero (FILE *f)
61 {
62 struct st1 s;
63 fread (&s, sizeof (s), 1, f);
64 if (s.b >= 0)
65 return s.a / s.b; /* { dg-warning "use of attacker-controlled value 's\\.b' as divisor without checking for zero" } */
66 else
67 return 0;
68 }
69
70 int test_checked_le_zero (FILE *f)
71 {
72 struct st1 s;
73 fread (&s, sizeof (s), 1, f);
74 if (s.b <= 0)
75 return s.a / s.b; /* { dg-warning "use of attacker-controlled value 's\\.b' as divisor without checking for zero" } */
76 else
77 return 0;
78 }
79
80 int test_checked_eq_zero (FILE *f)
81 {
82 struct st1 s;
83 fread (&s, sizeof (s), 1, f);
84 /* Wrong sense of test. */
85 if (s.b != 0)
86 return 0;
87 else
88 return s.a / s.b; /* { dg-warning "use of attacker-controlled value 's\\.b' as divisor without checking for zero" } */
89 }