From 7eda134fa7733e84424359c6ac23cd4ce1045435 Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Mon, 28 Jan 2013 11:24:13 +0000 Subject: [PATCH] [ASan] fix a bug in allocator-v2 which could lead to SEGV on realloc(malloc(0), 4) llvm-svn: 173681 --- compiler-rt/lib/asan/asan_allocator2.cc | 2 +- compiler-rt/lib/asan/tests/asan_test.cc | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/compiler-rt/lib/asan/asan_allocator2.cc b/compiler-rt/lib/asan/asan_allocator2.cc index 63cb41b4c97c..eae4bf5ee0de 100644 --- a/compiler-rt/lib/asan/asan_allocator2.cc +++ b/compiler-rt/lib/asan/asan_allocator2.cc @@ -612,7 +612,7 @@ void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack) { } void *asan_realloc(void *p, uptr size, StackTrace *stack) { - if (p == 0) + if (p == 0 || reinterpret_cast(p) == kReturnOnZeroMalloc) return Allocate(size, 8, stack, FROM_MALLOC); if (size == 0) { Deallocate(p, stack, FROM_MALLOC); diff --git a/compiler-rt/lib/asan/tests/asan_test.cc b/compiler-rt/lib/asan/tests/asan_test.cc index 487ed97a5742..f7b6097e4bfc 100644 --- a/compiler-rt/lib/asan/tests/asan_test.cc +++ b/compiler-rt/lib/asan/tests/asan_test.cc @@ -380,6 +380,15 @@ TEST(AddressSanitizer, ReallocTest) { (my_rand() % 1000 + kMinElem) * sizeof(int)); EXPECT_EQ(3, ptr[3]); } + free(ptr); + // Realloc pointer returned by malloc(0). + int *ptr2 = Ident((int*)malloc(0)); + fprintf(stderr, "Malloc(0): %p\n", ptr2); + ptr2 = Ident((int*)realloc(ptr2, sizeof(*ptr2))); + fprintf(stderr, "Realloc(0, 4): %p\n", ptr2); + *ptr2 = 42; + EXPECT_EQ(42, *ptr2); + free(ptr2); } #ifndef __APPLE__