1 // Test for LeakSanitizer+AddressSanitizer of different sizes.
2 // REQUIRES: leak-detection
3 //
4 // RUN: %clangxx_asan -O0 %s -o %t
5 // RUN: not %run %t 0 2>&1 | FileCheck %s
6 // RUN: not %run %t 1 2>&1 | FileCheck %s
7 // RUN: not %run %t 1000 2>&1 | FileCheck %s
8 // RUN: not %run %t 1000000 2>&1 | FileCheck %s
9 // RUN: not %run %t 10000000 2>&1 | FileCheck %s
10 
11 #include <cstdlib>
12 #include <stdio.h>
13 #include <thread>
14 int *t;
15 
leak(int n)16 __attribute__((noopt)) void leak(int n) {
17 #if defined(__ANDROID__) || defined(__BIONIC__)
18   // Bionic does not acutally allocate when n==0, hence
19   // there would not be a leak.
20   // Re-adjust n so the test can pass.
21   if (n == 0)
22     n = 1;
23 #endif
24 
25   // Repeat few times to make sure that at least one pointer is
26   // not somewhere on the stack.
27   for (int i = 0; i < 10; ++i) {
28     t = new int[n];
29     printf("t: %p\n", t);
30     t = 0;
31   }
32 }
33 
main(int argc,char ** argv)34 int main(int argc, char **argv) {
35   leak(atoi(argv[1]));
36 }
37 // CHECK: LeakSanitizer: detected memory leaks
38