[lsan] malloc_usable_size returns 0 for nullptr

This commit is contained in:
Vitaly Buka 2022-07-02 20:14:51 -07:00
parent 5148c685e3
commit 7788b0c097
2 changed files with 23 additions and 0 deletions

View File

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

View File

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