[sanitizer] improve the calloc overflow check (spotted by samsonov@)
authorKostya Serebryany <kcc@google.com>
Fri, 25 Jan 2013 12:22:21 +0000 (12:22 +0000)
committerKostya Serebryany <kcc@google.com>
Fri, 25 Jan 2013 12:22:21 +0000 (12:22 +0000)
llvm-svn: 173443

compiler-rt/lib/asan/tests/asan_noinst_test.cc
compiler-rt/lib/sanitizer_common/sanitizer_allocator.cc

index 95ad15e..278bde5 100644 (file)
@@ -840,3 +840,12 @@ TEST(AddressSanitizerInterface, CallocOverflow) {
   void *p = calloc(kArraySize, kArraySize2);  // Should return 0.
   EXPECT_EQ(0L, Ident(p));
 }
+
+TEST(AddressSanitizerInterface, CallocOverflow2) {
+#if SANITIZER_WORDSIZE == 32
+  size_t kArraySize = 112;
+  volatile size_t kArraySize2 = 43878406;
+  void *p = calloc(kArraySize, kArraySize2);  // Should return 0.
+  EXPECT_EQ(0L, Ident(p));
+#endif
+}
index 26baf73..88a3a1b 100644 (file)
@@ -76,8 +76,9 @@ void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback) {
 }
 
 bool CallocShouldReturnNullDueToOverflow(uptr size, uptr n) {
-  uptr mul = size * n;
-  return mul < size || mul < n;
+  if (!size) return false;
+  uptr max = (uptr)-1L;
+  return (max / size) < n;
 }
 
 }  // namespace __sanitizer