Mercurial > octave-dspies
diff liboctave/array/Array-util.cc @ 19009:8d47ce2053f2 draft
Added safety checks to Array::xelem
There's no reason to have a method which never checks invariants, ever. Added
debugging checks to Array::xelem to help catch and debug out-of-bounds errors
and reference overlap
* configure.ac: Added configuration option for uniqueness-checking with xelem
* jit-typeinfo.cc (octave_jit_paren_scalar): Call const Array::xelem rather
than Array::xelem
* Array-util.h, Array-util.cc (check_out_of_range): Extract common pattern to
method
(check_index): Methods to check index is in-bounds
(compute_index): Added bool parameter check. does not check bounds when check
is false and BOUNDS_CHECKING is off
* Array.h, Array.cc (xelem): Use methods from Array-util.h to compute indices
(is_unique): Check if this is the only reference to data
* CmplxQR.cc, dbleQR.cc, fCmplxQR.cc, floatQR.cc
(form): Move second assignment to after the call to xelem
* lo-array-gripes.h, lo-array-gripes.cc (gripe_modifying_nonunique): Added
error message for when non-const xelem is called on non-unique array
author | David Spies <dnspies@gmail.com> |
---|---|
date | Mon, 14 Jul 2014 13:07:59 -0600 |
parents | 8e056300994b |
children |
line wrap: on
line diff
--- a/liboctave/array/Array-util.cc Sat Jun 21 13:13:05 2014 -0600 +++ b/liboctave/array/Array-util.cc Mon Jul 14 13:07:59 2014 -0600 @@ -26,6 +26,7 @@ #endif #include "Array-util.h" +#include "Array.h" #include "dim-vector.h" #include "lo-error.h" #include "oct-locbuf.h" @@ -174,60 +175,55 @@ return retval; } -octave_idx_type -compute_index (octave_idx_type n, const dim_vector& dims) +void +check_index_bounds (int nd, int dim, octave_idx_type i, octave_idx_type size) { - if (n < 0) + if (i < 0) gripe_invalid_index (); - if (n >= dims.numel ()) - gripe_index_out_of_range (1, 1, n+1, dims.numel ()); + if (i >= size) + gripe_index_out_of_range (nd, dim + 1, i + 1, size); +} - return n; +void +check_index (octave_idx_type n, const dim_vector& dims) +{ + check_index_bounds (1, 0, n, dims.numel ()); } -octave_idx_type -compute_index (octave_idx_type i, octave_idx_type j, const dim_vector& dims) +void +check_index (octave_idx_type i, octave_idx_type j, const dim_vector& dims) { - if (i < 0 || j < 0) - gripe_invalid_index (); - if (i >= dims(0)) - gripe_index_out_of_range (2, 1, i+1, dims(0)); - if (j >= dims.numel (1)) - gripe_index_out_of_range (2, 2, j+1, dims.numel (1)); - - return j*dims(0) + i; + check_index_bounds (2, 0, i, dims(0)); + check_index_bounds (2, 1, j, dims.numel (1)); } -octave_idx_type -compute_index (octave_idx_type i, octave_idx_type j, octave_idx_type k, - const dim_vector& dims) +void +check_index (octave_idx_type i, octave_idx_type j, octave_idx_type k, + const dim_vector& dims) { - if (i < 0 || j < 0 || k < 0) - gripe_invalid_index (); - if (i >= dims(0)) - gripe_index_out_of_range (3, 1, i+1, dims(0)); - if (j >= dims(1)) - gripe_index_out_of_range (3, 2, j+1, dims(1)); - if (k >= dims.numel (2)) - gripe_index_out_of_range (3, 3, k+1, dims.numel (2)); - - return (k*dims(1) + j)*dims(0) + i; + check_index_bounds (3, 0, i, dims(0)); + check_index_bounds (3, 1, j, dims(1)); + check_index_bounds (3, 2, k, dims.numel (2)); } -octave_idx_type -compute_index (const Array<octave_idx_type>& ra_idx, const dim_vector& dims) +void +check_index (const Array<octave_idx_type>& ra_idx, const dim_vector& dims) { int nd = ra_idx.length (); const dim_vector dv = dims.redim (nd); for (int d = 0; d < nd; d++) { - if (ra_idx(d) < 0) - gripe_invalid_index (); - if (ra_idx(d) >= dv(d)) - gripe_index_out_of_range (nd, d+1, ra_idx(d)+1, dv(d)); + check_index_bounds (nd, d, ra_idx(d), dv(d)); } +} - return dv.compute_index (ra_idx.data ()); +octave_idx_type +compute_index (const Array<octave_idx_type>& ra_idx, const dim_vector& dims, + bool check) +{ + if (BOUNDS_CHECKING_DEFINED || check) + check_index (ra_idx, dims); + return dims.compute_index (ra_idx.data ()); } Array<octave_idx_type>