diff --git a/compiler-rt/lib/lsan/lsan_allocator.cpp b/compiler-rt/lib/lsan/lsan_allocator.cpp index b4fd7e904be0f..43928ad294e2c 100644 --- a/compiler-rt/lib/lsan/lsan_allocator.cpp +++ b/compiler-rt/lib/lsan/lsan_allocator.cpp @@ -146,6 +146,8 @@ void GetAllocatorCacheRange(uptr *begin, uptr *end) { } uptr GetMallocUsableSize(const void *p) { + if (!p) + return 0; ChunkMetadata *m = Metadata(p); if (!m) return 0; return m->requested_size; diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/malloc_usable_size.c b/compiler-rt/test/sanitizer_common/TestCases/Linux/malloc_usable_size.c new file mode 100644 index 0000000000000..c51ab73774eea --- /dev/null +++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/malloc_usable_size.c @@ -0,0 +1,21 @@ +// RUN: %clang -O2 %s -o %t && %run %t + +// Ubsan does not provide allocator. +// UNSUPPORTED: ubsan + +#include +#include +#include +#include + +int main() { + assert(__sanitizer_get_allocated_size(NULL) == 0); + assert(malloc_usable_size(NULL) == 0); + + int size = 1234567; + void *p = malloc(size); + assert(__sanitizer_get_allocated_size(p) == size); + assert(malloc_usable_size(p) == size); + free(p); + return 0; +}