Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-store=region -analyzer-checker=core -verify %s |
Zhongxing Xu | e8e4d8c | 2008-11-20 00:46:15 +0000 | [diff] [blame] | 2 | |
| 3 | struct s { |
| 4 | int data; |
| 5 | }; |
| 6 | |
| 7 | struct s global; |
| 8 | |
| 9 | void g(int); |
| 10 | |
| 11 | void f4() { |
| 12 | int a; |
| 13 | if (global.data == 0) |
| 14 | a = 3; |
Ted Kremenek | 4226846 | 2008-12-04 02:07:20 +0000 | [diff] [blame] | 15 | if (global.data == 0) // When the true branch is feasible 'a = 3'. |
Zhongxing Xu | e8e4d8c | 2008-11-20 00:46:15 +0000 | [diff] [blame] | 16 | g(a); // no-warning |
| 17 | } |
Ted Kremenek | c761f40 | 2009-08-28 20:25:33 +0000 | [diff] [blame] | 18 | |
| 19 | |
| 20 | // Test uninitialized value due to part of the structure being uninitialized. |
| 21 | struct TestUninit { int x; int y; }; |
| 22 | struct TestUninit test_uninit_aux(); |
Chris Lattner | e030358 | 2010-01-09 20:43:19 +0000 | [diff] [blame] | 23 | void test_unit_aux2(int); |
Ted Kremenek | c761f40 | 2009-08-28 20:25:33 +0000 | [diff] [blame] | 24 | void test_uninit_pos() { |
| 25 | struct TestUninit v1 = { 0, 0 }; |
| 26 | struct TestUninit v2 = test_uninit_aux(); |
| 27 | int z; |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 28 | v1.y = z; // expected-warning{{Assigned value is garbage or undefined}} |
| 29 | test_unit_aux2(v2.x + v1.y); |
Ted Kremenek | c761f40 | 2009-08-28 20:25:33 +0000 | [diff] [blame] | 30 | } |
Ted Kremenek | b107c4b | 2009-11-04 04:24:16 +0000 | [diff] [blame] | 31 | void test_uninit_pos_2() { |
| 32 | struct TestUninit v1 = { 0, 0 }; |
| 33 | struct TestUninit v2; |
| 34 | test_unit_aux2(v2.x + v1.y); // expected-warning{{The left operand of '+' is a garbage value}} |
| 35 | } |
| 36 | void test_uninit_pos_3() { |
| 37 | struct TestUninit v1 = { 0, 0 }; |
| 38 | struct TestUninit v2; |
| 39 | test_unit_aux2(v1.y + v2.x); // expected-warning{{The right operand of '+' is a garbage value}} |
| 40 | } |
| 41 | |
Ted Kremenek | c761f40 | 2009-08-28 20:25:33 +0000 | [diff] [blame] | 42 | void test_uninit_neg() { |
| 43 | struct TestUninit v1 = { 0, 0 }; |
| 44 | struct TestUninit v2 = test_uninit_aux(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 45 | test_unit_aux2(v2.x + v1.y); |
Ted Kremenek | c761f40 | 2009-08-28 20:25:33 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 48 | extern void test_uninit_struct_arg_aux(struct TestUninit arg); |
| 49 | void test_uninit_struct_arg() { |
| 50 | struct TestUninit x; |
| 51 | test_uninit_struct_arg_aux(x); // expected-warning{{Passed-by-value struct argument contains uninitialized data (e.g., field: 'x')}} |
| 52 | } |
| 53 | |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 54 | @interface Foo |
| 55 | - (void) passVal:(struct TestUninit)arg; |
| 56 | @end |
| 57 | void testFoo(Foo *o) { |
| 58 | struct TestUninit x; |
| 59 | [o passVal:x]; // expected-warning{{Passed-by-value struct argument contains uninitialized data (e.g., field: 'x')}} |
| 60 | } |
| 61 | |
Ted Kremenek | 12182a0 | 2010-03-22 22:16:26 +0000 | [diff] [blame] | 62 | // Test case from <rdar://problem/7780304>. That shows an uninitialized value |
| 63 | // being used in the LHS of a compound assignment. |
| 64 | void rdar_7780304() { |
| 65 | typedef struct s_r7780304 { int x; } s_r7780304; |
| 66 | s_r7780304 b; |
| 67 | b.x |= 1; // expected-warning{{The left expression of the compound assignment is an uninitialized value. The computed value will also be garbage}} |
| 68 | } |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 69 | |
Jordy Rose | eda3687 | 2011-06-27 20:36:38 +0000 | [diff] [blame] | 70 | |
| 71 | // The flip side of PR10163 -- float arrays that are actually uninitialized |
| 72 | // (The main test is in uninit-vals.m) |
| 73 | void test_PR10163(float); |
| 74 | void PR10163 (void) { |
| 75 | float x[2]; |
| 76 | test_PR10163(x[1]); // expected-warning{{uninitialized value}} |
| 77 | } |
| 78 | |
Anna Zaks | 52810c5 | 2013-06-18 23:16:15 +0000 | [diff] [blame] | 79 | struct MyStr { |
| 80 | int x; |
| 81 | int y; |
| 82 | }; |
| 83 | void swap(struct MyStr *To, struct MyStr *From) { |
| 84 | // This is not really a swap but close enough for our test. |
| 85 | To->x = From->x; |
| 86 | To->y = From->y; // no warning |
| 87 | } |
| 88 | int test_undefined_member_assignment_in_swap(struct MyStr *s2) { |
| 89 | struct MyStr s1; |
| 90 | s1.x = 5; |
| 91 | swap(s2, &s1); |
| 92 | return s2->y; // expected-warning{{Undefined or garbage value returned to caller}} |
| 93 | } |