xref: /llvm-project/compiler-rt/test/asan/TestCases/use-after-scope-goto.cpp (revision cd269daf25016f7115424a51ab5e7977a80e072d)
1 // RUN: %clangxx_asan -O0 %s -o %t && %run %t
2 
3 // Function jumps over variable initialization making lifetime analysis
4 // ambiguous. Asan should ignore such variable and program must not fail.
5 
6 #include <stdlib.h>
7 
8 int *ptr;
9 
f1(int cond)10 void f1(int cond) {
11   if (cond)
12     goto label;
13   int tmp;
14 
15  label:
16   ptr = &tmp;
17   *ptr = 5;
18 }
19 
f2(int cond)20 void f2(int cond) {
21   switch (cond) {
22   case 1: {
23     ++cond;
24     int tmp;
25     ptr = &tmp;
26     exit(0);
27   case 2:
28     ptr = &tmp;
29     *ptr = 5;
30     exit(0);
31   }
32   }
33 }
34 
f3(int cond)35 void f3(int cond) {
36   {
37     int tmp;
38     goto l2;
39   l1:
40     ptr = &tmp;
41     *ptr = 5;
42 
43     exit(0);
44   }
45  l2:
46   goto l1;
47 }
48 
use(int * x)49 void use(int *x) {
50   static int c = 10;
51   if (--c == 0)
52     exit(0);
53   (*x)++;
54 }
55 
f4()56 void f4() {
57   {
58     int x;
59  l2:
60     use(&x);
61     goto l1;
62   }
63  l1:
64   goto l2;
65 }
66 
main()67 int main() {
68   f1(1);
69   f2(1);
70   f3(1);
71   f4();
72   return 0;
73 }
74