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
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
13 #include "Inputs/system-header-simulator-objc.h"
15 typedef unsigned long size_t;
16 void *calloc(size_t nmemb, size_t size);
18 typedef void (^dispatch_block_t)(void);
19 typedef long dispatch_once_t;
20 void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block);
22 void test_stack(void) {
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'?}}
27 void test_static_local(void) {
28 static dispatch_once_t once;
29 dispatch_once(&once, ^{}); // no-warning
32 void test_heap_var(void) {
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$}}}}
38 void test_external_pointer(dispatch_once_t *once) {
39 // External pointer does not necessarily point to the heap.
40 dispatch_once(once, ^{}); // no-warning
47 void test_local_struct(void) {
49 dispatch_once(&s.once, ^{}); // expected-warning{{Call to 'dispatch_once' uses memory within the local variable 's' for the predicate value.}}
52 void test_heap_struct(void) {
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.}}
57 @interface Object : NSObject {
61 dispatch_once_t once_array[2];
63 - (void)test_ivar_from_inside;
64 - (void)test_ivar_struct_from_inside;
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.}}
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.}}
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.}}
79 void test_ivar_from_alloc_init(void) {
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.}}
83 void test_ivar_struct_from_alloc_init(void) {
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.}}
87 void test_ivar_array_from_alloc_init(void) {
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.}}
92 void 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.}}
96 void 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.}}
99 void 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.}}
103 void test_block_var_from_block(void) {
104 __block dispatch_once_t once;
106 dispatch_once(&once, ^{}); // expected-warning{{Call to 'dispatch_once' uses the block variable 'once' for the predicate value.}}
110 void use_block_var(dispatch_once_t *once);
112 void test_block_var_from_outside_block(void) {
113 __block dispatch_once_t once;
115 use_block_var(&once);
117 dispatch_once(&once, ^{}); // expected-warning{{Call to 'dispatch_once' uses the block variable 'once' for the predicate value.}}
120 void test_static_var_from_outside_block(void) {
121 static dispatch_once_t once;
123 dispatch_once(&once, ^{}); // no-warning