blob: 21a15d75eca0ab55108e2e3146c441bc46cb1082 [file] [log] [blame]
Richard Trieu3bb8b562014-02-26 02:36:06 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core -verify -std=c99 -Dbool=_Bool -Wno-bool-conversion %s
2// RUN: %clang_cc1 -analyze -analyzer-checker=core -verify -x c++ -Wno-bool-conversion %s
Zhongxing Xu87e7fc52010-06-08 10:00:00 +00003
Jordan Rosedc166282013-05-01 18:19:59 +00004typedef __INTPTR_TYPE__ intptr_t;
Zhongxing Xu87e7fc52010-06-08 10:00:00 +00005char const *p;
6
7void f0() {
8 char const str[] = "This will change";
Jordan Rosee37ab502012-11-15 19:11:43 +00009 p = str;
10} // expected-warning{{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}}
Zhongxing Xu87e7fc52010-06-08 10:00:00 +000011
12void f1() {
13 char const str[] = "This will change";
14 p = str;
15 p = 0; // no-warning
16}
Zhongxing Xu4200be52010-06-09 05:50:38 +000017
18void f2() {
Jordan Rosee37ab502012-11-15 19:11:43 +000019 p = (const char *) __builtin_alloca(12);
Jordan Rosedc166282013-05-01 18:19:59 +000020} // expected-warning{{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}}
Ted Kremenek17504be2010-06-17 00:24:44 +000021
22// PR 7383 - previosly the stack address checker would crash on this example
23// because it would attempt to do a direct load from 'pr7383_list'.
24static int pr7383(__const char *__)
25{
26 return 0;
27}
28extern __const char *__const pr7383_list[];
Ted Kremenek5df037e2010-06-17 04:21:37 +000029
30// Test that we catch multiple returns via globals when analyzing a function.
31void test_multi_return() {
32 static int *a, *b;
33 int x;
34 a = &x;
Jordan Rosee37ab502012-11-15 19:11:43 +000035 b = &x;
36} // expected-warning{{Address of stack memory associated with local variable 'x' is still referred to by the global variable 'a' upon returning}} expected-warning{{Address of stack memory associated with local variable 'x' is still referred to by the global variable 'b' upon returning}}
Jordan Rosedc166282013-05-01 18:19:59 +000037
38intptr_t returnAsNonLoc() {
39 int x;
40 return (intptr_t)&x; // expected-warning{{Address of stack memory associated with local variable 'x' returned to caller}}
41}
42
43bool returnAsBool() {
44 int x;
45 return &x; // no-warning
46}
47
48void assignAsNonLoc() {
49 extern intptr_t ip;
50 int x;
51 ip = (intptr_t)&x;
52} // expected-warning{{Address of stack memory associated with local variable 'x' is still referred to by the global variable 'ip' upon returning}}
53
54void assignAsBool() {
55 extern bool b;
56 int x;
57 b = &x;
58} // no-warning