Ted Kremenek | e0a5807 | 2009-09-18 22:37:37 +0000 | [diff] [blame] | 1 | // NOTE: Use '-fobjc-gc' to test the analysis being run twice, and multiple reports are not issued. |
Ted Kremenek | a8180e5 | 2012-01-20 06:00:17 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,experimental.deadcode.IdempotentOperations,experimental.core,osx.cocoa.AtSync -analyzer-store=region -analyzer-constraints=basic -verify -fblocks -Wno-unreachable-code -Wno-null-dereference %s |
| 3 | // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,experimental.deadcode.IdempotentOperations,experimental.core,osx.cocoa.AtSync -analyzer-store=region -analyzer-constraints=range -verify -fblocks -Wno-unreachable-code -Wno-null-dereference %s |
| 4 | // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,experimental.deadcode.IdempotentOperations,experimental.core,osx.cocoa.AtSync -analyzer-store=region -analyzer-constraints=basic -verify -fblocks -Wno-unreachable-code -Wno-null-dereference %s |
| 5 | // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,experimental.deadcode.IdempotentOperations,experimental.core,osx.cocoa.AtSync -analyzer-store=region -analyzer-constraints=range -verify -fblocks -Wno-unreachable-code -Wno-null-dereference %s |
Ted Kremenek | 2dabd42 | 2009-01-22 18:53:15 +0000 | [diff] [blame] | 6 | |
Ted Kremenek | f0af777 | 2010-05-26 21:36:54 +0000 | [diff] [blame] | 7 | #ifndef __clang_analyzer__ |
Dylan Noblesmith | b44b966 | 2011-12-18 18:07:42 +0000 | [diff] [blame] | 8 | #error __clang_analyzer__ not defined |
Ted Kremenek | f0af777 | 2010-05-26 21:36:54 +0000 | [diff] [blame] | 9 | #endif |
| 10 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 11 | typedef struct objc_ivar *Ivar; |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 12 | typedef struct objc_selector *SEL; |
| 13 | typedef signed char BOOL; |
| 14 | typedef int NSInteger; |
| 15 | typedef unsigned int NSUInteger; |
| 16 | typedef struct _NSZone NSZone; |
Ted Kremenek | 5997888 | 2009-07-08 22:42:46 +0000 | [diff] [blame] | 17 | @class NSInvocation, NSArray, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 18 | @protocol NSObject |
| 19 | - (BOOL)isEqual:(id)object; |
| 20 | - (id)autorelease; |
| 21 | @end |
| 22 | @protocol NSCopying |
| 23 | - (id)copyWithZone:(NSZone *)zone; |
| 24 | @end |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 25 | @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 26 | @protocol NSCoding |
| 27 | - (void)encodeWithCoder:(NSCoder *)aCoder; |
| 28 | @end |
| 29 | @interface NSObject <NSObject> {} |
| 30 | - (id)init; |
| 31 | + (id)allocWithZone:(NSZone *)zone; |
| 32 | @end |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 33 | extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); |
| 34 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> |
| 35 | - (NSUInteger)length; |
| 36 | + (id)stringWithUTF8String:(const char *)nullTerminatedCString; |
| 37 | @end extern NSString * const NSBundleDidLoadNotification; |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 38 | @interface NSValue : NSObject <NSCopying, NSCoding> |
| 39 | - (void)getValue:(void *)value; |
| 40 | @end |
| 41 | @interface NSNumber : NSValue |
| 42 | - (char)charValue; |
| 43 | - (id)initWithBool:(BOOL)value; |
| 44 | @end |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 45 | @interface NSAssertionHandler : NSObject {} |
| 46 | + (NSAssertionHandler *)currentHandler; |
| 47 | - (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...; |
| 48 | @end |
| 49 | extern NSString * const NSConnectionReplyMode; |
Ted Kremenek | 693de5d | 2009-03-23 15:42:58 +0000 | [diff] [blame] | 50 | typedef float CGFloat; |
| 51 | typedef struct _NSPoint { |
| 52 | CGFloat x; |
| 53 | CGFloat y; |
| 54 | } NSPoint; |
| 55 | typedef struct _NSSize { |
| 56 | CGFloat width; |
| 57 | CGFloat height; |
| 58 | } NSSize; |
| 59 | typedef struct _NSRect { |
| 60 | NSPoint origin; |
| 61 | NSSize size; |
| 62 | } NSRect; |
Ted Kremenek | 9f67ede | 2008-10-01 05:05:46 +0000 | [diff] [blame] | 63 | |
| 64 | // Reduced test case from crash in <rdar://problem/6253157> |
Ted Kremenek | 9f67ede | 2008-10-01 05:05:46 +0000 | [diff] [blame] | 65 | @interface A @end |
| 66 | @implementation A |
| 67 | - (void)foo:(void (^)(NSObject *x))block { |
| 68 | if (!((block != ((void *)0)))) {} |
| 69 | } |
| 70 | @end |
| 71 | |
Ted Kremenek | 6dfe2f5 | 2008-10-18 22:20:20 +0000 | [diff] [blame] | 72 | // Reduced test case from crash in PR 2796; |
| 73 | // http://llvm.org/bugs/show_bug.cgi?id=2796 |
| 74 | |
| 75 | unsigned foo(unsigned x) { return __alignof__((x)) + sizeof(x); } |
Ted Kremenek | 9253b0f | 2008-10-20 23:14:31 +0000 | [diff] [blame] | 76 | |
| 77 | // Improvement to path-sensitivity involving compound assignments. |
| 78 | // Addresses false positive in <rdar://problem/6268365> |
| 79 | // |
| 80 | |
| 81 | unsigned r6268365Aux(); |
| 82 | |
| 83 | void r6268365() { |
| 84 | unsigned x = 0; |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 85 | x &= r6268365Aux(); // expected-warning{{The left operand to '&=' is always 0}} |
Ted Kremenek | 9253b0f | 2008-10-20 23:14:31 +0000 | [diff] [blame] | 86 | unsigned j = 0; |
| 87 | |
| 88 | if (x == 0) ++j; |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 89 | if (x == 0) x = x / j; // expected-warning{{Assigned value is always the same as the existing value}} expected-warning{{The right operand to '/' is always 1}} |
Ted Kremenek | 9253b0f | 2008-10-20 23:14:31 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Ted Kremenek | c13b6e2 | 2008-10-20 23:40:25 +0000 | [diff] [blame] | 92 | void divzeroassume(unsigned x, unsigned j) { |
| 93 | x /= j; |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 94 | if (j == 0) x /= 0; // no static-analyzer warning expected-warning {{division by zero is undefined}} |
| 95 | if (j == 0) x /= j; // no static-analyzer warning |
| 96 | if (j == 0) x = x / 0; // no static-analyzer warning expected-warning {{division by zero is undefined}} |
Ted Kremenek | c13b6e2 | 2008-10-20 23:40:25 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void divzeroassumeB(unsigned x, unsigned j) { |
| 100 | x = x / j; |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 101 | if (j == 0) x /= 0; // no static-analyzer warning expected-warning {{division by zero is undefined}} |
| 102 | if (j == 0) x /= j; // no static-analyzer warning |
| 103 | if (j == 0) x = x / 0; // no static-analyzer warning expected-warning {{division by zero is undefined}} |
Ted Kremenek | c13b6e2 | 2008-10-20 23:40:25 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Ted Kremenek | 76dba7b | 2008-11-13 05:05:34 +0000 | [diff] [blame] | 106 | // InitListExpr processing |
| 107 | |
| 108 | typedef float __m128 __attribute__((__vector_size__(16), __may_alias__)); |
| 109 | __m128 return128() { |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 110 | // This compound literal has a Vector type. We currently just |
| 111 | // return UnknownVal. |
Ted Kremenek | 76dba7b | 2008-11-13 05:05:34 +0000 | [diff] [blame] | 112 | return __extension__(__m128) { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 113 | } |
| 114 | |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 115 | typedef long long __v2di __attribute__ ((__vector_size__ (16))); |
| 116 | typedef long long __m128i __attribute__ ((__vector_size__ (16), __may_alias__)); |
| 117 | __m128i vec128i(long long __q1, long long __q0) { |
| 118 | // This compound literal returns true for both isVectorType() and |
| 119 | // isIntegerType(). |
| 120 | return __extension__ (__m128i)(__v2di){ __q0, __q1 }; |
| 121 | } |
| 122 | |
Ted Kremenek | 8322d6a | 2008-12-09 00:14:48 +0000 | [diff] [blame] | 123 | // Zero-sized VLAs. |
| 124 | void check_zero_sized_VLA(int x) { |
| 125 | if (x) |
| 126 | return; |
| 127 | |
Ted Kremenek | ae78447 | 2009-11-05 08:30:12 +0000 | [diff] [blame] | 128 | int vla[x]; // expected-warning{{Declared variable-length array (VLA) has zero size}} |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void check_uninit_sized_VLA() { |
| 132 | int x; |
Ted Kremenek | ae78447 | 2009-11-05 08:30:12 +0000 | [diff] [blame] | 133 | int vla[x]; // expected-warning{{Declared variable-length array (VLA) uses a garbage value as its size}} |
Ted Kremenek | 8322d6a | 2008-12-09 00:14:48 +0000 | [diff] [blame] | 134 | } |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | 55f7bcb | 2008-12-15 18:51:00 +0000 | [diff] [blame] | 136 | // sizeof(void) |
| 137 | // - Tests a regression reported in PR 3211: http://llvm.org/bugs/show_bug.cgi?id=3211 |
| 138 | void handle_sizeof_void(unsigned flag) { |
| 139 | int* p = 0; |
| 140 | |
| 141 | if (flag) { |
| 142 | if (sizeof(void) == 1) |
| 143 | return; |
| 144 | // Infeasible. |
| 145 | *p = 1; // no-warning |
| 146 | } |
| 147 | |
| 148 | void* q; |
| 149 | |
| 150 | if (!flag) { |
| 151 | if (sizeof(*q) == 1) |
| 152 | return; |
| 153 | // Infeasibe. |
| 154 | *p = 1; // no-warning |
| 155 | } |
| 156 | |
| 157 | // Infeasible. |
| 158 | *p = 1; // no-warning |
| 159 | } |
| 160 | |
Ted Kremenek | 3f214b3 | 2009-10-29 05:33:39 +0000 | [diff] [blame] | 161 | // check deference of undefined values |
| 162 | void check_deref_undef(void) { |
| 163 | int *p; |
| 164 | *p = 0xDEADBEEF; // expected-warning{{Dereference of undefined pointer value}} |
| 165 | } |
| 166 | |
Ted Kremenek | d76d47e | 2009-01-27 18:29:03 +0000 | [diff] [blame] | 167 | // PR 3422 |
| 168 | void pr3422_helper(char *p); |
| 169 | void pr3422() { |
| 170 | char buf[100]; |
| 171 | char *q = &buf[10]; |
| 172 | pr3422_helper(&q[1]); |
| 173 | } |
| 174 | |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 175 | // PR 3543 (handle empty statement expressions) |
Mike Stump | 006105d | 2009-07-21 18:59:16 +0000 | [diff] [blame] | 176 | void pr_3543(void) { |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 177 | ({}); |
| 178 | } |
| 179 | |
Ted Kremenek | 265a305 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 180 | // <rdar://problem/6611677> |
| 181 | // This test case test the use of a vector type within an array subscript |
| 182 | // expression. |
| 183 | typedef long long __a64vector __attribute__((__vector_size__(8))); |
| 184 | typedef long long __a128vector __attribute__((__vector_size__(16))); |
| 185 | static inline __a64vector __attribute__((__always_inline__, __nodebug__)) |
| 186 | my_test_mm_movepi64_pi64(__a128vector a) { |
| 187 | return (__a64vector)a[0]; |
| 188 | } |
| 189 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 190 | // Test basic tracking of ivars associated with 'self'. |
| 191 | @interface SelfIvarTest : NSObject { |
| 192 | int flag; |
| 193 | } |
| 194 | - (void)test_self_tracking; |
| 195 | @end |
| 196 | |
| 197 | @implementation SelfIvarTest |
| 198 | - (void)test_self_tracking { |
| 199 | char *p = 0; |
| 200 | char c; |
| 201 | |
| 202 | if (flag) |
| 203 | p = "hello"; |
| 204 | |
| 205 | if (flag) |
| 206 | c = *p; // no-warning |
| 207 | } |
| 208 | @end |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 209 | |
Ted Kremenek | 7de20fe | 2009-03-11 02:29:48 +0000 | [diff] [blame] | 210 | // PR 3770 |
| 211 | char pr3770(int x) { |
| 212 | int y = x & 0x2; |
| 213 | char *p = 0; |
| 214 | if (y == 1) |
| 215 | p = "hello"; |
| 216 | |
| 217 | if (y == 1) |
| 218 | return p[0]; // no-warning |
| 219 | |
| 220 | return 'a'; |
| 221 | } |
| 222 | |
Ted Kremenek | 344d4c8 | 2009-03-11 18:17:16 +0000 | [diff] [blame] | 223 | // PR 3772 |
Ted Kremenek | fa6228d | 2009-03-11 02:52:39 +0000 | [diff] [blame] | 224 | // - We just want to test that this doesn't crash the analyzer. |
| 225 | typedef struct st ST; |
| 226 | struct st { char *name; }; |
| 227 | extern ST *Cur_Pu; |
| 228 | |
Ted Kremenek | 344d4c8 | 2009-03-11 18:17:16 +0000 | [diff] [blame] | 229 | void pr3772(void) |
Ted Kremenek | fa6228d | 2009-03-11 02:52:39 +0000 | [diff] [blame] | 230 | { |
| 231 | static ST *last_Cur_Pu; |
| 232 | if (last_Cur_Pu == Cur_Pu) { |
| 233 | return; |
| 234 | } |
| 235 | } |
| 236 | |
Ted Kremenek | 344d4c8 | 2009-03-11 18:17:16 +0000 | [diff] [blame] | 237 | // PR 3780 - This tests that StmtIterator isn't broken for VLAs in DeclGroups. |
| 238 | void pr3780(int sz) { typedef double MAT[sz][sz]; } |
Ted Kremenek | fa6228d | 2009-03-11 02:52:39 +0000 | [diff] [blame] | 239 | |
Ted Kremenek | ec099f1 | 2009-03-18 22:10:22 +0000 | [diff] [blame] | 240 | // <rdar://problem/6695527> - Test that we don't symbolicate doubles before |
| 241 | // we are ready to do something with them. |
| 242 | int rdar6695527(double x) { |
| 243 | if (!x) { return 0; } |
| 244 | return 1; |
| 245 | } |
Ted Kremenek | 693de5d | 2009-03-23 15:42:58 +0000 | [diff] [blame] | 246 | |
| 247 | // <rdar://problem/6708148> - Test that we properly invalidate structs |
| 248 | // passed-by-reference to a function. |
| 249 | void pr6708148_invalidate(NSRect *x); |
| 250 | void pr6708148_use(NSRect x); |
| 251 | void pr6708148_test(void) { |
| 252 | NSRect x; |
| 253 | pr6708148_invalidate(&x); |
| 254 | pr6708148_use(x); // no-warning |
| 255 | } |
| 256 | |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 257 | // Handle both kinds of noreturn attributes for pruning paths. |
| 258 | void rdar_6777003_noret() __attribute__((noreturn)); |
| 259 | void rdar_6777003_analyzer_noret() __attribute__((analyzer_noreturn)); |
| 260 | |
| 261 | void rdar_6777003(int x) { |
| 262 | int *p = 0; |
| 263 | |
| 264 | if (x == 1) { |
| 265 | rdar_6777003_noret(); |
| 266 | *p = 1; // no-warning; |
| 267 | } |
| 268 | |
| 269 | if (x == 2) { |
| 270 | rdar_6777003_analyzer_noret(); |
| 271 | *p = 1; // no-warning; |
| 272 | } |
| 273 | |
| 274 | *p = 1; // expected-warning{{Dereference of null pointer}} |
| 275 | } |
| 276 | |
Anna Zaks | 2cbe791 | 2011-12-20 22:35:30 +0000 | [diff] [blame] | 277 | // Check that the pointer-to-conts arguments do not get invalidated by Obj C |
| 278 | // interfaces. radar://10595327 |
| 279 | int rdar_10595327(char *str) { |
| 280 | char fl = str[0]; |
| 281 | int *p = 0; |
| 282 | NSString *s = [NSString stringWithUTF8String:str]; |
| 283 | if (str[0] != fl) |
| 284 | return *p; // no-warning |
| 285 | return 0; |
| 286 | } |
| 287 | |
Ted Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 288 | // For pointer arithmetic, --/++ should be treated as preserving non-nullness, |
| 289 | // regardless of how well the underlying StoreManager reasons about pointer |
| 290 | // arithmetic. |
| 291 | // <rdar://problem/6777209> |
Ted Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 292 | void rdar_6777209(char *p) { |
| 293 | if (p == 0) |
| 294 | return; |
| 295 | |
| 296 | ++p; |
| 297 | |
| 298 | // This branch should always be infeasible. |
| 299 | if (p == 0) |
| 300 | *p = 'c'; // no-warning |
| 301 | } |
Ted Kremenek | b3cfd58 | 2009-04-23 17:49:43 +0000 | [diff] [blame] | 302 | |
| 303 | // PR 4033. A symbolic 'void *' pointer can be used as the address for a |
| 304 | // computed goto. |
| 305 | typedef void *Opcode; |
| 306 | Opcode pr_4033_getOpcode(); |
| 307 | void pr_4033(void) { |
John McCall | b60a77e | 2010-08-01 00:26:45 +0000 | [diff] [blame] | 308 | void *lbl = &&next_opcode; |
Ted Kremenek | b3cfd58 | 2009-04-23 17:49:43 +0000 | [diff] [blame] | 309 | next_opcode: |
| 310 | { |
| 311 | Opcode op = pr_4033_getOpcode(); |
| 312 | if (op) goto *op; |
| 313 | } |
| 314 | } |
| 315 | |
Ted Kremenek | 956a37d | 2009-05-01 23:35:18 +0000 | [diff] [blame] | 316 | // Test invalidating pointers-to-pointers with slightly different types. This |
| 317 | // example came from a recent false positive due to a regression where the |
| 318 | // branch condition was falsely reported as being uninitialized. |
| 319 | void invalidate_by_ref(char **x); |
| 320 | int test_invalidate_by_ref() { |
| 321 | unsigned short y; |
| 322 | invalidate_by_ref((char**) &y); |
| 323 | if (y) // no-warning |
| 324 | return 1; |
| 325 | return 0; |
| 326 | } |
| 327 | |
Ted Kremenek | ed47fc6 | 2009-07-03 00:10:50 +0000 | [diff] [blame] | 328 | // Test for <rdar://problem/7027684>. This just tests that the CFG is |
| 329 | // constructed correctly. Previously, the successor block of the entrance |
| 330 | // was the block containing the merge for '?', which would trigger an |
| 331 | // assertion failure. |
| 332 | int rdar_7027684_aux(); |
Anders Carlsson | 5d1d7ae | 2010-09-03 00:25:02 +0000 | [diff] [blame] | 333 | int rdar_7027684_aux_2() __attribute__((noreturn)); |
Ted Kremenek | ed47fc6 | 2009-07-03 00:10:50 +0000 | [diff] [blame] | 334 | void rdar_7027684(int x, int y) { |
| 335 | {}; // this empty compound statement is critical. |
| 336 | (rdar_7027684_aux() ? rdar_7027684_aux_2() : (void) 0); |
| 337 | } |
| 338 | |
Ted Kremenek | 411af40 | 2009-07-06 22:23:45 +0000 | [diff] [blame] | 339 | // Test that we handle casts of string literals to arbitrary types. |
| 340 | unsigned const char *string_literal_test1() { |
| 341 | return (const unsigned char*) "hello"; |
| 342 | } |
| 343 | |
| 344 | const float *string_literal_test2() { |
| 345 | return (const float*) "hello"; |
| 346 | } |
| 347 | |
Ted Kremenek | 169077d | 2009-07-06 23:47:19 +0000 | [diff] [blame] | 348 | // Test that we handle casts *from* incomplete struct types. |
| 349 | extern const struct _FooAssertStruct _cmd; |
| 350 | void test_cast_from_incomplete_struct_aux(volatile const void *x); |
| 351 | void test_cast_from_incomplete_struct() { |
| 352 | test_cast_from_incomplete_struct_aux(&_cmd); |
| 353 | } |
Ted Kremenek | ed47fc6 | 2009-07-03 00:10:50 +0000 | [diff] [blame] | 354 | |
Ted Kremenek | 5997888 | 2009-07-08 22:42:46 +0000 | [diff] [blame] | 355 | // Test for <rdar://problem/7034511> |
| 356 | // "ValueManager::makeIntVal(uint64_t X, QualType T) should return a 'Loc' |
| 357 | // when 'T' is a pointer" |
| 358 | // |
| 359 | // Previously this case would crash. |
| 360 | void test_rdar_7034511(NSArray *y) { |
| 361 | NSObject *x; |
| 362 | for (x in y) {} |
| 363 | if (x == ((void*) 0)) {} |
| 364 | } |
| 365 | |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 366 | // Handle casts of function pointers (CodeTextRegions) to arbitrary pointer |
| 367 | // types. This was previously causing a crash in CastRegion. |
| 368 | void handle_funcptr_voidptr_casts() { |
Ted Kremenek | 3f9811b | 2009-07-10 21:11:16 +0000 | [diff] [blame] | 369 | void **ptr; |
| 370 | typedef void *PVOID; |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 371 | typedef void *PCHAR; |
Ted Kremenek | 3f9811b | 2009-07-10 21:11:16 +0000 | [diff] [blame] | 372 | typedef long INT_PTR, *PINT_PTR; |
| 373 | typedef INT_PTR (*FARPROC)(); |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 374 | FARPROC handle_funcptr_voidptr_casts_aux(); |
| 375 | PVOID handle_funcptr_voidptr_casts_aux_2(PVOID volatile *x); |
| 376 | PVOID handle_funcptr_voidptr_casts_aux_3(PCHAR volatile *x); |
Ted Kremenek | 3f9811b | 2009-07-10 21:11:16 +0000 | [diff] [blame] | 377 | |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 378 | ptr = (void**) handle_funcptr_voidptr_casts_aux(); |
| 379 | handle_funcptr_voidptr_casts_aux_2(ptr); |
| 380 | handle_funcptr_voidptr_casts_aux_3(ptr); |
Ted Kremenek | 3f9811b | 2009-07-10 21:11:16 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Ted Kremenek | 31ef2b6 | 2009-07-10 21:43:30 +0000 | [diff] [blame] | 383 | // RegionStore::Retrieve previously crashed on this example. This example |
| 384 | // was previously in the test file 'xfail_regionstore_wine_crash.c'. |
| 385 | void testA() { |
| 386 | long x = 0; |
| 387 | char *y = (char *) &x; |
| 388 | if (!*y) |
| 389 | return; |
| 390 | } |
| 391 | |
Ted Kremenek | 43d74a5 | 2009-07-11 04:38:49 +0000 | [diff] [blame] | 392 | // RegionStoreManager previously crashed on this example. The problem is that |
| 393 | // the value bound to the field of b->grue after the call to testB_aux is |
| 394 | // a symbolic region. The second '*__gruep__' involves performing a load |
| 395 | // from a 'int*' that really is a 'void**'. The loaded location must be |
| 396 | // implicitly converted to an integer that wraps a location. Previosly we would |
| 397 | // get a crash here due to an assertion failure. |
| 398 | typedef struct _BStruct { void *grue; } BStruct; |
| 399 | void testB_aux(void *ptr); |
| 400 | void testB(BStruct *b) { |
| 401 | { |
| 402 | int *__gruep__ = ((int *)&((b)->grue)); |
| 403 | int __gruev__ = *__gruep__; |
| 404 | testB_aux(__gruep__); |
| 405 | } |
| 406 | { |
| 407 | int *__gruep__ = ((int *)&((b)->grue)); |
| 408 | int __gruev__ = *__gruep__; |
| 409 | if (~0 != __gruev__) {} |
| 410 | } |
| 411 | } |
| 412 | |
Ted Kremenek | 54ca9b1 | 2009-07-13 21:55:12 +0000 | [diff] [blame] | 413 | void test_trivial_symbolic_comparison(int *x) { |
| 414 | int test_trivial_symbolic_comparison_aux(); |
| 415 | int a = test_trivial_symbolic_comparison_aux(); |
| 416 | int b = a; |
Tom Care | 9edd4d0 | 2010-08-27 22:50:47 +0000 | [diff] [blame] | 417 | if (a != b) { // expected-warning{{Both operands to '!=' always have the same value}} |
Ted Kremenek | 54ca9b1 | 2009-07-13 21:55:12 +0000 | [diff] [blame] | 418 | int *p = 0; |
| 419 | *p = 0xDEADBEEF; // no-warning |
| 420 | } |
| 421 | |
| 422 | a = a == 1; |
| 423 | b = b == 1; |
Tom Care | 9edd4d0 | 2010-08-27 22:50:47 +0000 | [diff] [blame] | 424 | if (a != b) { // expected-warning{{Both operands to '!=' always have the same value}} |
Ted Kremenek | 54ca9b1 | 2009-07-13 21:55:12 +0000 | [diff] [blame] | 425 | int *p = 0; |
| 426 | *p = 0xDEADBEEF; // no-warning |
| 427 | } |
| 428 | } |
| 429 | |
Ted Kremenek | fde2efe | 2009-07-15 22:09:25 +0000 | [diff] [blame] | 430 | // Test for: |
| 431 | // <rdar://problem/7062158> false positive null dereference due to |
| 432 | // BasicStoreManager not tracking *static* globals |
| 433 | // |
| 434 | // This just tests the proper tracking of symbolic values for globals (both |
| 435 | // static and non-static). |
| 436 | // |
| 437 | static int* x_rdar_7062158; |
| 438 | void rdar_7062158() { |
| 439 | int *current = x_rdar_7062158; |
| 440 | if (current == x_rdar_7062158) |
| 441 | return; |
| 442 | |
| 443 | int *p = 0; |
| 444 | *p = 0xDEADBEEF; // no-warning |
| 445 | } |
| 446 | |
| 447 | int* x_rdar_7062158_2; |
| 448 | void rdar_7062158_2() { |
| 449 | int *current = x_rdar_7062158_2; |
| 450 | if (current == x_rdar_7062158_2) |
| 451 | return; |
| 452 | |
| 453 | int *p = 0; |
| 454 | *p = 0xDEADBEEF; // no-warning |
| 455 | } |
| 456 | |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 457 | // This test reproduces a case for a crash when analyzing ClamAV using |
| 458 | // RegionStoreManager (the crash doesn't exhibit in BasicStoreManager because |
| 459 | // it isn't doing anything smart about arrays). The problem is that on the |
| 460 | // second line, 'p = &p[i]', p is assigned an ElementRegion whose index |
| 461 | // is a 16-bit integer. On the third line, a new ElementRegion is created |
| 462 | // based on the previous region, but there the region uses a 32-bit integer, |
| 463 | // resulting in a clash of values (an assertion failure at best). We resolve |
| 464 | // this problem by implicitly converting index values to 'int' when the |
| 465 | // ElementRegion is created. |
| 466 | unsigned char test_array_index_bitwidth(const unsigned char *p) { |
| 467 | unsigned short i = 0; |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 468 | for (i = 0; i < 2; i++) p = &p[i]; |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 469 | return p[i+1]; |
| 470 | } |
| 471 | |
Ted Kremenek | 63b9cfe | 2009-07-18 06:27:51 +0000 | [diff] [blame] | 472 | // This case tests that CastRegion handles casts involving BlockPointerTypes. |
| 473 | // It should not crash. |
| 474 | void test_block_cast() { |
| 475 | id test_block_cast_aux(); |
Argyrios Kyrtzidis | 1b2ad2f | 2010-09-19 23:03:35 +0000 | [diff] [blame] | 476 | (void (^)(void *))test_block_cast_aux(); // expected-warning{{expression result unused}} |
Ted Kremenek | 63b9cfe | 2009-07-18 06:27:51 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Chris Lattner | e030358 | 2010-01-09 20:43:19 +0000 | [diff] [blame] | 479 | int OSAtomicCompareAndSwap32Barrier(); |
| 480 | |
Ted Kremenek | 293769a | 2009-07-20 21:00:55 +0000 | [diff] [blame] | 481 | // Test comparison of 'id' instance variable to a null void* constant after |
| 482 | // performing an OSAtomicCompareAndSwap32Barrier. |
| 483 | // This previously was a crash in RegionStoreManager. |
| 484 | @interface TestIdNull { |
| 485 | id x; |
| 486 | } |
| 487 | -(int)foo; |
| 488 | @end |
| 489 | @implementation TestIdNull |
| 490 | -(int)foo { |
| 491 | OSAtomicCompareAndSwap32Barrier(0, (signed)2, (signed*)&x); |
| 492 | if (x == (void*) 0) { return 0; } |
| 493 | return 1; |
| 494 | } |
| 495 | @end |
| 496 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 497 | // PR 4594 - This was a crash when handling casts in SimpleSValuator. |
| 498 | void PR4594() { |
| 499 | char *buf[1]; |
| 500 | char **foo = buf; |
| 501 | *foo = "test"; |
| 502 | } |
Ted Kremenek | 0b331e3 | 2009-07-22 04:23:20 +0000 | [diff] [blame] | 503 | |
| 504 | // Test invalidation logic where an integer is casted to an array with a |
| 505 | // different sign and then invalidated. |
| 506 | void test_invalidate_cast_int() { |
| 507 | void test_invalidate_cast_int_aux(unsigned *i); |
| 508 | signed i; |
| 509 | test_invalidate_cast_int_aux((unsigned*) &i); |
| 510 | if (i < 0) |
| 511 | return; |
| 512 | } |
| 513 | |
Chris Lattner | e030358 | 2010-01-09 20:43:19 +0000 | [diff] [blame] | 514 | int ivar_getOffset(); |
| 515 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 516 | // Reduced from a crash involving the cast of an Objective-C symbolic region to |
| 517 | // 'char *' |
| 518 | static NSNumber *test_ivar_offset(id self, SEL _cmd, Ivar inIvar) { |
| 519 | return [[[NSNumber allocWithZone:((void*)0)] initWithBool:*(_Bool *)((char *)self + ivar_getOffset(inIvar))] autorelease]; |
| 520 | } |
Ted Kremenek | 9a108eb | 2009-08-02 04:12:53 +0000 | [diff] [blame] | 521 | |
| 522 | // Reduced from a crash in StoreManager::CastRegion involving a divide-by-zero. |
| 523 | // This resulted from not properly handling region casts to 'const void*'. |
| 524 | void test_cast_const_voidptr() { |
| 525 | char x[10]; |
| 526 | char *p = &x[1]; |
| 527 | const void* q = p; |
| 528 | } |
Ted Kremenek | 968f0a6 | 2009-08-03 21:41:46 +0000 | [diff] [blame] | 529 | |
| 530 | // Reduced from a crash when analyzing Wine. This test handles loads from |
| 531 | // function addresses. |
| 532 | typedef long (*FARPROC)(); |
| 533 | FARPROC test_load_func(FARPROC origfun) { |
| 534 | if (!*(unsigned char*) origfun) |
| 535 | return origfun; |
| 536 | return 0; |
| 537 | } |
Ted Kremenek | 48775d5 | 2009-08-03 23:22:53 +0000 | [diff] [blame] | 538 | |
| 539 | // Test passing-by-value an initialized struct variable. |
| 540 | struct test_pass_val { |
| 541 | int x; |
| 542 | int y; |
| 543 | }; |
| 544 | void test_pass_val_aux(struct test_pass_val s); |
| 545 | void test_pass_val() { |
| 546 | struct test_pass_val s; |
| 547 | s.x = 1; |
Ted Kremenek | 8eec7c0 | 2009-08-04 00:58:45 +0000 | [diff] [blame] | 548 | s.y = 2; |
Ted Kremenek | 48775d5 | 2009-08-03 23:22:53 +0000 | [diff] [blame] | 549 | test_pass_val_aux(s); |
| 550 | } |
| 551 | |
Ted Kremenek | 4ed4598 | 2009-08-05 05:31:02 +0000 | [diff] [blame] | 552 | // This is a reduced test case of a false positive that previously appeared |
| 553 | // in RegionStoreManager. Previously the array access resulted in dereferencing |
| 554 | // an undefined value. |
| 555 | int test_array_compound(int *q, int *r, int *z) { |
| 556 | int *array[] = { q, r, z }; |
| 557 | int j = 0; |
| 558 | for (unsigned i = 0; i < 3 ; ++i) |
| 559 | if (*array[i]) ++j; // no-warning |
| 560 | return j; |
| 561 | } |
| 562 | |
Ted Kremenek | 1894dce | 2009-08-25 20:51:30 +0000 | [diff] [blame] | 563 | // symbolic value stored in 'x' wouldn't be implicitly casted to a signed value |
| 564 | // during the comparison. |
| 565 | int rdar_7124210(unsigned int x) { |
| 566 | enum { SOME_CONSTANT = 123 }; |
| 567 | int compare = ((signed) SOME_CONSTANT) == *((signed *) &x); |
| 568 | return compare ? 0 : 1; // Forces the evaluation of the symbolic constraint. |
| 569 | } |
| 570 | |
Ted Kremenek | ab2f43c | 2009-08-25 23:29:04 +0000 | [diff] [blame] | 571 | void pr4781(unsigned long *raw1) { |
| 572 | unsigned long *cook, *raw0; |
| 573 | unsigned long dough[32]; |
| 574 | int i; |
| 575 | cook = dough; |
| 576 | for( i = 0; i < 16; i++, raw1++ ) { |
| 577 | raw0 = raw1++; |
| 578 | *cook = (*raw0 & 0x00fc0000L) << 6; |
| 579 | *cook |= (*raw0 & 0x00000fc0L) << 10; |
| 580 | } |
| 581 | } |
| 582 | |
Ted Kremenek | cfcd7fd | 2009-09-09 20:36:12 +0000 | [diff] [blame] | 583 | // <rdar://problem/7185647> - 'self' should be treated as being non-null |
| 584 | // upon entry to an objective-c method. |
| 585 | @interface RDar7185647 |
| 586 | - (id)foo; |
| 587 | @end |
| 588 | @implementation RDar7185647 |
| 589 | - (id) foo { |
| 590 | if (self) |
| 591 | return self; |
Chris Lattner | 8b5dec3 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 592 | *((volatile int *) 0x0) = 0xDEADBEEF; // no-warning |
Ted Kremenek | cfcd7fd | 2009-09-09 20:36:12 +0000 | [diff] [blame] | 593 | return self; |
| 594 | } |
| 595 | @end |
Ted Kremenek | e2b5744 | 2009-09-15 00:40:32 +0000 | [diff] [blame] | 596 | |
| 597 | // Test reasoning of __builtin_offsetof; |
| 598 | struct test_offsetof_A { |
| 599 | int x; |
| 600 | int y; |
| 601 | }; |
| 602 | struct test_offsetof_B { |
| 603 | int w; |
| 604 | int z; |
| 605 | }; |
| 606 | void test_offsetof_1() { |
| 607 | if (__builtin_offsetof(struct test_offsetof_A, x) == |
| 608 | __builtin_offsetof(struct test_offsetof_B, w)) |
| 609 | return; |
| 610 | int *p = 0; |
| 611 | *p = 0xDEADBEEF; // no-warning |
| 612 | } |
| 613 | void test_offsetof_2() { |
| 614 | if (__builtin_offsetof(struct test_offsetof_A, y) == |
| 615 | __builtin_offsetof(struct test_offsetof_B, z)) |
| 616 | return; |
| 617 | int *p = 0; |
| 618 | *p = 0xDEADBEEF; // no-warning |
| 619 | } |
| 620 | void test_offsetof_3() { |
| 621 | if (__builtin_offsetof(struct test_offsetof_A, y) - |
| 622 | __builtin_offsetof(struct test_offsetof_A, x) |
| 623 | == |
| 624 | __builtin_offsetof(struct test_offsetof_B, z) - |
| 625 | __builtin_offsetof(struct test_offsetof_B, w)) |
| 626 | return; |
| 627 | int *p = 0; |
| 628 | *p = 0xDEADBEEF; // no-warning |
| 629 | } |
| 630 | void test_offsetof_4() { |
| 631 | if (__builtin_offsetof(struct test_offsetof_A, y) == |
| 632 | __builtin_offsetof(struct test_offsetof_B, w)) |
| 633 | return; |
| 634 | int *p = 0; |
| 635 | *p = 0xDEADBEEF; // expected-warning{{Dereference of null pointer}} |
| 636 | } |
| 637 | |
Ted Kremenek | c32b244 | 2009-09-21 23:22:11 +0000 | [diff] [blame] | 638 | // <rdar://problem/6829164> "nil receiver" false positive: make tracking |
| 639 | // of the MemRegion for 'self' path-sensitive |
| 640 | @interface RDar6829164 : NSObject { |
| 641 | double x; int y; |
| 642 | } |
| 643 | - (id) init; |
| 644 | @end |
| 645 | |
| 646 | id rdar_6829164_1(); |
| 647 | double rdar_6829164_2(); |
| 648 | |
| 649 | @implementation RDar6829164 |
| 650 | - (id) init { |
| 651 | if((self = [super init]) != 0) { |
| 652 | id z = rdar_6829164_1(); |
| 653 | y = (z != 0); |
| 654 | if (y) |
| 655 | x = rdar_6829164_2(); |
| 656 | } |
| 657 | return self; |
| 658 | } |
| 659 | @end |
Ted Kremenek | cc969fd | 2009-09-22 04:48:39 +0000 | [diff] [blame] | 660 | |
| 661 | // <rdar://problem/7242015> - Invalidate values passed-by-reference |
| 662 | // to functions when the pointer to the value is passed as an integer. |
| 663 | void test_7242015_aux(unsigned long); |
| 664 | int rdar_7242015() { |
| 665 | int x; |
| 666 | test_7242015_aux((unsigned long) &x); // no-warning |
| 667 | return x; // Previously we return and uninitialized value when |
| 668 | // using RegionStore. |
| 669 | } |
| 670 | |
Ted Kremenek | cf54959 | 2009-09-22 21:19:14 +0000 | [diff] [blame] | 671 | // <rdar://problem/7242006> [RegionStore] compound literal assignment with |
| 672 | // floats not honored |
| 673 | CGFloat rdar7242006(CGFloat x) { |
| 674 | NSSize y = (NSSize){x, 10}; |
| 675 | return y.width; // no-warning |
| 676 | } |
| 677 | |
Ted Kremenek | 657406d | 2009-09-23 01:30:01 +0000 | [diff] [blame] | 678 | // PR 4988 - This test exhibits a case where a function can be referenced |
| 679 | // when not explicitly used in an "lvalue" context (as far as the analyzer is |
| 680 | // concerned). This previously triggered a crash due to an invalid assertion. |
| 681 | void pr_4988(void) { |
Argyrios Kyrtzidis | 1b2ad2f | 2010-09-19 23:03:35 +0000 | [diff] [blame] | 682 | pr_4988; // expected-warning{{expression result unused}} |
Ted Kremenek | 657406d | 2009-09-23 01:30:01 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Ted Kremenek | 022a125 | 2009-09-26 17:18:44 +0000 | [diff] [blame] | 685 | // <rdar://problem/7152418> - A 'signed char' is used as a flag, which is |
| 686 | // implicitly converted to an int. |
| 687 | void *rdar7152418_bar(); |
| 688 | @interface RDar7152418 { |
| 689 | signed char x; |
| 690 | } |
| 691 | -(char)foo; |
| 692 | @end; |
| 693 | @implementation RDar7152418 |
| 694 | -(char)foo { |
| 695 | char *p = 0; |
| 696 | void *result = 0; |
| 697 | if (x) { |
| 698 | result = rdar7152418_bar(); |
| 699 | p = "hello"; |
| 700 | } |
| 701 | if (!result) { |
| 702 | result = rdar7152418_bar(); |
| 703 | if (result && x) |
| 704 | return *p; // no-warning |
| 705 | } |
| 706 | return 1; |
| 707 | } |
| 708 | |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 709 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b1d0422 | 2009-10-06 03:44:49 +0000 | [diff] [blame] | 710 | // Test constant-folding of symbolic values, automatically handling type |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 711 | // conversions of the symbol as necessary. |
| 712 | //===----------------------------------------------------------------------===// |
| 713 | |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 714 | // Previously this would crash once we started eagerly evaluating symbols whose |
| 715 | // values were constrained to a single value. |
| 716 | void test_symbol_fold_1(signed char x) { |
Ted Kremenek | b1d0422 | 2009-10-06 03:44:49 +0000 | [diff] [blame] | 717 | while (1) { |
| 718 | if (x == ((signed char) 0)) {} |
| 719 | } |
| 720 | } |
| 721 | |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 722 | // This previously caused a crash because it triggered an assertion in APSInt. |
| 723 | void test_symbol_fold_2(unsigned int * p, unsigned int n, |
| 724 | const unsigned int * grumpkin, unsigned int dn) { |
Ted Kremenek | b5deae5 | 2009-10-16 20:46:24 +0000 | [diff] [blame] | 725 | unsigned int i; |
| 726 | unsigned int tempsub[8]; |
| 727 | unsigned int *solgrumpkin = tempsub + n; |
| 728 | for (i = 0; i < n; i++) |
| 729 | solgrumpkin[i] = (i < dn) ? ~grumpkin[i] : 0xFFFFFFFF; |
| 730 | for (i <<= 5; i < (n << 5); i++) {} |
| 731 | } |
Ted Kremenek | b1d0422 | 2009-10-06 03:44:49 +0000 | [diff] [blame] | 732 | |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 733 | // This previously caused a crash because it triggered an assertion in APSInt. |
| 734 | // 'x' would evaluate to a 8-bit constant (because of the return value of |
| 735 | // test_symbol_fold_3_aux()) which would not get properly promoted to an |
| 736 | // integer. |
| 737 | char test_symbol_fold_3_aux(void); |
| 738 | unsigned test_symbol_fold_3(void) { |
| 739 | unsigned x = test_symbol_fold_3_aux(); |
| 740 | if (x == 54) |
| 741 | return (x << 8) | 0x5; |
| 742 | return 0; |
Ted Kremenek | de7d800 | 2009-11-11 06:43:42 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | //===----------------------------------------------------------------------===// |
| 746 | // Tests for the warning of casting a non-struct type to a struct type |
| 747 | //===----------------------------------------------------------------------===// |
| 748 | |
| 749 | typedef struct {unsigned int v;} NSSwappedFloat; |
| 750 | |
| 751 | NSSwappedFloat test_cast_nonstruct_to_struct(float x) { |
| 752 | struct hodor { |
| 753 | float number; |
| 754 | NSSwappedFloat sf; |
| 755 | }; |
| 756 | return ((struct hodor *)&x)->sf; // expected-warning{{Casting a non-structure type to a structure type and accessing a field can lead to memory access errors or data corruption}} |
| 757 | } |
| 758 | |
| 759 | NSSwappedFloat test_cast_nonstruct_to_union(float x) { |
| 760 | union bran { |
| 761 | float number; |
| 762 | NSSwappedFloat sf; |
| 763 | }; |
| 764 | return ((union bran *)&x)->sf; // no-warning |
| 765 | } |
Ted Kremenek | 9b02034 | 2009-10-17 07:39:35 +0000 | [diff] [blame] | 766 | |
Zhongxing Xu | d694485 | 2009-11-11 13:42:54 +0000 | [diff] [blame] | 767 | void test_undefined_array_subscript() { |
| 768 | int i, a[10]; |
| 769 | int *p = &a[i]; // expected-warning{{Array subscript is undefined}} |
| 770 | } |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 771 | @end |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 772 | |
| 773 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 998c133 | 2009-11-23 17:58:48 +0000 | [diff] [blame] | 774 | // Test using an uninitialized value as a branch condition. |
| 775 | //===----------------------------------------------------------------------===// |
| 776 | |
| 777 | int test_uninit_branch(void) { |
| 778 | int x; |
| 779 | if (x) // expected-warning{{Branch condition evaluates to a garbage value}} |
| 780 | return 1; |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | int test_uninit_branch_b(void) { |
| 785 | int x; |
| 786 | return x ? 1 : 0; // expected-warning{{Branch condition evaluates to a garbage value}} |
| 787 | } |
| 788 | |
Ted Kremenek | 616cf05 | 2009-11-23 18:12:03 +0000 | [diff] [blame] | 789 | int test_uninit_branch_c(void) { |
| 790 | int x; |
| 791 | if ((short)x) // expected-warning{{Branch condition evaluates to a garbage value}} |
| 792 | return 1; |
| 793 | return 0; |
| 794 | } |
Ted Kremenek | 998c133 | 2009-11-23 17:58:48 +0000 | [diff] [blame] | 795 | |
| 796 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 797 | // Test passing an undefined value in a message or function call. |
| 798 | //===----------------------------------------------------------------------===// |
| 799 | |
| 800 | void test_bad_call_aux(int x); |
| 801 | void test_bad_call(void) { |
| 802 | int y; |
Ted Kremenek | 818b433 | 2010-09-09 22:51:55 +0000 | [diff] [blame] | 803 | test_bad_call_aux(y); // expected-warning{{Function call argument is an uninitialized value}} |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | @interface TestBadArg {} |
| 807 | - (void) testBadArg:(int) x; |
| 808 | @end |
| 809 | |
| 810 | void test_bad_msg(TestBadArg *p) { |
| 811 | int y; |
Ted Kremenek | 818b433 | 2010-09-09 22:51:55 +0000 | [diff] [blame] | 812 | [p testBadArg:y]; // expected-warning{{Argument in message expression is an uninitialized value}} |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Ted Kremenek | c213b48 | 2010-01-15 07:56:51 +0000 | [diff] [blame] | 815 | //===----------------------------------------------------------------------===// |
| 816 | // PR 6033 - Test emitting the correct output in a warning where we use '%' |
| 817 | // with operands that are undefined. |
| 818 | //===----------------------------------------------------------------------===// |
| 819 | |
| 820 | int pr6033(int x) { |
| 821 | int y; |
| 822 | return x % y; // expected-warning{{The right operand of '%' is a garbage value}} |
| 823 | } |
| 824 | |
Zhongxing Xu | 45eb406 | 2010-01-19 12:11:55 +0000 | [diff] [blame] | 825 | struct trie { |
| 826 | struct trie* next; |
| 827 | }; |
| 828 | |
| 829 | struct kwset { |
| 830 | struct trie *trie; |
Ted Kremenek | ac518ec | 2011-02-11 20:13:27 +0000 | [diff] [blame] | 831 | unsigned char y[10]; |
Zhongxing Xu | 45eb406 | 2010-01-19 12:11:55 +0000 | [diff] [blame] | 832 | struct trie* next[10]; |
| 833 | int d; |
| 834 | }; |
| 835 | |
| 836 | typedef struct trie trie_t; |
| 837 | typedef struct kwset kwset_t; |
| 838 | |
| 839 | void f(kwset_t *kws, char const *p, char const *q) { |
| 840 | struct trie const *trie; |
| 841 | struct trie * const *next = kws->next; |
| 842 | register unsigned char c; |
| 843 | register char const *end = p; |
| 844 | register char const *lim = q; |
| 845 | register int d = 1; |
Ted Kremenek | ac518ec | 2011-02-11 20:13:27 +0000 | [diff] [blame] | 846 | register unsigned char const *y = kws->y; |
Zhongxing Xu | 45eb406 | 2010-01-19 12:11:55 +0000 | [diff] [blame] | 847 | |
Ted Kremenek | ac518ec | 2011-02-11 20:13:27 +0000 | [diff] [blame] | 848 | d = y[c = (end+=d)[-1]]; // no-warning |
Zhongxing Xu | 45eb406 | 2010-01-19 12:11:55 +0000 | [diff] [blame] | 849 | trie = next[c]; |
| 850 | } |
Ted Kremenek | 61e8e1b | 2010-02-02 02:01:51 +0000 | [diff] [blame] | 851 | |
| 852 | //===----------------------------------------------------------------------===// |
| 853 | // <rdar://problem/7593875> When handling sizeof(VLA) it leads to a hole in |
| 854 | // the ExplodedGraph (causing a false positive) |
| 855 | //===----------------------------------------------------------------------===// |
| 856 | |
| 857 | int rdar_7593875_aux(int x); |
| 858 | int rdar_7593875(int n) { |
| 859 | int z[n > 10 ? 10 : n]; // VLA. |
| 860 | int v; |
| 861 | v = rdar_7593875_aux(sizeof(z)); |
| 862 | // Previously we got a false positive about 'v' being uninitialized. |
| 863 | return v; // no-warning |
| 864 | } |
Ted Kremenek | f681704 | 2010-02-02 21:11:40 +0000 | [diff] [blame] | 865 | |
| 866 | //===----------------------------------------------------------------------===// |
| 867 | // Handle casts from symbolic regions (packaged as integers) to doubles. |
| 868 | // Previously this caused an assertion failure. |
| 869 | //===----------------------------------------------------------------------===// |
| 870 | |
| 871 | void *foo_rev95119(); |
| 872 | void baz_rev95119(double x); |
| 873 | void bar_rev95119() { |
| 874 | // foo_rev95119() returns a symbolic pointer. It is then |
| 875 | // cast to an int which is then cast to a double. |
| 876 | int value = (int) foo_rev95119(); |
| 877 | baz_rev95119((double)value); |
| 878 | } |
| 879 | |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 880 | //===----------------------------------------------------------------------===// |
| 881 | // Handle loading a symbolic pointer from a symbolic region that was |
| 882 | // invalidated by a call to an unknown function. |
| 883 | //===----------------------------------------------------------------------===// |
| 884 | |
| 885 | void bar_rev95192(int **x); |
| 886 | void foo_rev95192(int **x) { |
| 887 | *x = 0; |
| 888 | bar_rev95192(x); |
| 889 | // Not a null dereference. |
| 890 | **x = 1; // no-warning |
| 891 | } |
| 892 | |
Ted Kremenek | 40c37e1 | 2010-02-04 00:47:48 +0000 | [diff] [blame] | 893 | //===----------------------------------------------------------------------===// |
| 894 | // Handle casts of a function to a function pointer with a different return |
| 895 | // value. We don't yet emit an error for such cases, but we now we at least |
| 896 | // don't crash when the return value gets interpreted in a way that |
| 897 | // violates our invariants. |
| 898 | //===----------------------------------------------------------------------===// |
| 899 | |
| 900 | void *foo_rev95267(); |
| 901 | int bar_rev95267() { |
| 902 | char (*Callback_rev95267)(void) = (char (*)(void)) foo_rev95267; |
| 903 | if ((*Callback_rev95267)() == (char) 0) |
| 904 | return 1; |
| 905 | return 0; |
| 906 | } |
Ted Kremenek | 95a0112 | 2010-02-04 04:18:55 +0000 | [diff] [blame] | 907 | |
| 908 | // Same as previous case, but handle casts to 'void'. |
| 909 | int bar_rev95274() { |
| 910 | void (*Callback_rev95274)(void) = (void (*)(void)) foo_rev95267; |
| 911 | (*Callback_rev95274)(); |
| 912 | return 0; |
| 913 | } |
| 914 | |
Ted Kremenek | 4dc1566 | 2010-02-06 03:57:59 +0000 | [diff] [blame] | 915 | void rdar7582031_test_static_init_zero() { |
| 916 | static unsigned x; |
| 917 | if (x == 0) |
| 918 | return; |
| 919 | int *p = 0; |
| 920 | *p = 0xDEADBEEF; |
| 921 | } |
Ted Kremenek | 81861ab | 2010-02-06 04:04:46 +0000 | [diff] [blame] | 922 | void rdar7582031_test_static_init_zero_b() { |
| 923 | static void* x; |
| 924 | if (x == 0) |
| 925 | return; |
| 926 | int *p = 0; |
| 927 | *p = 0xDEADBEEF; |
| 928 | } |
Ted Kremenek | 8ec4aac | 2010-02-09 19:11:53 +0000 | [diff] [blame] | 929 | |
| 930 | //===----------------------------------------------------------------------===// |
| 931 | // Test handling of parameters that are structs that contain floats and // |
| 932 | // nested fields. // |
| 933 | //===----------------------------------------------------------------------===// |
| 934 | |
| 935 | struct s_rev95547_nested { float x, y; }; |
| 936 | struct s_rev95547 { |
| 937 | struct s_rev95547_nested z1; |
| 938 | struct s_rev95547_nested z2; |
| 939 | }; |
| 940 | float foo_rev95547(struct s_rev95547 w) { |
| 941 | return w.z1.x + 20.0; // no-warning |
| 942 | } |
| 943 | void foo_rev95547_b(struct s_rev95547 w) { |
| 944 | struct s_rev95547 w2 = w; |
| 945 | w2.z1.x += 20.0; // no-warning |
| 946 | } |
Ted Kremenek | 862b24f | 2010-04-29 01:10:26 +0000 | [diff] [blame] | 947 | |
| 948 | //===----------------------------------------------------------------------===// |
| 949 | // Test handling statement expressions that don't populate a CFG block that |
| 950 | // is used to represent the computation of the RHS of a logical operator. |
| 951 | // This previously triggered a crash. |
| 952 | //===----------------------------------------------------------------------===// |
| 953 | |
| 954 | void pr6938() { |
| 955 | if (1 && ({ |
| 956 | while (0); |
| 957 | 0; |
| 958 | }) == 0) { |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | void pr6938_b() { |
| 963 | if (1 && *({ // expected-warning{{Dereference of null pointer}} |
| 964 | while (0) {} |
| 965 | ({ |
| 966 | (int *) 0; |
| 967 | }); |
| 968 | }) == 0) { |
| 969 | } |
| 970 | } |
Ted Kremenek | fadebba | 2010-05-13 16:38:08 +0000 | [diff] [blame] | 971 | |
| 972 | //===----------------------------------------------------------------------===// |
| 973 | // <rdar://problem/7979430> - The CFG for code containing an empty |
| 974 | // @synchronized block was previously broken (and would crash the analyzer). |
| 975 | //===----------------------------------------------------------------------===// |
| 976 | |
| 977 | void r7979430(id x) { |
| 978 | @synchronized(x) {} |
| 979 | } |
| 980 | |
Ted Kremenek | fcd783d | 2010-06-15 00:55:40 +0000 | [diff] [blame] | 981 | //===----------------------------------------------------------------------=== |
| 982 | // PR 7361 - Test that functions wrapped in macro instantiations are analyzed. |
| 983 | //===----------------------------------------------------------------------=== |
| 984 | #define MAKE_TEST_FN() \ |
| 985 | void test_pr7361 (char a) {\ |
| 986 | char* b = 0x0; *b = a;\ |
| 987 | } |
| 988 | |
| 989 | MAKE_TEST_FN() // expected-warning{{null pointer}} |
| 990 | |
Jordy Rose | 5ca129c | 2010-06-27 01:20:56 +0000 | [diff] [blame] | 991 | //===----------------------------------------------------------------------=== |
| 992 | // PR 7491 - Test that symbolic expressions can be used as conditions. |
| 993 | //===----------------------------------------------------------------------=== |
| 994 | |
| 995 | void pr7491 () { |
| 996 | extern int getint(); |
| 997 | int a = getint()-1; |
| 998 | if (a) { |
| 999 | return; |
| 1000 | } |
| 1001 | if (!a) { |
| 1002 | return; |
| 1003 | } else { |
| 1004 | // Should be unreachable |
| 1005 | (void)*(char*)0; // no-warning |
| 1006 | } |
| 1007 | } |
| 1008 | |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 1009 | //===----------------------------------------------------------------------=== |
| 1010 | // PR 7475 - Test that assumptions about global variables are reset after |
| 1011 | // calling a global function. |
| 1012 | //===----------------------------------------------------------------------=== |
| 1013 | |
| 1014 | int *pr7475_someGlobal; |
| 1015 | void pr7475_setUpGlobal(); |
| 1016 | |
| 1017 | void pr7475() { |
| 1018 | if (pr7475_someGlobal == 0) |
| 1019 | pr7475_setUpGlobal(); |
| 1020 | *pr7475_someGlobal = 0; // no-warning |
| 1021 | } |
| 1022 | |
| 1023 | void pr7475_warn() { |
| 1024 | static int *someStatic = 0; |
| 1025 | if (someStatic == 0) |
| 1026 | pr7475_setUpGlobal(); |
| 1027 | *someStatic = 0; // expected-warning{{null pointer}} |
| 1028 | } |
| 1029 | |
Ted Kremenek | cb07788 | 2010-07-17 01:28:55 +0000 | [diff] [blame] | 1030 | // <rdar://problem/8202272> - __imag passed non-complex should not crash |
| 1031 | float f0(_Complex float x) { |
| 1032 | float l0 = __real x; |
| 1033 | return __real l0 + __imag l0; |
| 1034 | } |
| 1035 | |
Jordy Rose | a277e77 | 2010-08-09 20:31:57 +0000 | [diff] [blame] | 1036 | |
| 1037 | //===----------------------------------------------------------------------=== |
| 1038 | // Test that we can reduce symbols to constants whether they are on the left |
| 1039 | // or right side of an expression. |
| 1040 | //===----------------------------------------------------------------------=== |
| 1041 | |
| 1042 | void reduce_to_constant(int x, int y) { |
| 1043 | if (x != 20) |
| 1044 | return; |
| 1045 | |
| 1046 | int a = x + y; |
| 1047 | int b = y + x; |
| 1048 | |
| 1049 | if (y == -20 && a != 0) |
| 1050 | (void)*(char*)0; // no-warning |
| 1051 | if (y == -20 && b != 0) |
| 1052 | (void)*(char*)0; // no-warning |
| 1053 | } |
Ted Kremenek | 34feff6 | 2010-08-26 22:19:33 +0000 | [diff] [blame] | 1054 | |
| 1055 | // <rdar://problem/8360854> - Test that code after a switch statement with no |
| 1056 | // 'case:' labels is correctly evaluated. |
| 1057 | void r8360854(int n) { |
| 1058 | switch (n) { |
| 1059 | default: ; |
| 1060 | } |
| 1061 | int *p = 0; |
| 1062 | *p = 0xDEADBEEF; // expected-warning{{null pointer}} |
| 1063 | } |
| 1064 | |
Ted Kremenek | f69cf18 | 2010-09-01 20:35:38 +0000 | [diff] [blame] | 1065 | // PR 8050 - crash in CastSizeChecker when pointee is an incomplete type |
| 1066 | typedef long unsigned int __darwin_size_t; |
| 1067 | typedef __darwin_size_t size_t; |
| 1068 | void *malloc(size_t); |
| 1069 | |
| 1070 | struct PR8050; |
| 1071 | |
| 1072 | void pr8050(struct PR8050 **arg) |
| 1073 | { |
| 1074 | *arg = malloc(1); |
| 1075 | } |
| 1076 | |
Ted Kremenek | 977a58a | 2010-09-09 00:40:43 +0000 | [diff] [blame] | 1077 | // <rdar://problem/5880430> Switch on enum should not consider default case live |
| 1078 | // if all enum values are covered |
| 1079 | enum Cases { C1, C2, C3, C4 }; |
| 1080 | void test_enum_cases(enum Cases C) { |
| 1081 | switch (C) { |
| 1082 | case C1: |
| 1083 | case C2: |
| 1084 | case C4: |
| 1085 | case C3: |
| 1086 | return; |
| 1087 | } |
| 1088 | int *p = 0; |
| 1089 | *p = 0xDEADBEEF; // no-warning |
| 1090 | } |
| 1091 | |
| 1092 | void test_enum_cases_positive(enum Cases C) { |
| 1093 | switch (C) { // expected-warning{{enumeration value 'C4' not handled in switch}} |
| 1094 | case C1: |
| 1095 | case C2: |
| 1096 | case C3: |
| 1097 | return; |
| 1098 | } |
| 1099 | int *p = 0; |
| 1100 | *p = 0xDEADBEEF; // expected-warning{{Dereference of null pointer}} |
| 1101 | } |
| 1102 | |
Ted Kremenek | 4f6aa77 | 2010-09-10 03:05:40 +0000 | [diff] [blame] | 1103 | // <rdar://problem/6351970> rule request: warn if synchronization mutex can be nil |
| 1104 | void rdar6351970() { |
| 1105 | id x = 0; |
| 1106 | @synchronized(x) {} // expected-warning{{Nil value used as mutex for @synchronized() (no synchronization will occur)}} |
| 1107 | } |
| 1108 | |
| 1109 | void rdar6351970_b(id x) { |
| 1110 | if (!x) |
| 1111 | @synchronized(x) {} // expected-warning{{Nil value used as mutex for @synchronized() (no synchronization will occur)}} |
| 1112 | } |
| 1113 | |
| 1114 | void rdar6351970_c() { |
| 1115 | id x; |
| 1116 | @synchronized(x) {} // expected-warning{{Uninitialized value used as mutex for @synchronized}} |
| 1117 | } |
Ted Kremenek | 977a58a | 2010-09-09 00:40:43 +0000 | [diff] [blame] | 1118 | |
Ted Kremenek | 1adee4b | 2010-10-21 15:38:55 +0000 | [diff] [blame] | 1119 | @interface Rdar8578650 |
| 1120 | - (id) foo8578650; |
| 1121 | @end |
| 1122 | |
| 1123 | void rdar8578650(id x) { |
| 1124 | @synchronized (x) { |
| 1125 | [x foo8578650]; |
| 1126 | } |
| 1127 | // At this point we should assume that 'x' is not nil, not |
| 1128 | // the inverse. |
| 1129 | @synchronized (x) { // no-warning |
| 1130 | } |
| 1131 | } |
| 1132 | |
Ted Kremenek | 0853c7f | 2010-09-10 20:20:49 +0000 | [diff] [blame] | 1133 | // <rdar://problem/6352035> rule request: direct structure member access null pointer dereference |
| 1134 | @interface RDar6352035 { |
| 1135 | int c; |
| 1136 | } |
| 1137 | - (void)foo; |
| 1138 | - (void)bar; |
| 1139 | @end |
| 1140 | |
| 1141 | @implementation RDar6352035 |
| 1142 | - (void)foo { |
| 1143 | RDar6352035 *friend = 0; |
| 1144 | friend->c = 7; // expected-warning{{Instance variable access (via 'friend') results in a null pointer dereference}} |
| 1145 | } |
| 1146 | - (void)bar { |
| 1147 | self = 0; |
| 1148 | c = 7; // expected-warning{{Instance variable access (via 'self') results in a null pointer dereference}} |
| 1149 | } |
| 1150 | @end |
| 1151 | |
Ted Kremenek | 8f3b834 | 2010-09-15 07:01:20 +0000 | [diff] [blame] | 1152 | // PR 8149 - GNU statement expression in condition of ForStmt. |
| 1153 | // This previously triggered an assertion failure in CFGBuilder. |
| 1154 | void pr8149(void) { |
| 1155 | for (; ({ do { } while (0); 0; });) { } |
| 1156 | } |
| 1157 | |
Ted Kremenek | 0d4f767 | 2010-10-25 20:20:56 +0000 | [diff] [blame] | 1158 | // PR 8458 - Make sure @synchronized doesn't crash with properties. |
| 1159 | @interface PR8458 {} |
| 1160 | @property(readonly) id lock; |
| 1161 | @end |
| 1162 | |
| 1163 | static |
| 1164 | void __PR8458(PR8458 *x) { |
| 1165 | @synchronized(x.lock) {} // no-warning |
| 1166 | } |
| 1167 | |
Ted Kremenek | c1143e5 | 2010-10-26 00:06:17 +0000 | [diff] [blame] | 1168 | // PR 8440 - False null dereference during store to array-in-field-in-global. |
| 1169 | // This test case previously resulted in a bogus null deref warning from |
| 1170 | // incorrect lazy symbolication logic in RegionStore. |
| 1171 | static struct { |
| 1172 | int num; |
| 1173 | char **data; |
| 1174 | } saved_pr8440; |
| 1175 | |
| 1176 | char *foo_pr8440(); |
| 1177 | char **bar_pr8440(); |
| 1178 | void baz_pr8440(int n) |
| 1179 | { |
| 1180 | saved_pr8440.num = n; |
| 1181 | if (saved_pr8440.data) |
| 1182 | return; |
| 1183 | saved_pr8440.data = bar_pr8440(); |
| 1184 | for (int i = 0 ; i < n ; i ++) |
| 1185 | saved_pr8440.data[i] = foo_pr8440(); // no-warning |
| 1186 | } |
| 1187 | |
Ted Kremenek | 29836f9 | 2010-11-11 23:10:10 +0000 | [diff] [blame] | 1188 | // Support direct accesses to non-null memory. Reported in: |
| 1189 | // PR 5272 |
| 1190 | // <rdar://problem/6839683> |
| 1191 | int test_direct_address_load() { |
| 1192 | int *p = (int*) 0x4000; |
| 1193 | return *p; // no-warning |
| 1194 | } |
| 1195 | |
| 1196 | void pr5272_test() { |
| 1197 | struct pr5272 { int var2; }; |
| 1198 | (*(struct pr5272*)0xBC000000).var2 = 0; // no-warning |
| 1199 | (*(struct pr5272*)0xBC000000).var2 += 2; // no-warning |
| 1200 | } |
| 1201 | |
Ted Kremenek | 948163b | 2010-11-15 20:09:42 +0000 | [diff] [blame] | 1202 | // Support casting the return value of function to another different type |
| 1203 | // This previously caused a crash, although we likely need more precise |
| 1204 | // reasoning here. <rdar://problem/8663544> |
| 1205 | void* rdar8663544(); |
| 1206 | typedef struct {} Val8663544; |
| 1207 | Val8663544 bazR8663544() { |
| 1208 | Val8663544(*func) () = (Val8663544(*) ()) rdar8663544; |
| 1209 | return func(); |
| 1210 | } |
| 1211 | |
Ted Kremenek | e4ae4dc | 2010-11-15 22:59:22 +0000 | [diff] [blame] | 1212 | // PR 8619 - Handle ternary expressions with a call to a noreturn function. |
| 1213 | // This previously resulted in a crash. |
| 1214 | void pr8619_noreturn(int x) __attribute__((noreturn)); |
| 1215 | |
| 1216 | void pr8619(int a, int b, int c) { |
| 1217 | a ?: pr8619_noreturn(b || c); |
| 1218 | } |
| 1219 | |
| 1220 | |
Ted Kremenek | ac518ec | 2011-02-11 20:13:27 +0000 | [diff] [blame] | 1221 | // PR 8646 - crash in the analyzer when handling unions. |
| 1222 | union pr8648_union { |
| 1223 | signed long long pr8648_union_field; |
| 1224 | }; |
| 1225 | void pr8648() { |
| 1226 | long long y; |
| 1227 | union pr8648_union x = { .pr8648_union_field = 0LL }; |
| 1228 | y = x.pr8648_union_field; |
| 1229 | |
| 1230 | union pr8648_union z; |
| 1231 | z = (union pr8648_union) { .pr8648_union_field = 0LL }; |
| 1232 | |
| 1233 | union pr8648_union w; |
| 1234 | w = ({ (union pr8648_union) { .pr8648_union_field = 0LL }; }); |
| 1235 | |
| 1236 | // crash, no assignment |
| 1237 | (void) ({ (union pr8648_union) { .pr8648_union_field = 0LL }; }).pr8648_union_field; |
| 1238 | |
| 1239 | // crash with assignment |
| 1240 | y = ({ (union pr8648_union) { .pr8648_union_field = 0LL }; }).pr8648_union_field; |
| 1241 | } |
Ted Kremenek | 6b12da9 | 2011-02-21 22:11:26 +0000 | [diff] [blame] | 1242 | |
| 1243 | // PR 9269 - don't assert when building the following CFG. The for statement |
| 1244 | // contains a condition with multiple basic blocks, and the value of the |
| 1245 | // statement expression is then indexed as part of a bigger condition expression. |
| 1246 | // This example exposed a bug in child traversal in the CFGBuilder. |
| 1247 | void pr9269() { |
| 1248 | struct s { char *bar[10]; } baz[2] = { 0 }; |
| 1249 | unsigned i = 0; |
| 1250 | for (i = 0; |
| 1251 | (* ({ while(0); ({ &baz[0]; }); })).bar[0] != 0; |
| 1252 | ++i) {} |
| 1253 | } |
| 1254 | |
Ted Kremenek | f226d18 | 2011-02-24 03:09:15 +0000 | [diff] [blame] | 1255 | // Test evaluation of GNU-style ?:. |
| 1256 | int pr9287(int type) { return type ? : 0; } // no-warning |
| 1257 | |
| 1258 | void pr9287_b(int type, int *p) { |
| 1259 | int x = type ? : 0; |
| 1260 | if (x) { |
| 1261 | p = 0; |
| 1262 | } |
| 1263 | if (type) { |
| 1264 | *p = 0xDEADBEEF; // expected-warning {{null pointer}} |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | void pr9287_c(int type, int *p) { |
| 1269 | int x = type ? : 0; |
| 1270 | if (x) { |
| 1271 | p = 0; |
| 1272 | } |
| 1273 | if (!type) { |
| 1274 | *p = 0xDEADBEEF; // no-warning |
| 1275 | } |
| 1276 | } |
| 1277 | |
Ted Kremenek | e71f3d5 | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 1278 | void test_switch() { |
| 1279 | switch (4) { |
| 1280 | case 1: { |
| 1281 | int *p = 0; |
| 1282 | *p = 0xDEADBEEF; // no-warning |
| 1283 | break; |
| 1284 | } |
| 1285 | case 4: { |
| 1286 | int *p = 0; |
| 1287 | *p = 0xDEADBEEF; // expected-warning {{null}} |
| 1288 | break; |
| 1289 | } |
| 1290 | default: { |
| 1291 | int *p = 0; |
| 1292 | *p = 0xDEADBEEF; // no-warning |
| 1293 | break; |
| 1294 | } |
| 1295 | } |
Ted Kremenek | e9cd9c0 | 2011-03-13 03:48:04 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | // PR 9467. Tests various CFG optimizations. This previously crashed. |
| 1299 | static void test(unsigned int bit_mask) |
| 1300 | { |
| 1301 | unsigned int bit_index; |
| 1302 | for (bit_index = 0; |
| 1303 | bit_index < 24; |
| 1304 | bit_index++) { |
| 1305 | switch ((0x01 << bit_index) & bit_mask) { |
| 1306 | case 0x100000: ; |
| 1307 | } |
| 1308 | } |
| 1309 | } |
Ted Kremenek | bc869de | 2011-05-10 18:42:15 +0000 | [diff] [blame] | 1310 | |
| 1311 | // Don't crash on code containing __label__. |
| 1312 | int radar9414427_aux(); |
| 1313 | void radar9414427() { |
| 1314 | __label__ mylabel; |
| 1315 | if (radar9414427_aux()) { |
| 1316 | mylabel: do {} |
| 1317 | while (0); |
| 1318 | } |
| 1319 | } |
| 1320 | |
Ted Kremenek | 6e744db | 2011-05-19 00:56:53 +0000 | [diff] [blame] | 1321 | // Analyze methods in @implementation (category) |
| 1322 | @interface RDar9465344 |
| 1323 | @end |
| 1324 | |
| 1325 | @implementation RDar9465344 (MyCategory) |
| 1326 | - (void) testcategoryImpl { |
| 1327 | int *p = 0x0; |
| 1328 | *p = 0xDEADBEEF; // expected-warning {{null}} |
| 1329 | } |
| 1330 | @end |
| 1331 | |
| 1332 | @implementation RDar9465344 |
| 1333 | @end |
| 1334 | |
Ted Kremenek | ccf1bfd | 2011-11-14 19:36:08 +0000 | [diff] [blame] | 1335 | // Don't crash when analyzing access to 'self' within a block. |
| 1336 | @interface Rdar10380300Base |
| 1337 | - (void) foo; |
| 1338 | @end |
| 1339 | @interface Rdar10380300 : Rdar10380300Base @end |
| 1340 | @implementation Rdar10380300 |
| 1341 | - (void)foo { |
| 1342 | ^{ |
| 1343 | [super foo]; |
| 1344 | }(); |
| 1345 | } |
| 1346 | @end |
| 1347 | |