Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/torture/taint-read-index-2.c

raw
1 /* { dg-skip-if "" { *-*-* } { "-fno-fat-lto-objects" } { "" } } */
2
3 #define LOWER_LIMIT 5
4 #define UPPER_LIMIT 20
5
6 static int arr[UPPER_LIMIT];
7
8 static int
9 called_by_test_1 (int iarg)
10 {
11 return arr[iarg]; /* { dg-warning "without bounds checking" } */
12 }
13
14 int __attribute__((tainted_args))
15 test_1 (unsigned long ularg)
16 {
17 return called_by_test_1 (ularg);
18 }
19
20 static int
21 called_by_test_2 (int iarg)
22 {
23 if (iarg < LOWER_LIMIT || iarg > UPPER_LIMIT)
24 return 0;
25 return arr[iarg]; /* { dg-bogus "bounds checking" } */
26 }
27
28 int __attribute__((tainted_args))
29 test_2 (unsigned long ularg)
30 {
31 return called_by_test_2 (ularg);
32 }
33
34 int __attribute__((tainted_args))
35 test_3 (int iarg)
36 {
37 if (iarg < LOWER_LIMIT || iarg > UPPER_LIMIT)
38 return 0;
39 return arr[iarg]; /* { dg-bogus "bounds checking" } */
40 }
41
42 static int
43 called_by_test_4 (int iarg)
44 {
45 if (iarg < LOWER_LIMIT || iarg > UPPER_LIMIT)
46 return 0;
47 return arr[iarg]; /* { dg-bogus "bounds checking" } */
48 }
49
50 int __attribute__((tainted_args))
51 test_4 (unsigned uarg)
52 {
53 return called_by_test_4 (uarg);
54 }
55
56 int __attribute__((tainted_args))
57 test_5 (int idx)
58 {
59 switch (idx)
60 {
61 default:
62 return 0;
63 case 5 ... 20:
64 return arr[idx]; /* { dg-bogus "bounds checking" } */
65 /* 20 is still an out-of-bounds error (off-by-one)
66 but we don't check for that, just that bounds have been imposed. */
67
68 /* Extra cases to avoid optimizing the switch away. */
69 case 22:
70 return 22;
71 case 23:
72 return -17;
73 }
74 }
75
76 int __attribute__((tainted_args))
77 test_6 (int idx)
78 {
79 switch (idx)
80 {
81 default:
82 return arr[idx]; /* { dg-warning "without bounds checking" } */
83
84 case 2:
85 return arr[idx]; /* { dg-bogus "bounds checking" } */
86
87 case 6 ... 19:
88 return arr[idx]; /* { dg-bogus "bounds checking" } */
89
90 case 22:
91 return 22;
92 case 23:
93 return -17;
94 }
95 }
96
97 int __attribute__((tainted_args))
98 test_7 (int idx)
99 {
100 switch (idx)
101 {
102 default:
103 return arr[idx]; /* { dg-warning "without bounds checking" } */
104
105 case 2 ... 4:
106 case 7 ... 9:
107 return arr[idx]; /* { dg-bogus "bounds checking" } */
108
109 case 12 ... 19:
110 return arr[idx]; /* { dg-bogus "bounds checking" } */
111
112 case 22:
113 return 22;
114 case 23:
115 return -17;
116 }
117 }
118
119 int __attribute__((tainted_args))
120 test_8 (unsigned idx)
121 {
122 switch (idx)
123 {
124 default:
125 return arr[idx]; /* { dg-warning "without upper-bounds checking" } */
126
127 case 2 ... 4:
128 case 7 ... 9:
129 return arr[idx]; /* { dg-bogus "bounds checking" } */
130
131 case 12 ... 19:
132 return arr[idx]; /* { dg-bogus "bounds checking" } */
133
134 case 22:
135 return 22;
136 case 23:
137 return -17;
138 }
139 }