diff options
author | Alexey Samsonov <samsonov@google.com> | 2013-01-28 11:24:13 +0000 |
---|---|---|
committer | Alexey Samsonov <samsonov@google.com> | 2013-01-28 11:24:13 +0000 |
commit | 7eda134fa7733e84424359c6ac23cd4ce1045435 (patch) | |
tree | 90ad54f5d3cf7a7ad0c1083c71927c881dd4cef2 /compiler-rt | |
parent | 20b09efabb21f97d7ade4f794e65fcfa46e26a3d (diff) | |
download | bcm5719-llvm-7eda134fa7733e84424359c6ac23cd4ce1045435.tar.gz bcm5719-llvm-7eda134fa7733e84424359c6ac23cd4ce1045435.zip |
[ASan] fix a bug in allocator-v2 which could lead to SEGV on realloc(malloc(0), 4)
llvm-svn: 173681
Diffstat (limited to 'compiler-rt')
-rw-r--r-- | compiler-rt/lib/asan/asan_allocator2.cc | 2 | ||||
-rw-r--r-- | compiler-rt/lib/asan/tests/asan_test.cc | 9 |
2 files changed, 10 insertions, 1 deletions
diff --git a/compiler-rt/lib/asan/asan_allocator2.cc b/compiler-rt/lib/asan/asan_allocator2.cc index 63cb41b4c97..eae4bf5ee0d 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<uptr>(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 487ed97a574..f7b6097e4bf 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__ |