2017-03-03 07:05:45 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-store=region -analyzer-checker=core -verify %s
|
2008-11-20 08:46:15 +08:00
|
|
|
|
|
|
|
struct s {
|
|
|
|
int data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct s global;
|
|
|
|
|
|
|
|
void g(int);
|
|
|
|
|
|
|
|
void f4() {
|
|
|
|
int a;
|
|
|
|
if (global.data == 0)
|
|
|
|
a = 3;
|
2008-12-04 10:07:20 +08:00
|
|
|
if (global.data == 0) // When the true branch is feasible 'a = 3'.
|
2008-11-20 08:46:15 +08:00
|
|
|
g(a); // no-warning
|
|
|
|
}
|
2009-08-29 04:25:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Test uninitialized value due to part of the structure being uninitialized.
|
|
|
|
struct TestUninit { int x; int y; };
|
|
|
|
struct TestUninit test_uninit_aux();
|
2010-01-10 04:43:19 +08:00
|
|
|
void test_unit_aux2(int);
|
2009-08-29 04:25:33 +08:00
|
|
|
void test_uninit_pos() {
|
|
|
|
struct TestUninit v1 = { 0, 0 };
|
|
|
|
struct TestUninit v2 = test_uninit_aux();
|
|
|
|
int z;
|
2009-11-04 12:24:16 +08:00
|
|
|
v1.y = z; // expected-warning{{Assigned value is garbage or undefined}}
|
|
|
|
test_unit_aux2(v2.x + v1.y);
|
2009-08-29 04:25:33 +08:00
|
|
|
}
|
2009-11-04 12:24:16 +08:00
|
|
|
void test_uninit_pos_2() {
|
|
|
|
struct TestUninit v1 = { 0, 0 };
|
|
|
|
struct TestUninit v2;
|
|
|
|
test_unit_aux2(v2.x + v1.y); // expected-warning{{The left operand of '+' is a garbage value}}
|
|
|
|
}
|
|
|
|
void test_uninit_pos_3() {
|
|
|
|
struct TestUninit v1 = { 0, 0 };
|
|
|
|
struct TestUninit v2;
|
|
|
|
test_unit_aux2(v1.y + v2.x); // expected-warning{{The right operand of '+' is a garbage value}}
|
|
|
|
}
|
|
|
|
|
2009-08-29 04:25:33 +08:00
|
|
|
void test_uninit_neg() {
|
|
|
|
struct TestUninit v1 = { 0, 0 };
|
|
|
|
struct TestUninit v2 = test_uninit_aux();
|
2014-01-04 13:52:11 +08:00
|
|
|
test_unit_aux2(v2.x + v1.y);
|
2009-08-29 04:25:33 +08:00
|
|
|
}
|
|
|
|
|
2010-03-18 10:17:27 +08:00
|
|
|
extern void test_uninit_struct_arg_aux(struct TestUninit arg);
|
|
|
|
void test_uninit_struct_arg() {
|
|
|
|
struct TestUninit x;
|
|
|
|
test_uninit_struct_arg_aux(x); // expected-warning{{Passed-by-value struct argument contains uninitialized data (e.g., field: 'x')}}
|
|
|
|
}
|
|
|
|
|
2010-03-18 11:22:29 +08:00
|
|
|
@interface Foo
|
|
|
|
- (void) passVal:(struct TestUninit)arg;
|
|
|
|
@end
|
|
|
|
void testFoo(Foo *o) {
|
|
|
|
struct TestUninit x;
|
|
|
|
[o passVal:x]; // expected-warning{{Passed-by-value struct argument contains uninitialized data (e.g., field: 'x')}}
|
|
|
|
}
|
|
|
|
|
2010-03-23 06:16:26 +08:00
|
|
|
// Test case from <rdar://problem/7780304>. That shows an uninitialized value
|
|
|
|
// being used in the LHS of a compound assignment.
|
|
|
|
void rdar_7780304() {
|
|
|
|
typedef struct s_r7780304 { int x; } s_r7780304;
|
|
|
|
s_r7780304 b;
|
|
|
|
b.x |= 1; // expected-warning{{The left expression of the compound assignment is an uninitialized value. The computed value will also be garbage}}
|
|
|
|
}
|
2010-03-18 11:22:29 +08:00
|
|
|
|
2011-06-28 04:36:38 +08:00
|
|
|
|
|
|
|
// The flip side of PR10163 -- float arrays that are actually uninitialized
|
|
|
|
// (The main test is in uninit-vals.m)
|
|
|
|
void test_PR10163(float);
|
|
|
|
void PR10163 (void) {
|
|
|
|
float x[2];
|
|
|
|
test_PR10163(x[1]); // expected-warning{{uninitialized value}}
|
|
|
|
}
|
|
|
|
|
2013-06-19 07:16:15 +08:00
|
|
|
struct MyStr {
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
};
|
|
|
|
void swap(struct MyStr *To, struct MyStr *From) {
|
|
|
|
// This is not really a swap but close enough for our test.
|
|
|
|
To->x = From->x;
|
|
|
|
To->y = From->y; // no warning
|
|
|
|
}
|
|
|
|
int test_undefined_member_assignment_in_swap(struct MyStr *s2) {
|
|
|
|
struct MyStr s1;
|
|
|
|
s1.x = 5;
|
|
|
|
swap(s2, &s1);
|
|
|
|
return s2->y; // expected-warning{{Undefined or garbage value returned to caller}}
|
|
|
|
}
|