1// RUN: %clang_analyze_cc1 -w -fblocks -verify %s \ 2// RUN: -analyzer-checker=core \ 3// RUN: -analyzer-checker=osx.API \ 4// RUN: -analyzer-checker=unix.Malloc \ 5// RUN: -analyzer-config display-checker-name=false 6 7// RUN: %clang_analyze_cc1 -w -fblocks -fobjc-arc -verify %s \ 8// RUN: -analyzer-checker=core \ 9// RUN: -analyzer-checker=osx.API \ 10// RUN: -analyzer-checker=unix.Malloc \ 11// RUN: -analyzer-config display-checker-name=false 12 13#include "Inputs/system-header-simulator-objc.h" 14 15typedef unsigned long size_t; 16void *calloc(size_t nmemb, size_t size); 17 18typedef void (^dispatch_block_t)(void); 19typedef long dispatch_once_t; 20void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block); 21 22void test_stack() { 23 dispatch_once_t once; 24 dispatch_once(&once, ^{}); // expected-warning{{Call to 'dispatch_once' uses the local variable 'once' for the predicate value. Using such transient memory for the predicate is potentially dangerous. Perhaps you intended to declare the variable as 'static'?}} 25} 26 27void test_static_local() { 28 static dispatch_once_t once; 29 dispatch_once(&once, ^{}); // no-warning 30} 31 32void test_heap_var() { 33 dispatch_once_t *once = calloc(1, sizeof(dispatch_once_t)); 34 // Use regexps to check that we're NOT suggesting to make this static. 35 dispatch_once(once, ^{}); // expected-warning-re{{{{^Call to 'dispatch_once' uses heap-allocated memory for the predicate value. Using such transient memory for the predicate is potentially dangerous$}}}} 36} 37 38void test_external_pointer(dispatch_once_t *once) { 39 // External pointer does not necessarily point to the heap. 40 dispatch_once(once, ^{}); // no-warning 41} 42 43typedef struct { 44 dispatch_once_t once; 45} Struct; 46 47void test_local_struct() { 48 Struct s; 49 dispatch_once(&s.once, ^{}); // expected-warning{{Call to 'dispatch_once' uses memory within the local variable 's' for the predicate value.}} 50} 51 52void test_heap_struct() { 53 Struct *s = calloc(1, sizeof(Struct)); 54 dispatch_once(&s->once, ^{}); // expected-warning{{Call to 'dispatch_once' uses heap-allocated memory for the predicate value.}} 55} 56 57@interface Object : NSObject { 58@public 59 dispatch_once_t once; 60 Struct s; 61 dispatch_once_t once_array[2]; 62} 63- (void)test_ivar_from_inside; 64- (void)test_ivar_struct_from_inside; 65@end 66 67@implementation Object 68- (void)test_ivar_from_inside { 69 dispatch_once(&once, ^{}); // expected-warning{{Call to 'dispatch_once' uses the instance variable 'once' for the predicate value.}} 70} 71- (void)test_ivar_struct_from_inside { 72 dispatch_once(&s.once, ^{}); // expected-warning{{Call to 'dispatch_once' uses memory within the instance variable 's' for the predicate value.}} 73} 74- (void)test_ivar_array_from_inside { 75 dispatch_once(&once_array[1], ^{}); // expected-warning{{Call to 'dispatch_once' uses memory within the instance variable 'once_array' for the predicate value.}} 76} 77@end 78 79void test_ivar_from_alloc_init() { 80 Object *o = [[Object alloc] init]; 81 dispatch_once(&o->once, ^{}); // expected-warning{{Call to 'dispatch_once' uses the instance variable 'once' for the predicate value.}} 82} 83void test_ivar_struct_from_alloc_init() { 84 Object *o = [[Object alloc] init]; 85 dispatch_once(&o->s.once, ^{}); // expected-warning{{Call to 'dispatch_once' uses memory within the instance variable 's' for the predicate value.}} 86} 87void test_ivar_array_from_alloc_init() { 88 Object *o = [[Object alloc] init]; 89 dispatch_once(&o->once_array[1], ^{}); // expected-warning{{Call to 'dispatch_once' uses memory within the instance variable 'once_array' for the predicate value.}} 90} 91 92void test_ivar_from_external_obj(Object *o) { 93 // ObjC object pointer always points to the heap. 94 dispatch_once(&o->once, ^{}); // expected-warning{{Call to 'dispatch_once' uses the instance variable 'once' for the predicate value.}} 95} 96void test_ivar_struct_from_external_obj(Object *o) { 97 dispatch_once(&o->s.once, ^{}); // expected-warning{{Call to 'dispatch_once' uses memory within the instance variable 's' for the predicate value.}} 98} 99void test_ivar_array_from_external_obj(Object *o) { 100 dispatch_once(&o->once_array[1], ^{}); // expected-warning{{Call to 'dispatch_once' uses memory within the instance variable 'once_array' for the predicate value.}} 101} 102 103void test_block_var_from_block() { 104 __block dispatch_once_t once; 105 ^{ 106 dispatch_once(&once, ^{}); // expected-warning{{Call to 'dispatch_once' uses the block variable 'once' for the predicate value.}} 107 }; 108} 109 110void use_block_var(dispatch_once_t *once); 111 112void test_block_var_from_outside_block() { 113 __block dispatch_once_t once; 114 ^{ 115 use_block_var(&once); 116 }; 117 dispatch_once(&once, ^{}); // expected-warning{{Call to 'dispatch_once' uses the block variable 'once' for the predicate value.}} 118} 119 120void test_static_var_from_outside_block() { 121 static dispatch_once_t once; 122 ^{ 123 dispatch_once(&once, ^{}); // no-warning 124 }; 125} 126