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