1651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// RUN: %clang_cc1 -analyze -analyzer-checker=core -verify -std=c99 -Dbool=_Bool -Wno-bool-conversion %s
2651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// RUN: %clang_cc1 -analyze -analyzer-checker=core -verify -x c++ -Wno-bool-conversion %s
31622a547971cee50e386b4cdfe62ed1fcee1036dZhongxing Xu
4112344ab7f96cf482bce80530676712c282756d5Jordan Rosetypedef __INTPTR_TYPE__ intptr_t;
51622a547971cee50e386b4cdfe62ed1fcee1036dZhongxing Xuchar const *p;
61622a547971cee50e386b4cdfe62ed1fcee1036dZhongxing Xu
71622a547971cee50e386b4cdfe62ed1fcee1036dZhongxing Xuvoid f0() {
81622a547971cee50e386b4cdfe62ed1fcee1036dZhongxing Xu  char const str[] = "This will change";
963bc186d6ac0b44ba4ec6fccb5f471b05c79b666Jordan Rose  p = str;
1063bc186d6ac0b44ba4ec6fccb5f471b05c79b666Jordan Rose}  // 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}}
111622a547971cee50e386b4cdfe62ed1fcee1036dZhongxing Xu
121622a547971cee50e386b4cdfe62ed1fcee1036dZhongxing Xuvoid f1() {
131622a547971cee50e386b4cdfe62ed1fcee1036dZhongxing Xu  char const str[] = "This will change";
141622a547971cee50e386b4cdfe62ed1fcee1036dZhongxing Xu  p = str;
151622a547971cee50e386b4cdfe62ed1fcee1036dZhongxing Xu  p = 0; // no-warning
161622a547971cee50e386b4cdfe62ed1fcee1036dZhongxing Xu}
172c46458d4cd96a3a33e8810e95e692d8e2e05ff3Zhongxing Xu
182c46458d4cd96a3a33e8810e95e692d8e2e05ff3Zhongxing Xuvoid f2() {
1963bc186d6ac0b44ba4ec6fccb5f471b05c79b666Jordan Rose  p = (const char *) __builtin_alloca(12);
20112344ab7f96cf482bce80530676712c282756d5Jordan Rose} // 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}}
21551bd1f9191af0eecdc29764e34e01803c73ae31Ted Kremenek
22551bd1f9191af0eecdc29764e34e01803c73ae31Ted Kremenek// PR 7383 - previosly the stack address checker would crash on this example
23551bd1f9191af0eecdc29764e34e01803c73ae31Ted Kremenek//  because it would attempt to do a direct load from 'pr7383_list'.
24551bd1f9191af0eecdc29764e34e01803c73ae31Ted Kremenekstatic int pr7383(__const char *__)
25551bd1f9191af0eecdc29764e34e01803c73ae31Ted Kremenek{
26551bd1f9191af0eecdc29764e34e01803c73ae31Ted Kremenek  return 0;
27551bd1f9191af0eecdc29764e34e01803c73ae31Ted Kremenek}
28551bd1f9191af0eecdc29764e34e01803c73ae31Ted Kremenekextern __const char *__const pr7383_list[];
29a8166156a6414ddd6a68514dc4f48e95d2259977Ted Kremenek
30a8166156a6414ddd6a68514dc4f48e95d2259977Ted Kremenek// Test that we catch multiple returns via globals when analyzing a function.
31a8166156a6414ddd6a68514dc4f48e95d2259977Ted Kremenekvoid test_multi_return() {
32a8166156a6414ddd6a68514dc4f48e95d2259977Ted Kremenek  static int *a, *b;
33a8166156a6414ddd6a68514dc4f48e95d2259977Ted Kremenek  int x;
34a8166156a6414ddd6a68514dc4f48e95d2259977Ted Kremenek  a = &x;
3563bc186d6ac0b44ba4ec6fccb5f471b05c79b666Jordan Rose  b = &x;
3663bc186d6ac0b44ba4ec6fccb5f471b05c79b666Jordan Rose} // 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}}
37112344ab7f96cf482bce80530676712c282756d5Jordan Rose
38112344ab7f96cf482bce80530676712c282756d5Jordan Roseintptr_t returnAsNonLoc() {
39112344ab7f96cf482bce80530676712c282756d5Jordan Rose  int x;
40112344ab7f96cf482bce80530676712c282756d5Jordan Rose  return (intptr_t)&x; // expected-warning{{Address of stack memory associated with local variable 'x' returned to caller}}
41112344ab7f96cf482bce80530676712c282756d5Jordan Rose}
42112344ab7f96cf482bce80530676712c282756d5Jordan Rose
43112344ab7f96cf482bce80530676712c282756d5Jordan Rosebool returnAsBool() {
44112344ab7f96cf482bce80530676712c282756d5Jordan Rose  int x;
45112344ab7f96cf482bce80530676712c282756d5Jordan Rose  return &x; // no-warning
46112344ab7f96cf482bce80530676712c282756d5Jordan Rose}
47112344ab7f96cf482bce80530676712c282756d5Jordan Rose
48112344ab7f96cf482bce80530676712c282756d5Jordan Rosevoid assignAsNonLoc() {
49112344ab7f96cf482bce80530676712c282756d5Jordan Rose  extern intptr_t ip;
50112344ab7f96cf482bce80530676712c282756d5Jordan Rose  int x;
51112344ab7f96cf482bce80530676712c282756d5Jordan Rose  ip = (intptr_t)&x;
52112344ab7f96cf482bce80530676712c282756d5Jordan Rose} // expected-warning{{Address of stack memory associated with local variable 'x' is still referred to by the global variable 'ip' upon returning}}
53112344ab7f96cf482bce80530676712c282756d5Jordan Rose
54112344ab7f96cf482bce80530676712c282756d5Jordan Rosevoid assignAsBool() {
55112344ab7f96cf482bce80530676712c282756d5Jordan Rose  extern bool b;
56112344ab7f96cf482bce80530676712c282756d5Jordan Rose  int x;
57112344ab7f96cf482bce80530676712c282756d5Jordan Rose  b = &x;
58112344ab7f96cf482bce80530676712c282756d5Jordan Rose} // no-warning
59