| 1 |
// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core.BoolAssignment,optin.taint -verify -std=c99 -Dbool=_Bool %s |
| 2 |
// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core.BoolAssignment,optin.taint -verify -x c++ %s |
| 3 |
|
| 4 |
// Test C++'s bool and C's _Bool. |
| 5 |
// FIXME: We stopped warning on these when SValBuilder got smarter about |
| 6 |
// casts to bool. Arguably, however, these conversions are okay; the result |
| 7 |
// is always 'true' or 'false'. |
| 8 |
|
| 9 |
void test_stdbool_initialization(int y) { |
| 10 |
bool constant = 2; // no-warning |
| 11 |
if (y < 0) { |
| 12 |
bool x = y; // no-warning |
| 13 |
return; |
| 14 |
} |
| 15 |
if (y > 1) { |
| 16 |
bool x = y; // no-warning |
| 17 |
return; |
| 18 |
} |
| 19 |
bool x = y; // no-warning |
| 20 |
} |
| 21 |
|
| 22 |
void test_stdbool_assignment(int y) { |
| 23 |
bool x = 0; // no-warning |
| 24 |
if (y < 0) { |
| 25 |
x = y; // no-warning |
| 26 |
return; |
| 27 |
} |
| 28 |
if (y > 1) { |
| 29 |
x = y; // no-warning |
| 30 |
return; |
| 31 |
} |
| 32 |
x = y; // no-warning |
| 33 |
} |
| 34 |
|
| 35 |
// Test Objective-C's BOOL |
| 36 |
|
| 37 |
typedef signed char BOOL; |
| 38 |
|
| 39 |
void test_BOOL_initialization(int y) { |
| 40 |
BOOL constant = 2; // expected-warning {{Assignment of a non-Boolean value}} |
| 41 |
if (y < 0) { |
| 42 |
BOOL x = y; // expected-warning {{Assignment of a non-Boolean value}} |
| 43 |
return; |
| 44 |
} |
| 45 |
if (y > 200 && y < 250) { |
| 46 |
BOOL x = y; // no-warning |
| 47 |
return; |
| 48 |
} |
| 49 |
if (y >= 127 && y < 150) { |
| 50 |
BOOL x = y; // expected-warning{{Assignment of a non-Boolean value}} |
| 51 |
return; |
| 52 |
} |
| 53 |
if (y > 1) { |
| 54 |
BOOL x = y; // expected-warning {{Assignment of a non-Boolean value}} |
| 55 |
return; |
| 56 |
} |
| 57 |
BOOL x = y; // no-warning |
| 58 |
} |
| 59 |
|
| 60 |
void test_BOOL_assignment(int y) { |
| 61 |
BOOL x = 0; // no-warning |
| 62 |
if (y < 0) { |
| 63 |
x = y; // expected-warning {{Assignment of a non-Boolean value}} |
| 64 |
return; |
| 65 |
} |
| 66 |
if (y > 1) { |
| 67 |
x = y; // expected-warning {{Assignment of a non-Boolean value}} |
| 68 |
return; |
| 69 |
} |
| 70 |
x = y; // no-warning |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
// Test MacTypes.h's Boolean |
| 75 |
|
| 76 |
typedef unsigned char Boolean; |
| 77 |
|
| 78 |
void test_Boolean_initialization(int y) { |
| 79 |
Boolean constant = 2; // expected-warning {{Assignment of a non-Boolean value}} |
| 80 |
if (y < 0) { |
| 81 |
Boolean x = y; // expected-warning {{Assignment of a non-Boolean value}} |
| 82 |
return; |
| 83 |
} |
| 84 |
if (y > 1) { |
| 85 |
Boolean x = y; // expected-warning {{Assignment of a non-Boolean value}} |
| 86 |
return; |
| 87 |
} |
| 88 |
Boolean x = y; // no-warning |
| 89 |
} |
| 90 |
|
| 91 |
void test_Boolean_assignment(int y) { |
| 92 |
Boolean x = 0; // no-warning |
| 93 |
if (y < 0) { |
| 94 |
x = y; // expected-warning {{Assignment of a non-Boolean value}} |
| 95 |
return; |
| 96 |
} |
| 97 |
if (y > 1) { |
| 98 |
x = y; // expected-warning {{Assignment of a non-Boolean value}} |
| 99 |
return; |
| 100 |
} |
| 101 |
x = y; // no-warning |
| 102 |
} |
| 103 |
|
| 104 |
int scanf(const char *format, ...); |
| 105 |
void test_tainted_Boolean() { |
| 106 |
int n; |
| 107 |
scanf("%d", &n); |
| 108 |
Boolean copy = n; // expected-warning {{Might assign a tainted non-Boolean value}} |
| 109 |
} |