analyzer: add heuristics for switch on enum type [PR105273]
[platform/upstream/gcc.git] / gcc / testsuite / gcc.dg / analyzer / switch-enum-taint-1.c
1 // TODO: remove need for this option
2 /* { dg-additional-options "-fanalyzer-checker=taint" } */
3
4 #include "analyzer-decls.h"
5
6 /* Verify the handling of "switch (enum_value)".  */
7
8 enum e
9 {
10  E_VAL0,
11  E_VAL1,
12  E_VAL2
13 };
14
15 /* Verify that we consider that "switch (enum)" could follow implicit
16    "default" even when all enum values have cases if the value is
17    attacker-controlled.  */
18
19 int  __attribute__((tainted_args))
20 test_all_values_covered_implicit_default_1 (enum e x)
21 {
22   switch (x) /* { dg-message "following 'default:' branch" } */
23     {
24     case E_VAL0:
25       return 1066;
26     case E_VAL1:
27       return 1776;
28     case E_VAL2:
29       return 1945;
30     }
31   __analyzer_dump_path (); /* { dg-message "path" } */
32 }
33
34 int  __attribute__((tainted_args))
35 test_all_values_covered_implicit_default_2 (enum e x)
36 {
37   int result;
38   switch (x) /* { dg-message "following 'default:' branch" } */
39     {
40     case E_VAL0:
41       result = 1066;
42       break;
43     case E_VAL1:
44       result = 1776;
45       break;
46     case E_VAL2:
47       result = 1945;
48       break;
49     }
50   return result; /* { dg-message "uninitialized" } */
51 }
52
53 /* Verify that explicit "default" isn't rejected.  */
54
55 int __attribute__((tainted_args))
56 test_all_values_covered_explicit_default_1 (enum e x)
57 {
58   switch (x)
59     {
60     case E_VAL0:
61       return 1066;
62     case E_VAL1:
63       return 1776;
64     case E_VAL2:
65       return 1945;
66     default:
67       __analyzer_dump_path (); /* { dg-message "path" } */
68       return 0;
69     }
70 }
71
72 int  __attribute__((tainted_args))
73 test_missing_values_explicit_default_1 (enum e x)
74 {
75   switch (x)
76     {
77     default:
78     case E_VAL0:
79       return 1066;
80     case E_VAL1:
81       return 1776;
82     }
83   __analyzer_dump_path (); /* { dg-bogus "path" } */
84   return 0;
85 }
86
87 int __attribute__((tainted_args))
88 test_missing_values_explicit_default_2 (enum e x)
89 {
90   switch (x)
91     {
92     case E_VAL0:
93       return 1066;
94     case E_VAL1:
95       return 1776;
96     default:
97       __analyzer_dump_path (); /* { dg-message "path" } */
98       return 1945;
99     }
100   __analyzer_dump_path (); /* { dg-bogus "path" } */
101   return 0;
102 }