Debian Codegraph @

clang/test/Analysis/stackaddrleak.c

raw
1 // RUN: %clang_analyze_cc1 -fblocks -analyzer-checker=core,unix.Malloc -verify -std=c99 -Dbool=_Bool -Wno-bool-conversion %s
2 // RUN: %clang_analyze_cc1 -fblocks -analyzer-checker=core,unix.Malloc -verify -x c++ -Wno-bool-conversion %s
3
4 typedef __INTPTR_TYPE__ intptr_t;
5 char const *p;
6
7 void f0(void) {
8 char const str[] = "This will change";
9 p = str;
10 } // expected-warning@-1{{Address of stack memory associated with local variable 'str' is still referred to by the global variable 'p' upon returning to the caller. This will be a dangling reference}}
11
12 void f1(void) {
13 char const str[] = "This will change";
14 p = str;
15 p = 0; // no-warning
16 }
17
18 void f2(void) {
19 p = (const char *) __builtin_alloca(12);
20 } // expected-warning@-1{{Address of stack memory allocated by call to alloca() on line 19 is still referred to by the global variable 'p' upon returning to the caller. This will be a dangling reference}}
21
22 // PR 7383 - previously the stack address checker would crash on this example
23 // because it would attempt to do a direct load from 'pr7383_list'.
24 static int pr7383(__const char *__)
25 {
26 return 0;
27 }
28 extern __const char *__const pr7383_list[];
29
30 // Test that we catch multiple returns via globals when analyzing a function.
31 void test_multi_return(void) {
32 static int *a, *b;
33 int x;
34 a = &x;
35 b = &x;
36 } // expected-warning@-1{{Address of stack memory associated with local variable 'x' is still referred to by the static variable 'a' upon returning}} expected-warning@-1{{Address of stack memory associated with local variable 'x' is still referred to by the static variable 'b' upon returning}}
37
38 intptr_t returnAsNonLoc(void) {
39 int x;
40 return (intptr_t)&x; // expected-warning{{Address of stack memory associated with local variable 'x' returned to caller}} expected-warning{{address of stack memory associated with local variable 'x' returned}}
41 }
42
43 bool returnAsBool(void) {
44 int x;
45 return &x; // no-warning
46 }
47
48 void assignAsNonLoc(void) {
49 extern intptr_t ip;
50 int x;
51 ip = (intptr_t)&x;
52 } // expected-warning@-1{{Address of stack memory associated with local variable 'x' is still referred to by the global variable 'ip' upon returning}}
53
54 void assignAsBool(void) {
55 extern bool b;
56 int x;
57 b = &x;
58 } // no-warning
59
60 int *f(int* p __attribute__((lifetimebound)));
61 int *g() {
62 int i;
63 return f(&i); // expected-warning {{address of stack memory associated with local variable 'i' returned}}
64 }
65
66 int *f_no_lifetime_bound(int *p);
67 int *g_no_lifetime_bound() {
68 int i = 0;
69 return f_no_lifetime_bound(&i); // no-warning
70 }
71
72 struct child_stack_context_s {
73 int *p;
74 };
75
76 struct child_stack_context_s return_child_stack_context() {
77 struct child_stack_context_s s;
78 {
79 int a = 1;
80 s = (struct child_stack_context_s){ &a };
81 }
82 return s; // expected-warning {{Address of stack memory associated with local variable 'a' returned to caller}}
83 }
84
85 struct child_stack_context_s return_child_stack_context_field() {
86 struct child_stack_context_s s;
87 {
88 int a = 1;
89 s.p = &a;
90 }
91 return s; // expected-warning {{Address of stack memory associated with local variable 'a' returned to caller}}
92 }
93
94 // Returns an 'int' block taking an 'int'.
95 int (^copy_self_referencing_block(void))(int) {
96 // It is important that the 'fib' block captures itself.
97 __block int (^fib)(int) = ^(int n) {
98 if (n <= 1) return n;
99 return fib(n - 1) + fib(n - 2);
100 };
101 return fib; // no-crash when copying a self-referencing 'fib'
102 // expected-warning-re@-1 {{Address of stack-allocated block declared on line {{[0-9]+}} is captured by a returned block}}
103 }