changeset 31204:a81fad5c9fef

idx_vector: Fix wrong length calculation (bug #62968). * liboctave/array/idx-vector.cc: Fix wrong calculation for m_len in (start, limit, step) constructor to account for incomplete intervals. * liboctave/array/idx-vector.h: Add comment to (start, limit, step) constructor.
author Maged Rifaat <magedrifaat@gmail.com>
date Sat, 27 Aug 2022 13:41:44 +0200
parents 4c2b83516eb7
children 7ae0a0772e9d
files liboctave/array/idx-vector.cc liboctave/array/idx-vector.h
diffstat 2 files changed, 6 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/array/idx-vector.cc	Fri Aug 26 22:30:43 2022 +0200
+++ b/liboctave/array/idx-vector.cc	Sat Aug 27 13:41:44 2022 +0200
@@ -103,8 +103,10 @@
                                             octave_idx_type limit,
                                             octave_idx_type step)
     : idx_base_rep (), m_start(start),
-      m_len (step ? std::max ((limit - start) / step,
-                              static_cast<octave_idx_type> (0))
+      // Round length away from zero to catch incomplete intervals
+      m_len (step
+             ? std::max ((limit - start + step - (step > 0 ? 1 : -1)) / step,
+                         static_cast<octave_idx_type> (0))
              : -1),
       m_step (step)
   {
--- a/liboctave/array/idx-vector.h	Fri Aug 26 22:30:43 2022 +0200
+++ b/liboctave/array/idx-vector.h	Sat Aug 27 13:41:44 2022 +0200
@@ -171,7 +171,8 @@
                      octave_idx_type step, direct)
         : idx_base_rep (), m_start (start), m_len (len), m_step (step) { }
 
-      // Zero-based constructor.
+      // Zero-based constructor for index range starting at `start` (inclusive)
+      // and ending at `limit` (exclusive) in steps of `step`.
       idx_range_rep (octave_idx_type start, octave_idx_type limit,
                      octave_idx_type step);