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

This commit is contained in:
Fangrui Song 2021-03-29 11:41:07 -07:00
parent 9b0517035f
commit 59e422c90b
2 changed files with 28 additions and 0 deletions

View File

@ -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;
}

View File

@ -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;
}