From 59e422c90bf4796fc73237e838d8954b4e2099b1 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Mon, 29 Mar 2021 11:41:07 -0700 Subject: [PATCH] [lsan][test] Add malloc(0) and realloc(p, 0) tests --- compiler-rt/test/lsan/TestCases/malloc_zero.c | 15 +++++++++++++++ compiler-rt/test/lsan/TestCases/realloc_zero.c | 13 +++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 compiler-rt/test/lsan/TestCases/malloc_zero.c create mode 100644 compiler-rt/test/lsan/TestCases/realloc_zero.c diff --git a/compiler-rt/test/lsan/TestCases/malloc_zero.c b/compiler-rt/test/lsan/TestCases/malloc_zero.c new file mode 100644 index 0000000..daefe71 --- /dev/null +++ b/compiler-rt/test/lsan/TestCases/malloc_zero.c @@ -0,0 +1,15 @@ +// RUN: %clang_lsan %s -o %t +// RUN: %env_lsan_opts=use_stacks=0 not %run %t 2>&1 | FileCheck %s + +#include +#include + +// CHECK: {{Leak|Address}}Sanitizer: detected memory leaks +// CHECK: {{Leak|Address}}Sanitizer: 1 byte(s) leaked in 1 allocation(s). + +int main() { + // The behavior of malloc(0) is implementation-defined. + char *p = malloc(0); + fprintf(stderr, "zero: %p\n", p); + p = 0; +} diff --git a/compiler-rt/test/lsan/TestCases/realloc_zero.c b/compiler-rt/test/lsan/TestCases/realloc_zero.c new file mode 100644 index 0000000..d4ce475 --- /dev/null +++ b/compiler-rt/test/lsan/TestCases/realloc_zero.c @@ -0,0 +1,13 @@ +// RUN: %clang_lsan %s -o %t +// RUN: %run %t + +#include +#include + +int main() { + char *p = malloc(1); + // The behavior of realloc(p, 0) is implementation-defined. + // We free the allocation. + assert(realloc(p, 0) == NULL); + p = 0; +} -- 2.7.4