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