1// RUN: %clangxx_asan -O0 %s -o %t
2// Default is true (free on realloc to 0 size)
3// RUN: %run %t 2>&1 | FileCheck %s
4// RUN: %env_asan_opts=allocator_frees_and_returns_null_on_realloc_zero=true %run %t 2>&1 | FileCheck %s
5// RUN: %env_asan_opts=allocator_frees_and_returns_null_on_realloc_zero=false %run %t 2>&1 | FileCheck %s --check-prefix=NO-FREE
6
7#include <stdio.h>
8#include <stdlib.h>
9
10int main() {
11 void *p = malloc(size: 42);
12 p = realloc(ptr: p, size: 0);
13 if (p) {
14 // NO-FREE: Allocated something on realloc(p, 0)
15 fprintf(stderr, format: "Allocated something on realloc(p, 0)\n");
16 } else {
17 // CHECK: realloc(p, 0) returned nullptr
18 fprintf(stderr, format: "realloc(p, 0) returned nullptr\n");
19 }
20 free(ptr: p);
21}
22

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