[compiler-rt] 7788b0c - [lsan] malloc_usable_size returns 0 for nullptr

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 2 20:16:44 PDT 2022


Author: Vitaly Buka
Date: 2022-07-02T20:16:30-07:00
New Revision: 7788b0c09754312445fb5296d9042740aa781880

URL: https://github.com/llvm/llvm-project/commit/7788b0c09754312445fb5296d9042740aa781880
DIFF: https://github.com/llvm/llvm-project/commit/7788b0c09754312445fb5296d9042740aa781880.diff

LOG: [lsan] malloc_usable_size returns 0 for nullptr

Added: 
    compiler-rt/test/sanitizer_common/TestCases/Linux/malloc_usable_size.c

Modified: 
    compiler-rt/lib/lsan/lsan_allocator.cpp

Removed: 
    


################################################################################
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 <assert.h>
+#include <malloc.h>
+#include <sanitizer/allocator_interface.h>
+#include <stdlib.h>
+
+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;
+}


        


More information about the llvm-commits mailing list