changeset 32401:1156548e6211

Fix making id undefined in zero iteration for-loops in VM with wide slots. (bug #64749) E.g.: for i = 1:0 end Would clear the wrong id instead of 'i' if 'i' had a slot number over 255 due to reading a short as a char. * libinterp/parse-tree/pt-bytecode-vm.cc: Proper byte shifting of shorts
author Petter T. <petter.vilhelm@gmail.com>
date Sun, 08 Oct 2023 22:25:06 +0200
parents 3f48081aa034
children 7da49c0da993
files libinterp/parse-tree/pt-bytecode-vm.cc test/compile/bytecode_misc.m
diffstat 2 files changed, 15 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/parse-tree/pt-bytecode-vm.cc	Wed Oct 11 18:46:51 2023 +0200
+++ b/libinterp/parse-tree/pt-bytecode-vm.cc	Sun Oct 08 22:25:06 2023 +0200
@@ -2731,10 +2731,17 @@
       {
         // Slot from the for_cond that always follow a for_setup
         int slot;
+        // The next opcode is in arg0, and is either WIDE or FOR_COND
         if (arg0 == static_cast<int> (INSTR::WIDE))
-          slot = ip[1];
+          {
+            // Byte layout: ip[-2]:FOR_SETUP, ip[-1]:WIDE, ip[0]:FOR_COND, ip[1]:slot_lsb, ip[2]:slot_msb
+            slot = ip[1] + (ip[2] << 8);
+          }
         else
-          slot = ip[0];
+          {
+            // Byte layout: ip[-2]:FOR_SETUP, ip[-1]:FOR_COND, ip[0]:slot
+            slot = ip[0];
+          }
         try
         {
           octave_value &lhs_ov = bsp[slot].ov;
--- a/test/compile/bytecode_misc.m	Wed Oct 11 18:46:51 2023 +0200
+++ b/test/compile/bytecode_misc.m	Sun Oct 08 22:25:06 2023 +0200
@@ -127,6 +127,12 @@
     assert (b == 2)
   end
 
+  % Bug #64749: With no iterations, FOR_SETUP writes to the wrong id with wide slots
+  i = 123;
+  for i = 1:0
+  end
+  assert (isempty (i));
+  assert (size(i) == [1 0])
 
   % Check that a001 to a512 have to correct values
   for i = 1:512