[llvm-commits] [compiler-rt] r173681 - [ASan] fix a bug in allocator-v2 which could lead to SEGV on realloc(malloc(0), 4)

Alexey Samsonov samsonov at google.com
Mon Jan 28 03:24:13 PST 2013


Author: samsonov
Date: Mon Jan 28 05:24:13 2013
New Revision: 173681

URL: http://llvm.org/viewvc/llvm-project?rev=173681&view=rev
Log:
[ASan] fix a bug in allocator-v2 which could lead to SEGV on realloc(malloc(0), 4)

Modified:
    compiler-rt/trunk/lib/asan/asan_allocator2.cc
    compiler-rt/trunk/lib/asan/tests/asan_test.cc

Modified: compiler-rt/trunk/lib/asan/asan_allocator2.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_allocator2.cc?rev=173681&r1=173680&r2=173681&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_allocator2.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_allocator2.cc Mon Jan 28 05:24:13 2013
@@ -612,7 +612,7 @@ void *asan_calloc(uptr nmemb, uptr size,
 }
 
 void *asan_realloc(void *p, uptr size, StackTrace *stack) {
-  if (p == 0)
+  if (p == 0 || reinterpret_cast<uptr>(p) == kReturnOnZeroMalloc)
     return Allocate(size, 8, stack, FROM_MALLOC);
   if (size == 0) {
     Deallocate(p, stack, FROM_MALLOC);

Modified: compiler-rt/trunk/lib/asan/tests/asan_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/tests/asan_test.cc?rev=173681&r1=173680&r2=173681&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/tests/asan_test.cc (original)
+++ compiler-rt/trunk/lib/asan/tests/asan_test.cc Mon Jan 28 05:24:13 2013
@@ -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__





More information about the llvm-commits mailing list