1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
// RUN: %clang_analyze_cc1 -x c++ -analyzer-checker=core -analyzer-output=text -verify %s
int initializer1(int &p, int x) {
if (x) { // expected-note{{Taking false branch}}
p = 1;
return 0;
} else {
return 1; // expected-note {{Returning without writing to 'p'}}
}
}
int param_not_initialized_by_func() {
int p; // expected-note {{'p' declared without an initial value}}
int out = initializer1(p, 0); // expected-note{{Calling 'initializer1'}}
// expected-note@-1{{Returning from 'initializer1'}}
return p; // expected-note{{Undefined or garbage value returned to caller}}
// expected-warning@-1{{Undefined or garbage value returned to caller}}
}
struct S {
int initialize(int *p, int param) {
if (param) { //expected-note{{Taking false branch}}
*p = 1;
return 1;
}
return 0; // expected-note{{Returning without writing to '*p'}}
}
};
int use(S *s) {
int p; //expected-note{{'p' declared without an initial value}}
s->initialize(&p, 0); //expected-note{{Calling 'S::initialize'}}
//expected-note@-1{{Returning from 'S::initialize'}}
return p; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
int initializer2(const int &p) {
return 0;
}
int no_msg_const_ref() {
int p; //expected-note{{'p' declared without an initial value}}
initializer2(p);
return p; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
void nested() {}
void init_in_nested_func(int **x) {
*x = 0; // expected-note{{Null pointer value stored to 'y'}}
nested();
} // no-note
int call_init_nested() {
int x = 0;
int *y = &x;
init_in_nested_func(&y); // expected-note{{Calling 'init_in_nested_func'}}
// expected-note@-1{{Returning from 'init_in_nested_func'}}
return *y; //expected-warning{{Dereference of null pointer (loaded from variable 'y')}}
//expected-note@-1{{Dereference of null pointer (loaded from variable 'y')}}
}
struct A {
int x;
int y;
};
void partial_init_by_reference(A &a) {
a.x = 0;
} // expected-note {{Returning without writing to 'a.y'}}
int use_partial_init_by_reference() {
A a;
partial_init_by_reference(a); // expected-note{{Calling 'partial_init_by_reference'}}
// expected-note@-1{{Returning from 'partial_init_by_reference'}}
return a.y; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
struct B : A {
};
void partially_init_inherited_struct(B *b) {
b->x = 0;
} // expected-note{{Returning without writing to 'b->y'}}
int use_partially_init_inherited_struct() {
B b;
partially_init_inherited_struct(&b); // expected-note{{Calling 'partially_init_inherited_struct'}}
// expected-note@-1{{Returning from 'partially_init_inherited_struct'}}
return b.y; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
struct C {
int x;
int y;
C(int pX, int pY) : x(pX) {} // expected-note{{Returning without writing to 'this->y'}}
C(int pX, int pY, bool Flag) {
x = pX;
if (Flag) // expected-note{{Assuming 'Flag' is not equal to 0}}
// expected-note@-1{{Taking true branch}}
return; // expected-note{{Returning without writing to 'this->y'}}
y = pY;
}
};
int use_constructor() {
C c(0, 0); // expected-note{{Calling constructor for 'C'}}
// expected-note@-1{{Returning from constructor for 'C'}}
return c.y; // expected-note{{Undefined or garbage value returned to caller}}
// expected-warning@-1{{Undefined or garbage value returned to caller}}
}
int coin();
int use_other_constructor() {
C c(0, 0, coin()); // expected-note{{Calling constructor for 'C'}}
// expected-note@-1{{Returning from constructor for 'C'}}
return c.y; // expected-note{{Undefined or garbage value returned to caller}}
// expected-warning@-1{{Undefined or garbage value returned to caller}}
}
struct D {
void initialize(int *);
};
void D::initialize(int *p) {
} // expected-note{{Returning without writing to '*p'}}
int use_d_initializer(D* d) {
int p; // expected-note {{'p' declared without an initial value}}
d->initialize(&p); // expected-note{{Calling 'D::initialize'}}
// expected-note@-1{{Returning from 'D::initialize'}}
return p; // expected-note{{Undefined or garbage value returned to caller}}
// expected-warning@-1{{Undefined or garbage value returned to caller}}
}
struct S2 {
int x;
};
int pointerreference(S2* &s) {
if (coin()) // expected-note{{Assuming the condition is true}}
// expected-note@-1{{Taking true branch}}
return 1; // expected-note{{Returning without writing to 's->x'}}
s->x = 0;
return 0;
}
int usepointerreference() {
S2 s;
S2* p = &s;
pointerreference(p); //expected-note{{Calling 'pointerreference'}}
//expected-note@-1{{Returning from 'pointerreference'}}
return s.x; // expected-warning{{Undefined or garbage value returned to caller}}
// expected-note@-1{{Undefined or garbage value returned to caller}}
}
void *has_no_argument_and_returns_null(void) {
return 0;
}
void rdar40335545() {
int local; // expected-note{{}}
void (*takes_int_ptr_argument)(int *) = (void (*)(int*))has_no_argument_and_returns_null;
takes_int_ptr_argument(&local); // no-crash
int useLocal = local; //expected-warning{{}}
//expected-note@-1{{}}
(void)useLocal;
}
|