comparison 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
comparison
equal deleted inserted replaced
19008:80ca3b05d77c 19009:8d47ce2053f2
24 #ifdef HAVE_CONFIG_H 24 #ifdef HAVE_CONFIG_H
25 #include <config.h> 25 #include <config.h>
26 #endif 26 #endif
27 27
28 #include "Array-util.h" 28 #include "Array-util.h"
29 #include "Array.h"
29 #include "dim-vector.h" 30 #include "dim-vector.h"
30 #include "lo-error.h" 31 #include "lo-error.h"
31 #include "oct-locbuf.h" 32 #include "oct-locbuf.h"
32 33
33 bool 34 bool
172 } 173 }
173 } 174 }
174 return retval; 175 return retval;
175 } 176 }
176 177
177 octave_idx_type 178 void
178 compute_index (octave_idx_type n, const dim_vector& dims) 179 check_index_bounds (int nd, int dim, octave_idx_type i, octave_idx_type size)
179 { 180 {
180 if (n < 0) 181 if (i < 0)
181 gripe_invalid_index (); 182 gripe_invalid_index ();
182 if (n >= dims.numel ()) 183 if (i >= size)
183 gripe_index_out_of_range (1, 1, n+1, dims.numel ()); 184 gripe_index_out_of_range (nd, dim + 1, i + 1, size);
184 185 }
185 return n; 186
186 } 187 void
187 188 check_index (octave_idx_type n, const dim_vector& dims)
188 octave_idx_type 189 {
189 compute_index (octave_idx_type i, octave_idx_type j, const dim_vector& dims) 190 check_index_bounds (1, 0, n, dims.numel ());
190 { 191 }
191 if (i < 0 || j < 0) 192
192 gripe_invalid_index (); 193 void
193 if (i >= dims(0)) 194 check_index (octave_idx_type i, octave_idx_type j, const dim_vector& dims)
194 gripe_index_out_of_range (2, 1, i+1, dims(0)); 195 {
195 if (j >= dims.numel (1)) 196 check_index_bounds (2, 0, i, dims(0));
196 gripe_index_out_of_range (2, 2, j+1, dims.numel (1)); 197 check_index_bounds (2, 1, j, dims.numel (1));
197 198 }
198 return j*dims(0) + i; 199
199 } 200 void
200 201 check_index (octave_idx_type i, octave_idx_type j, octave_idx_type k,
201 octave_idx_type 202 const dim_vector& dims)
202 compute_index (octave_idx_type i, octave_idx_type j, octave_idx_type k, 203 {
203 const dim_vector& dims) 204 check_index_bounds (3, 0, i, dims(0));
204 { 205 check_index_bounds (3, 1, j, dims(1));
205 if (i < 0 || j < 0 || k < 0) 206 check_index_bounds (3, 2, k, dims.numel (2));
206 gripe_invalid_index (); 207 }
207 if (i >= dims(0)) 208
208 gripe_index_out_of_range (3, 1, i+1, dims(0)); 209 void
209 if (j >= dims(1)) 210 check_index (const Array<octave_idx_type>& ra_idx, const dim_vector& dims)
210 gripe_index_out_of_range (3, 2, j+1, dims(1));
211 if (k >= dims.numel (2))
212 gripe_index_out_of_range (3, 3, k+1, dims.numel (2));
213
214 return (k*dims(1) + j)*dims(0) + i;
215 }
216
217 octave_idx_type
218 compute_index (const Array<octave_idx_type>& ra_idx, const dim_vector& dims)
219 { 211 {
220 int nd = ra_idx.length (); 212 int nd = ra_idx.length ();
221 const dim_vector dv = dims.redim (nd); 213 const dim_vector dv = dims.redim (nd);
222 for (int d = 0; d < nd; d++) 214 for (int d = 0; d < nd; d++)
223 { 215 {
224 if (ra_idx(d) < 0) 216 check_index_bounds (nd, d, ra_idx(d), dv(d));
225 gripe_invalid_index (); 217 }
226 if (ra_idx(d) >= dv(d)) 218 }
227 gripe_index_out_of_range (nd, d+1, ra_idx(d)+1, dv(d)); 219
228 } 220 octave_idx_type
229 221 compute_index (const Array<octave_idx_type>& ra_idx, const dim_vector& dims,
230 return dv.compute_index (ra_idx.data ()); 222 bool check)
223 {
224 if (BOUNDS_CHECKING_DEFINED || check)
225 check_index (ra_idx, dims);
226 return dims.compute_index (ra_idx.data ());
231 } 227 }
232 228
233 Array<octave_idx_type> 229 Array<octave_idx_type>
234 conv_to_int_array (const Array<idx_vector>& a) 230 conv_to_int_array (const Array<idx_vector>& a)
235 { 231 {