[PATCH] D22800: [tsan] Fix behavior of realloc(nullptr, 0) on Darwin

Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 2 07:29:53 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL277458: [tsan] Fix behavior of realloc(nullptr, 0) on Darwin (authored by kuba.brecka).

Changed prior to commit:
  https://reviews.llvm.org/D22800?vs=65489&id=66473#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D22800

Files:
  compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc
  compiler-rt/trunk/test/tsan/Darwin/realloc-zero.cc

Index: compiler-rt/trunk/test/tsan/Darwin/realloc-zero.cc
===================================================================
--- compiler-rt/trunk/test/tsan/Darwin/realloc-zero.cc
+++ compiler-rt/trunk/test/tsan/Darwin/realloc-zero.cc
@@ -0,0 +1,20 @@
+// Test that realloc(nullptr, 0) return a non-NULL pointer.
+
+// RUN: %clang_tsan %s -o %t
+// RUN: %run %t 2>&1 | FileCheck %s
+
+#include <malloc/malloc.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+
+int main() {
+  void *p = realloc(nullptr, 0);
+  if (!p) {
+    abort();
+  }
+  fprintf(stderr, "Okay.\n");
+  return 0;
+}
+
+// CHECK: Okay.
Index: compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc
===================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc
@@ -195,20 +195,16 @@
 }
 
 void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz) {
-  void *p2 = 0;
   // FIXME: Handle "shrinking" more efficiently,
   // it seems that some software actually does this.
-  if (sz) {
-    p2 = user_alloc(thr, pc, sz);
-    if (p2 == 0)
-      return 0;
-    if (p) {
-      uptr oldsz = user_alloc_usable_size(p);
-      internal_memcpy(p2, p, min(oldsz, sz));
-    }
-  }
-  if (p)
+  void *p2 = user_alloc(thr, pc, sz);
+  if (p2 == 0)
+    return 0;
+  if (p) {
+    uptr oldsz = user_alloc_usable_size(p);
+    internal_memcpy(p2, p, min(oldsz, sz));
     user_free(thr, pc, p);
+  }
   return p2;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22800.66473.patch
Type: text/x-patch
Size: 1492 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160802/42970d97/attachment.bin>


More information about the llvm-commits mailing list