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
30int main(int argc, char **argv) {
31 leak(atoi(argv[1]));
32}
33// CHECK: LeakSanitizer: detected memory leaks
34

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of compiler-rt/test/asan/TestCases/leaks.cpp