1// RUN: %clangxx_asan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s
2
3#include <stdio.h>
4
5struct IntHolder {
6 explicit IntHolder(int *val = 0) : val_(val) { }
7 __attribute__((noinline)) ~IntHolder() {
8 printf(format: "Value: %d\n", *val_); // BOOM
9 // CHECK: ERROR: AddressSanitizer: stack-use-after-scope
10 // CHECK: #0 0x{{.*}} in IntHolder::~IntHolder{{.*}}.cpp:[[@LINE-2]]
11 }
12 void set(int *val) { val_ = val; }
13 int *get() { return val_; }
14
15 int *val_;
16};
17
18int main(int argc, char *argv[]) {
19 // It is incorrect to use "x" int IntHolder destructor, because "x" is
20 // "destroyed" earlier as it's declared later.
21 IntHolder holder;
22 int x = argc;
23 holder.set(&x);
24 return 0;
25}
26

source code of compiler-rt/test/asan/TestCases/use-after-scope-dtor-order.cpp