| 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 | |
| 10 | #include <cstdlib> |
| 11 | #include <thread> |
| 12 | |
| 13 | __attribute__((noopt)) void leak(int n) { |
| 14 | #if defined(__ANDROID__) || defined(__BIONIC__) |
| 15 | // Bionic does not acutally allocate when n==0, hence |
| 16 | // there would not be a leak. |
| 17 | // Re-adjust n so the test can pass. |
| 18 | if (n == 0) |
| 19 | n = 1; |
| 20 | #endif |
| 21 | |
| 22 | // Repeat few times to make sure that at least one pointer is |
| 23 | // not somewhere on the stack. |
| 24 | for (int i = 0; i < 10; ++i) { |
| 25 | volatile int *t = new int[n]; |
| 26 | t = nullptr; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | int main(int argc, char **argv) { |
| 31 | leak(atoi(argv[1])); |
| 32 | } |
| 33 | // CHECK: LeakSanitizer: detected memory leaks |
| 34 | |