changeset 32472:b5e433b52bc3

Avoid integer overflow when saving sparse bool matrix in binary format. * libinterp/octave-value/ov-bool-sparse.cc (octave_sparse_bool_matrix::save_binary): Make sure that the number of non-zero elements, the number of rows, and the number of columns are non-negative, and they can be stored with 4 bytes. (Prompted by a compiler warning with GCC 13.)
author Markus Mützel <markus.muetzel@gmx.de>
date Mon, 13 Nov 2023 16:58:52 +0100
parents 88c36f68771c
children 7bd4d95bcb7a
files libinterp/octave-value/ov-bool-sparse.cc
diffstat 1 files changed, 14 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/octave-value/ov-bool-sparse.cc	Sun Nov 12 14:46:15 2023 +0100
+++ b/libinterp/octave-value/ov-bool-sparse.cc	Mon Nov 13 16:58:52 2023 +0100
@@ -187,9 +187,20 @@
   // Ensure that additional memory is deallocated
   matrix.maybe_compress ();
 
-  int nr = dv(0);
-  int nc = dv(1);
-  int nz = nnz ();
+  octave_idx_type nr = dv(0);
+  octave_idx_type nc = dv(1);
+  octave_idx_type nz = nnz ();
+
+  // For compatiblity, indices are always saved with 4 bytes
+#if OCTAVE_SIZEOF_IDX_TYPE == OCTAVE_SIZEOF_INT
+  if (nr < 0 || nc < 0 || nz < 0)
+    return false;
+#else
+  octave_idx_type max_val = std::numeric_limits<uint32_t>::max ();
+  if (nr < 0 || nr > max_val || nc < 0 || nc > max_val
+      || nz < 0 || nz > max_val)
+    return false;
+#endif
 
   int32_t itmp;
   // Use negative value for ndims to be consistent with other formats