1 // RUN: %clangxx_asan -O0 -fsanitize=use-after-scope %s -o %t && \ 2 // RUN: not %t 2>&1 | FileCheck %s 3 #include <stdio.h> 4 5 struct IntHolder { 6 explicit IntHolder(int *val = 0) : val_(val) { } 7 ~IntHolder() { 8 printf("Value: %d\n", *val_); // BOOM 9 // CHECK: ERROR: AddressSanitizer: stack-use-after-scope 10 // CHECK: #0 0x{{.*}} in IntHolder::~IntHolder{{.*}}use-after-scope-dtor-order.cc:[[@LINE-2]] 11 } 12 void set(int *val) { val_ = val; } 13 int *get() { return val_; } 14 15 int *val_; 16 }; 17 18 int 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