8168542: os::realloc should return a valid pointer for input size=0
authorstuefe
Mon, 24 Oct 2016 14:51:16 +0200
changeset 46295 6092ca55b77b
parent 46294 345a46524a19
child 46296 15ba6edb7910
child 46298 b1aa28a58628
8168542: os::realloc should return a valid pointer for input size=0 Reviewed-by: dholmes, cjplummer, dsamersoff, rehn
hotspot/src/share/vm/runtime/os.cpp
--- a/hotspot/src/share/vm/runtime/os.cpp	Tue Feb 28 10:51:47 2017 -0800
+++ b/hotspot/src/share/vm/runtime/os.cpp	Mon Oct 24 14:51:16 2016 +0200
@@ -649,6 +649,12 @@
     return NULL;
   }
 
+  if (size == 0) {
+    // return a valid pointer if size is zero
+    // if NULL is returned the calling functions assume out of memory.
+    size = 1;
+  }
+
 #ifndef ASSERT
   NOT_PRODUCT(inc_stat_counter(&num_mallocs, 1));
   NOT_PRODUCT(inc_stat_counter(&alloc_bytes, size));
@@ -669,9 +675,6 @@
   // NMT support
   void* membase = MemTracker::malloc_base(memblock);
   verify_memory(membase);
-  if (size == 0) {
-    return NULL;
-  }
   // always move the block
   void* ptr = os::malloc(size, memflags, stack);
   if (PrintMalloc && tty != NULL) {