changeset 23399:5e0350f5e2dd

always return valid address for local buffers * oct-locbuf.h, oct-locbuf.cc (local_buffer::local_buffer): Always allocate, even for zero-size buffers. (chunk_buffer::chunk_buffer): Likewise.
author John W. Eaton <jwe@octave.org>
date Fri, 14 Apr 2017 14:00:41 -0400
parents d4ef04757ead
children b5ee9b985a82
files liboctave/util/oct-locbuf.cc liboctave/util/oct-locbuf.h
diffstat 2 files changed, 10 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/util/oct-locbuf.cc	Thu Apr 13 14:25:16 2017 -0400
+++ b/liboctave/util/oct-locbuf.cc	Fri Apr 14 14:00:41 2017 -0400
@@ -57,10 +57,11 @@
                                       ? sizeof (double)
                                       : sizeof (long)) - 1;
 
-    active++;
+    // Always allocate, even for zero-size buffers so that local buffers
+    // always have valid addresses, same as for directly using operator
+    // new.
 
-    if (! size)
-      return;
+    active++;
 
     // Align size.  Note that size_t is unsigned, so size-1 must correctly
     // wrap around.
--- a/liboctave/util/oct-locbuf.h	Thu Apr 13 14:25:16 2017 -0400
+++ b/liboctave/util/oct-locbuf.h	Fri Apr 14 14:00:41 2017 -0400
@@ -38,12 +38,12 @@
   class local_buffer
   {
   public:
-    local_buffer (size_t size)
-      : data (0)
-    {
-      if (size)
-        data = new T [size];
-    }
+
+    // Always allocate, even for zero-size buffers so that local
+    // buffers always have valid addresses, same as for directly using
+    // operator new.
+
+    local_buffer (size_t size) : data (new T [size]) { }
 
     // No copying!