Skip to content

Commit

Permalink
Casts to avoid integer overflow in matrix row/columns. (#1281)
Browse files Browse the repository at this point in the history
* Casts to avoid integer overflow in matrix row/columns.

* Updated ChangeLog.

* Missed a few spots.

* Missed another spot.

* Even more spots missed.

* Remove an extraneous cast that breaks the Vector::end iterator.

* Updated Changelog again.
  • Loading branch information
LTLA authored Oct 27, 2023
1 parent ae1eccc commit 8575b2e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
2023-10-22 Aaron Lun <infinite.monkeys.with.keyboards@gmail.com>

* inst/include/Rcpp/vector/MatrixColumn.h: Cast integer index
to R_xlen_t to avoid integer overflow with large matrices.
* inst/include/Rcpp/vector/MatrixRow.h: Ditto.
* inst/include/Rcpp/vector/Vector.h: Remove stray cast to int
that causes overflow when returning the end iterator.

2023-09-30 Dirk Eddelbuettel <edd@debian.org>

* vignettes/rmd/Rcpp-introduction.Rmd (Rcpp): Correct caption of
Expand Down
2 changes: 1 addition & 1 deletion inst/include/Rcpp/vector/MatrixColumn.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class ConstMatrixColumn : public VectorBase<RTYPE,true,ConstMatrixColumn<RTYPE>

ConstMatrixColumn( const MATRIX& parent, int i ) :
n(parent.nrow()),
const_start(parent.begin() + i *n)
const_start(parent.begin() + static_cast<R_xlen_t>(i) * n)
{
if( i < 0 || i >= parent.ncol() ) {
const char* fmt = "Column index is out of bounds: "
Expand Down
16 changes: 8 additions & 8 deletions inst/include/Rcpp/vector/MatrixRow.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class MatrixRow : public VectorBase< RTYPE, true, MatrixRow<RTYPE> > {
}

inline reference operator[]( int i ) const {
return parent[ row + i * parent_nrow ] ;
return parent[ row + static_cast<R_xlen_t>(i) * parent_nrow ] ;
}

inline iterator begin(){
Expand Down Expand Up @@ -206,9 +206,9 @@ class MatrixRow : public VectorBase< RTYPE, true, MatrixRow<RTYPE> > {
int parent_nrow ;
int row ;

inline int get_parent_index(int i) const {
RCPP_DEBUG_4( "MatrixRow<%d>::get_parent_index(int = %d), parent_nrow = %d >> %d\n", RTYPE, i, parent_nrow, i*parent_nrow )
return i * parent_nrow ;
inline R_xlen_t get_parent_index(int i) const {
RCPP_DEBUG_4( "MatrixRow<%d>::get_parent_index(int = %d), parent_nrow = %d >> %d\n", RTYPE, i, parent_nrow, static_cast<R_xlen_t>(i) * parent_nrow )
return static_cast<R_xlen_t>(i) * parent_nrow ;
}
} ;

Expand Down Expand Up @@ -310,7 +310,7 @@ class ConstMatrixRow : public VectorBase< RTYPE, true, ConstMatrixRow<RTYPE> > {
{} ;

inline const_reference operator[]( int i ) const {
return parent[ row + i * parent_nrow ] ;
return parent[ row + static_cast<R_xlen_t>(i) * parent_nrow ] ;
}

inline const_iterator begin() const {
Expand All @@ -331,9 +331,9 @@ class ConstMatrixRow : public VectorBase< RTYPE, true, ConstMatrixRow<RTYPE> > {
int parent_nrow ;
int row ;

inline int get_parent_index(int i) const {
RCPP_DEBUG_4( "ConstMatrixRow<%d>::get_parent_index(int = %d), parent_nrow = %d >> %d\n", RTYPE, i, parent_nrow, i*parent_nrow )
return i * parent_nrow ;
inline R_xlen_t get_parent_index(int i) const {
RCPP_DEBUG_4( "ConstMatrixRow<%d>::get_parent_index(int = %d), parent_nrow = %d >> %d\n", RTYPE, i, parent_nrow, static_cast<R_xlen_t>(i) * parent_nrow )
return static_cast<R_xlen_t>(i) * parent_nrow ;
}
} ;
}
Expand Down
2 changes: 1 addition & 1 deletion inst/include/Rcpp/vector/Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class Vector :
}

inline iterator begin() { return cache.get() ; }
inline iterator end() { return cache.get() + static_cast<int>(size()) ; }
inline iterator end() { return cache.get() + size() ; }
inline const_iterator begin() const{ return cache.get_const() ; }
inline const_iterator end() const{ return cache.get_const() + size() ; }
inline const_iterator cbegin() const{ return cache.get_const() ; }
Expand Down

0 comments on commit 8575b2e

Please sign in to comment.