Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casts to avoid integer overflow in matrix row/columns. #1281

Merged
merged 7 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>

* 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 <[email protected]>

* 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