[compiler-rt] 59e422c - [lsan][test] Add malloc(0) and realloc(p, 0) tests

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 29 11:41:12 PDT 2021


Author: Fangrui Song
Date: 2021-03-29T11:41:07-07:00
New Revision: 59e422c90bf4796fc73237e838d8954b4e2099b1

URL: https://github.com/llvm/llvm-project/commit/59e422c90bf4796fc73237e838d8954b4e2099b1
DIFF: https://github.com/llvm/llvm-project/commit/59e422c90bf4796fc73237e838d8954b4e2099b1.diff

LOG: [lsan][test] Add malloc(0) and realloc(p, 0) tests

Added: 
    compiler-rt/test/lsan/TestCases/malloc_zero.c
    compiler-rt/test/lsan/TestCases/realloc_zero.c

Modified: 
    

Removed: 
    


################################################################################
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 000000000000..daefe71ab1d6
--- /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 <stdio.h>
+#include <stdlib.h>
+
+// 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 000000000000..d4ce4754d9bd
--- /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 <assert.h>
+#include <stdlib.h>
+
+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;
+}


        


More information about the llvm-commits mailing list