xref: /llvm-project/compiler-rt/test/asan/TestCases/Windows/use_after_realloc.cpp (revision 53a81d4d26f0409de8a0655d7af90f2bea222a12)
1 // RUN: %clang_cl_asan %Od %s %Fe%t
2 // RUN: not %run %t 2>&1 | FileCheck %s
3 
4 #include <malloc.h>
5 
6 int main() {
7   char *buffer = (char*)realloc(0, 32),
8        *stale = buffer;
9   buffer = (char*)realloc(buffer, 64);
10   // The 'stale' may now point to a free'd memory.
11   stale[0] = 42;
12   // CHECK: AddressSanitizer: heap-use-after-free on address [[ADDR:0x[0-9a-f]+]]
13   // CHECK: WRITE of size 1 at [[ADDR]] thread T0
14   // CHECK-NEXT: {{#0 .* main .*use_after_realloc.cpp}}:[[@LINE-3]]
15   // CHECK: [[ADDR]] is located 0 bytes inside of 32-byte region
16   // CHECK: freed by thread T0 here:
17   // CHECK-NEXT: {{#0 .* realloc }}
18   // CHECK: {{ #[1-3] .* main .*use_after_realloc.cpp}}:[[@LINE-9]]
19   // CHECK: previously allocated by thread T0 here:
20   // CHECK-NEXT: {{#0 .* realloc }}
21   // CHECK: {{ #[1-3] .* main .*use_after_realloc.cpp}}:[[@LINE-14]]
22   free(buffer);
23 }
24