Ted Kremenek | 565e465 | 2010-02-05 02:06:54 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=basic -verify %s |
| 2 | // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=range -verify %s |
| 3 | // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=basic -verify %s |
| 4 | // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -verify %s |
Zhongxing Xu | ef8b28e | 2008-10-17 05:19:52 +0000 | [diff] [blame] | 5 | |
Zhongxing Xu | 72e1682 | 2008-10-24 08:51:58 +0000 | [diff] [blame] | 6 | struct s { |
| 7 | int data; |
| 8 | int data_array[10]; |
| 9 | }; |
Zhongxing Xu | ef8b28e | 2008-10-17 05:19:52 +0000 | [diff] [blame] | 10 | |
Zhongxing Xu | 234a7d2 | 2008-10-27 09:19:25 +0000 | [diff] [blame] | 11 | typedef struct { |
| 12 | int data; |
| 13 | } STYPE; |
| 14 | |
Zhongxing Xu | 9184412 | 2009-05-20 09:18:48 +0000 | [diff] [blame] | 15 | void g(char *p); |
Zhongxing Xu | 04b90bc | 2008-11-02 13:17:44 +0000 | [diff] [blame] | 16 | void g1(struct s* p); |
| 17 | |
Zhongxing Xu | 661fc39 | 2008-11-25 01:45:11 +0000 | [diff] [blame] | 18 | // Array to pointer conversion. Array in the struct field. |
Zhongxing Xu | ef8b28e | 2008-10-17 05:19:52 +0000 | [diff] [blame] | 19 | void f(void) { |
| 20 | int a[10]; |
| 21 | int (*p)[10]; |
| 22 | p = &a; |
| 23 | (*p)[3] = 1; |
| 24 | |
| 25 | struct s d; |
| 26 | struct s *q; |
| 27 | q = &d; |
Zhongxing Xu | 72e1682 | 2008-10-24 08:51:58 +0000 | [diff] [blame] | 28 | q->data = 3; |
| 29 | d.data_array[9] = 17; |
Zhongxing Xu | ef8b28e | 2008-10-17 05:19:52 +0000 | [diff] [blame] | 30 | } |
Zhongxing Xu | 2e97120 | 2008-10-25 14:11:23 +0000 | [diff] [blame] | 31 | |
Zhongxing Xu | 661fc39 | 2008-11-25 01:45:11 +0000 | [diff] [blame] | 32 | // StringLiteral in lvalue context and pointer to array type. |
| 33 | // p: ElementRegion, q: StringRegion |
Zhongxing Xu | 2e97120 | 2008-10-25 14:11:23 +0000 | [diff] [blame] | 34 | void f2() { |
| 35 | char *p = "/usr/local"; |
| 36 | char (*q)[4]; |
| 37 | q = &"abc"; |
| 38 | } |
Zhongxing Xu | 234a7d2 | 2008-10-27 09:19:25 +0000 | [diff] [blame] | 39 | |
Zhongxing Xu | 661fc39 | 2008-11-25 01:45:11 +0000 | [diff] [blame] | 40 | // Typedef'ed struct definition. |
Zhongxing Xu | 234a7d2 | 2008-10-27 09:19:25 +0000 | [diff] [blame] | 41 | void f3() { |
| 42 | STYPE s; |
| 43 | } |
Zhongxing Xu | df2aa1e | 2008-10-31 10:23:14 +0000 | [diff] [blame] | 44 | |
Zhongxing Xu | 661fc39 | 2008-11-25 01:45:11 +0000 | [diff] [blame] | 45 | // Initialize array with InitExprList. |
Zhongxing Xu | df2aa1e | 2008-10-31 10:23:14 +0000 | [diff] [blame] | 46 | void f4() { |
| 47 | int a[] = { 1, 2, 3}; |
| 48 | int b[3] = { 1, 2 }; |
Zhongxing Xu | b61f49c | 2009-01-23 10:23:13 +0000 | [diff] [blame] | 49 | struct s c[] = {{1,{1}}}; |
Zhongxing Xu | df2aa1e | 2008-10-31 10:23:14 +0000 | [diff] [blame] | 50 | } |
Zhongxing Xu | 04b90bc | 2008-11-02 13:17:44 +0000 | [diff] [blame] | 51 | |
Zhongxing Xu | 661fc39 | 2008-11-25 01:45:11 +0000 | [diff] [blame] | 52 | // Struct variable in lvalue context. |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 53 | // Assign UnknownVal to the whole struct. |
Zhongxing Xu | 04b90bc | 2008-11-02 13:17:44 +0000 | [diff] [blame] | 54 | void f5() { |
| 55 | struct s data; |
| 56 | g1(&data); |
| 57 | } |
Zhongxing Xu | b670133 | 2008-11-13 07:59:15 +0000 | [diff] [blame] | 58 | |
Zhongxing Xu | 661fc39 | 2008-11-25 01:45:11 +0000 | [diff] [blame] | 59 | // AllocaRegion test. |
Zhongxing Xu | b670133 | 2008-11-13 07:59:15 +0000 | [diff] [blame] | 60 | void f6() { |
| 61 | char *p; |
| 62 | p = __builtin_alloca(10); |
Zhongxing Xu | 9184412 | 2009-05-20 09:18:48 +0000 | [diff] [blame] | 63 | g(p); |
| 64 | char c = *p; |
Zhongxing Xu | b670133 | 2008-11-13 07:59:15 +0000 | [diff] [blame] | 65 | p[1] = 'a'; |
Zhongxing Xu | 2acc399 | 2009-05-20 09:03:10 +0000 | [diff] [blame] | 66 | // Test if RegionStore::EvalBinOp converts the alloca region to element |
| 67 | // region. |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 68 | p += 2; |
Zhongxing Xu | b670133 | 2008-11-13 07:59:15 +0000 | [diff] [blame] | 69 | } |
Zhongxing Xu | fb75b25 | 2008-11-13 08:44:52 +0000 | [diff] [blame] | 70 | |
| 71 | struct s2; |
| 72 | |
| 73 | void g2(struct s2 *p); |
| 74 | |
Zhongxing Xu | 661fc39 | 2008-11-25 01:45:11 +0000 | [diff] [blame] | 75 | // Incomplete struct pointer used as function argument. |
Zhongxing Xu | fb75b25 | 2008-11-13 08:44:52 +0000 | [diff] [blame] | 76 | void f7() { |
| 77 | struct s2 *p = __builtin_alloca(10); |
| 78 | g2(p); |
| 79 | } |
Zhongxing Xu | 26134a1 | 2008-11-13 09:20:05 +0000 | [diff] [blame] | 80 | |
Zhongxing Xu | 661fc39 | 2008-11-25 01:45:11 +0000 | [diff] [blame] | 81 | // sizeof() is unsigned while -1 is signed in array index. |
Zhongxing Xu | 26134a1 | 2008-11-13 09:20:05 +0000 | [diff] [blame] | 82 | void f8() { |
| 83 | int a[10]; |
Zhongxing Xu | 33d7cbf | 2008-11-24 23:45:56 +0000 | [diff] [blame] | 84 | a[sizeof(a)/sizeof(int) - 1] = 1; // no-warning |
Zhongxing Xu | 26134a1 | 2008-11-13 09:20:05 +0000 | [diff] [blame] | 85 | } |
Zhongxing Xu | 617ff31 | 2008-11-18 13:30:46 +0000 | [diff] [blame] | 86 | |
Zhongxing Xu | 661fc39 | 2008-11-25 01:45:11 +0000 | [diff] [blame] | 87 | // Initialization of struct array elements. |
Zhongxing Xu | 617ff31 | 2008-11-18 13:30:46 +0000 | [diff] [blame] | 88 | void f9() { |
| 89 | struct s a[10]; |
| 90 | } |
Zhongxing Xu | 27cae9e | 2008-11-30 05:51:19 +0000 | [diff] [blame] | 91 | |
| 92 | // Initializing array with string literal. |
| 93 | void f10() { |
| 94 | char a1[4] = "abc"; |
Zhongxing Xu | 27cae9e | 2008-11-30 05:51:19 +0000 | [diff] [blame] | 95 | char a3[6] = "abc"; |
| 96 | } |
Zhongxing Xu | 562c4d9 | 2009-01-23 11:22:12 +0000 | [diff] [blame] | 97 | |
| 98 | // Retrieve the default value of element/field region. |
| 99 | void f11() { |
| 100 | struct s a; |
Zhongxing Xu | 9184412 | 2009-05-20 09:18:48 +0000 | [diff] [blame] | 101 | g1(&a); |
Zhongxing Xu | 562c4d9 | 2009-01-23 11:22:12 +0000 | [diff] [blame] | 102 | if (a.data == 0) // no-warning |
| 103 | a.data = 1; |
| 104 | } |
Zhongxing Xu | 3450a55 | 2009-02-19 08:42:43 +0000 | [diff] [blame] | 105 | |
| 106 | // Convert unsigned offset to signed when creating ElementRegion from |
| 107 | // SymbolicRegion. |
| 108 | void f12(int *list) { |
| 109 | unsigned i = 0; |
| 110 | list[i] = 1; |
| 111 | } |
Zhongxing Xu | c57bc59 | 2009-03-18 02:07:30 +0000 | [diff] [blame] | 112 | |
| 113 | struct s1 { |
| 114 | struct s2 { |
| 115 | int d; |
| 116 | } e; |
| 117 | }; |
| 118 | |
| 119 | // The binding of a.e.d should not be removed. Test recursive subregion map |
| 120 | // building: a->e, e->d. Only then 'a' could be added to live region roots. |
| 121 | void f13(double timeout) { |
| 122 | struct s1 a; |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 123 | a.e.d = (int) timeout; |
Zhongxing Xu | c57bc59 | 2009-03-18 02:07:30 +0000 | [diff] [blame] | 124 | if (a.e.d == 10) |
| 125 | a.e.d = 4; |
| 126 | } |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 127 | |
| 128 | struct s3 { |
| 129 | int a[2]; |
| 130 | }; |
| 131 | |
| 132 | static struct s3 opt; |
| 133 | |
| 134 | // Test if the embedded array is retrieved correctly. |
| 135 | void f14() { |
| 136 | struct s3 my_opt = opt; |
| 137 | } |
Zhongxing Xu | 264e937 | 2009-05-12 10:10:00 +0000 | [diff] [blame] | 138 | |
| 139 | void bar(int*); |
| 140 | |
| 141 | // Test if the array is correctly invalidated. |
| 142 | void f15() { |
| 143 | int a[10]; |
| 144 | bar(a); |
| 145 | if (a[1]) // no-warning |
Anders Carlsson | 9668b1f | 2009-07-30 22:37:41 +0000 | [diff] [blame] | 146 | (void)1; |
Zhongxing Xu | 264e937 | 2009-05-12 10:10:00 +0000 | [diff] [blame] | 147 | } |
Zhongxing Xu | 3f6978a | 2009-06-11 09:11:27 +0000 | [diff] [blame] | 148 | |
| 149 | struct s3 p[1]; |
| 150 | |
| 151 | // Code from postgresql. |
| 152 | // Current cast logic of region store mistakenly leaves the final result region |
| 153 | // an ElementRegion of type 'char'. Then load a nonloc::SymbolVal from it and |
| 154 | // assigns to 'a'. |
| 155 | void f16(struct s3 *p) { |
Zhongxing Xu | 4f3dc69 | 2009-11-09 08:07:38 +0000 | [diff] [blame] | 156 | struct s3 a = *((struct s3*) ((char*) &p[0])); // expected-warning{{Casting a non-structure type to a structure type and accessing a field can lead to memory access errors or data corruption.}} |
Zhongxing Xu | 3f6978a | 2009-06-11 09:11:27 +0000 | [diff] [blame] | 157 | } |
Zhongxing Xu | 6bd8a52 | 2009-06-28 13:59:24 +0000 | [diff] [blame] | 158 | |
| 159 | void inv(struct s1 *); |
| 160 | |
| 161 | // Invalidate the struct field. |
| 162 | void f17() { |
| 163 | struct s1 t; |
| 164 | int x; |
| 165 | inv(&t); |
| 166 | if (t.e.d) |
| 167 | x = 1; |
| 168 | } |
Zhongxing Xu | a03f157 | 2009-06-29 06:43:40 +0000 | [diff] [blame] | 169 | |
| 170 | void read(char*); |
| 171 | |
| 172 | void f18() { |
| 173 | char *q; |
| 174 | char *p = (char *) __builtin_alloca(10); |
| 175 | read(p); |
| 176 | q = p; |
| 177 | q++; |
| 178 | if (*q) { // no-warning |
| 179 | } |
| 180 | } |