changeset 23514:fd7a16594614

Don't iterate over empty for loop expressions (bug #50893). * pt-eval.cc (tree_evaluator::visit_simple_for_command): Don't execute for loop for empty expressions. However, do assign an empty value to loop variable. * test/for.tst: Add BIST tests for bug #50893.
author Rik <rik@octave.org>
date Sun, 21 May 2017 18:03:58 -0700
parents 4396af814c6a
children 4d7928872999
files libinterp/parse-tree/pt-eval.cc test/for.tst
diffstat 2 files changed, 44 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/parse-tree/pt-eval.cc	Thu May 18 08:12:40 2017 -0700
+++ b/libinterp/parse-tree/pt-eval.cc	Sun May 21 18:03:58 2017 -0700
@@ -792,15 +792,16 @@
         octave_idx_type nrows = dv(0);
         octave_idx_type steps = dv(1);
 
-        if (steps > 0)
+        octave_value arg = rhs;
+        if (rhs.ndims () > 2)
+          arg = arg.reshape (dv);
+
+        if (nrows > 0 && steps > 0)
           {
-            octave_value arg = rhs;
-            if (rhs.ndims () > 2)
-              arg = arg.reshape (dv);
+            octave_value_list idx;
+            octave_idx_type iidx;
 
             // for row vectors, use single index to speed things up.
-            octave_value_list idx;
-            octave_idx_type iidx;
             if (nrows == 1)
               {
                 idx.resize (1);
@@ -827,6 +828,11 @@
                 if (quit_loop_now ())
                   break;
               }
+          } 
+        else
+          {
+            // Handle empty cases, while still assigning to loop var.
+            ult.assign (octave_value::op_asn_eq, arg);
           }
       }
     else
--- a/test/for.tst	Thu May 18 08:12:40 2017 -0700
+++ b/test/for.tst	Sun May 21 18:03:58 2017 -0700
@@ -122,3 +122,35 @@
 %! endparfor
 %! __printf_assert__ ("\n");
 %! assert (__prog_output_assert__ ("1234"));
+
+%!test <50893>
+%! cnt = 0; 
+%! for k = zeros (0,3);
+%!   cnt++;
+%! endfor
+%! assert (cnt, 0);
+%! assert (k, zeros (0,1));
+
+%!test <50893>
+%! cnt = 0; 
+%! for k = zeros (3,0);
+%!   cnt++;
+%! endfor
+%! assert (cnt, 0);
+%! assert (k, zeros (1,0));
+
+%!test <50893>
+%! cnt = 0; 
+%! for k = zeros (3,0, "uint32");
+%!   cnt++;
+%! endfor
+%! assert (cnt, 0);
+%! assert (k, zeros (1,0, "uint32"));
+
+%!test <50893>
+%! cnt = 0; 
+%! for k = cell (0,3);
+%!   cnt++;
+%! endfor
+%! assert (cnt, 0);
+%! assert (k, cell (0,1));