changeset 32461:09ee4030e927

Do type check before static_cast to wrong type The virtual function is_defined() was called on a pointer to octave_cached_value , when the object actually was a octave_base_value. The type check call need to be done on a pointer to the base class to be standard compliant. * pt-bytecode-vm.cc: Comply
author Petter T. <petter.vilhelm@gmail.com>
date Sat, 04 Nov 2023 01:35:04 +0100
parents ba52044813f1
children a3d5446b3e93
files libinterp/parse-tree/pt-bytecode-vm.cc
diffstat 1 files changed, 11 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/parse-tree/pt-bytecode-vm.cc	Sat Nov 04 01:35:04 2023 +0100
+++ b/libinterp/parse-tree/pt-bytecode-vm.cc	Sat Nov 04 01:35:04 2023 +0100
@@ -1772,15 +1772,22 @@
     unsigned char b0 = *ip++;
     unsigned char b1 = *ip++;
 
-    octave_cached_value *ovb = static_cast<octave_cached_value*> (bsp[slot].ovb);
-    if (ovb->is_defined () && ovb->cache_is_valid ())
-      {
-        PUSH_OV (ovb->get_cached_value ());
+    // If the slot value is defined it is a octave_cached_value, since only
+    // this opcode and SET_FOLDED_CST writes to the slot.
+
+    octave_base_value *ovb = bsp[slot].ovb;
+    octave_cached_value *ovbc = static_cast<octave_cached_value*> (ovb);
+    if (ovb->is_defined () && ovbc->cache_is_valid ())
+      {
+        // Use the cached value. Push it to the stack.
+        PUSH_OV (ovbc->get_cached_value ());
+        // Jump over the initialization code (the folded code) of the cached value
         int target = USHORT_FROM_UCHARS (b0, b1);
         ip = code + target;
       }
     else
       {
+        // Put a octave_cached_value in the slot for SET_FOLDED_CST
         bsp[slot].ov = octave_value {new octave_cached_value};
       }
   }