Skip to content

Commit

Permalink
Avoid integer overflow when saving sparse bool matrix in binary format.
Browse files Browse the repository at this point in the history
* 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.)
  • Loading branch information
mmuetzel committed Nov 13, 2023
1 parent 0a5c7ad commit c1dda7b
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions libinterp/octave-value/ov-bool-sparse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,20 @@ octave_sparse_bool_matrix::save_binary (std::ostream& os, bool)
// 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
Expand Down

0 comments on commit c1dda7b

Please sign in to comment.