Skip to content

Commit 2eab5d8

Browse files
author
Florian Plaza Oñate
committed
Change offset methods to detect negative indexes
1 parent 434b940 commit 2eab5d8

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

inst/include/Rcpp/vector/Vector.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,26 +278,26 @@ class Vector :
278278
/**
279279
* offset based on the dimensions of this vector
280280
*/
281-
size_t offset(const size_t& i, const size_t& j) const {
281+
R_xlen_t offset(const int& i, const int& j) const {
282282
if( !::Rf_isMatrix(Storage::get__()) ) throw not_a_matrix() ;
283283

284284
/* we need to extract the dimensions */
285-
int *dim = dims() ;
286-
size_t nrow = static_cast<size_t>(dim[0]) ;
287-
size_t ncol = static_cast<size_t>(dim[1]) ;
288-
if( i >= nrow || j >= ncol ) throw index_out_of_bounds() ;
289-
return i + nrow*j ;
285+
const int* dim = dims() ;
286+
const int nrow = dim[0] ;
287+
const int ncol = dim[1] ;
288+
if(i < 0|| i >= nrow || j < 0 || j >= ncol ) throw index_out_of_bounds() ;
289+
return i + static_cast<R_xlen_t>(nrow)*j ;
290290
}
291291

292292
/**
293293
* one dimensional offset doing bounds checking to ensure
294294
* it is valid
295295
*/
296-
size_t offset(const size_t& i) const {
297-
if( static_cast<R_xlen_t>(i) >= ::Rf_xlength(Storage::get__()) ) throw index_out_of_bounds() ;
296+
R_xlen_t offset(const R_xlen_t& i) const {
297+
if(i < 0 || i >= ::Rf_xlength(Storage::get__()) ) throw index_out_of_bounds() ;
298298
return i ;
299299
}
300-
300+
301301
R_xlen_t offset(const std::string& name) const {
302302
SEXP names = RCPP_GET_NAMES( Storage::get__() ) ;
303303
if( Rf_isNull(names) ) throw index_out_of_bounds();

0 commit comments

Comments
 (0)