changeset 25707:39fd627b2c5d

Emit an immediate error if size of an octave_stream object is invalid (bug #54405). * oct-stream.cc (get_size (double d, ...)): Change error message about NaN as invalid size specifier to match other errors of same type. * oct-stream.cc (get_size (const Array<double>& size, ...)): Change error message about infinite value as size specifier to match other errors of same type. Change error about invalid size to include extra message that size must be 2-D. Check for overflow of octave_idx_type and emit immediate exception-based error if encountered. * oct-stream.cc (stream::read): Remove FIXME about checking for overflow.
author Rik <rik@octave.org>
date Tue, 31 Jul 2018 10:17:17 -0700
parents 35002c2b886b
children ca38400776bc
files libinterp/corefcn/oct-stream.cc
diffstat 1 files changed, 12 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/oct-stream.cc	Mon Jul 30 18:11:04 2018 +0200
+++ b/libinterp/corefcn/oct-stream.cc	Tue Jul 31 10:17:17 2018 -0700
@@ -122,7 +122,7 @@
     octave_idx_type retval = -1;
 
     if (lo_ieee_isnan (d))
-      ::error ("%s: NaN is invalid as size specification", who.c_str ());
+      ::error ("%s: NaN invalid as size specification", who.c_str ());
 
     if (math::isinf (d))
       retval = -1;
@@ -170,17 +170,25 @@
         dnr = size(0);
 
         if (math::isinf (dnr))
-          ::error ("%s: invalid size specification", who.c_str ());
+          ::error ("%s: infinite value invalid as size specification",
+                   who.c_str ());
 
         dnc = size(1);
       }
     else
-      ::error ("%s: invalid size specification", who.c_str ());
+      ::error ("%s: invalid size specification (must be 2-D)", who.c_str ());
 
     nr = get_size (dnr, who);
 
     if (dnc >= 0.0)
-      nc = get_size (dnc, who);
+      {
+        nc = get_size (dnc, who);
+
+        // Check for overflow.
+        if (nr != 0 &&
+            abs (nc) > abs (std::numeric_limits<octave_idx_type>::max () / nr))
+         ::error ("%s: size too large for Octave's index type", who.c_str ());
+      }
   }
 
   static std::string
@@ -6568,9 +6576,6 @@
           nr = nc = 0;
       }
 
-    // FIXME: Ensure that this does not overflow.
-    //        Maybe try comparing nr * nc computed in double with
-    //        std::numeric_limits<octave_idx_type>::max ();
     octave_idx_type elts_to_read = nr * nc;
 
     bool read_to_eof = elts_to_read < 0;