# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1342735688 14400 # Node ID 4315a39da4c90705e030a6ed6cfddf641b2553c9 # Parent 9e5df4d6cefa66853b08b9555f0994e922b9e9c3 Do computations with octave_uint64 to avoid overflow * sparse-dmsolve (MSparse::dmsolve_extract): Perform multiplication and comparison in octave_uint64 to avoid overflow. diff -r 9e5df4d6cefa -r 4315a39da4c9 liboctave/sparse-dmsolve.cc --- a/liboctave/sparse-dmsolve.cc Mon Jul 16 16:55:27 2012 +0100 +++ b/liboctave/sparse-dmsolve.cc Thu Jul 19 18:08:08 2012 -0400 @@ -33,6 +33,7 @@ #include "MatrixType.h" #include "oct-sort.h" #include "oct-locbuf.h" +#include "oct-inttypes.h" template static MSparse @@ -42,9 +43,17 @@ octave_idx_type cend, octave_idx_type maxnz = -1, bool lazy = false) { - octave_idx_type nz = (rend - rst) * (cend - cst); + octave_idx_type nr = rend - rst, nc = cend - cst; maxnz = (maxnz < 0 ? A.nnz () : maxnz); - MSparse B (rend - rst, cend - cst, (nz < maxnz ? nz : maxnz)); + octave_idx_type nz; + + // Cast to uint64 to handle overflow in this multiplication + if (octave_uint64 (nr)*octave_uint64 (nc) < octave_uint64 (maxnz)) + nz = nr*nz; + else + nz = maxnz; + + MSparse B (nr, nc, (nz < maxnz ? nz : maxnz)); // Some sparse functions can support lazy indexing (where elements // in the row are in no particular order), even though octave in // general can't. For those functions that can using it is a big