34 lines · cpp
1// Test for LeakSanitizer+AddressSanitizer of different sizes.2// REQUIRES: leak-detection3//4// RUN: %clangxx_asan -O0 %s -o %t5// RUN: not %run %t 0 2>&1 | FileCheck %s6// RUN: not %run %t 1 2>&1 | FileCheck %s7// RUN: not %run %t 1000 2>&1 | FileCheck %s8// RUN: not %run %t 1000000 2>&1 | FileCheck %s9 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, hence16 // there would not be a leak.17 // Re-adjust n so the test can pass.18 if (n == 0)19 n = 1;20#endif21 22 // Repeat few times to make sure that at least one pointer is23 // 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 30int main(int argc, char **argv) {31 leak(atoi(argv[1]));32}33// CHECK: LeakSanitizer: detected memory leaks34