Skip to content

Commit 8575b2e

Browse files
authored
Casts to avoid integer overflow in matrix row/columns. (#1281)
* 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.
1 parent ae1eccc commit 8575b2e

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2023-10-22 Aaron Lun <[email protected]>
2+
3+
* inst/include/Rcpp/vector/MatrixColumn.h: Cast integer index
4+
to R_xlen_t to avoid integer overflow with large matrices.
5+
* inst/include/Rcpp/vector/MatrixRow.h: Ditto.
6+
* inst/include/Rcpp/vector/Vector.h: Remove stray cast to int
7+
that causes overflow when returning the end iterator.
8+
19
2023-09-30 Dirk Eddelbuettel <[email protected]>
210

311
* vignettes/rmd/Rcpp-introduction.Rmd (Rcpp): Correct caption of

inst/include/Rcpp/vector/MatrixColumn.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class ConstMatrixColumn : public VectorBase<RTYPE,true,ConstMatrixColumn<RTYPE>
129129

130130
ConstMatrixColumn( const MATRIX& parent, int i ) :
131131
n(parent.nrow()),
132-
const_start(parent.begin() + i *n)
132+
const_start(parent.begin() + static_cast<R_xlen_t>(i) * n)
133133
{
134134
if( i < 0 || i >= parent.ncol() ) {
135135
const char* fmt = "Column index is out of bounds: "

inst/include/Rcpp/vector/MatrixRow.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class MatrixRow : public VectorBase< RTYPE, true, MatrixRow<RTYPE> > {
169169
}
170170

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

175175
inline iterator begin(){
@@ -206,9 +206,9 @@ class MatrixRow : public VectorBase< RTYPE, true, MatrixRow<RTYPE> > {
206206
int parent_nrow ;
207207
int row ;
208208

209-
inline int get_parent_index(int i) const {
210-
RCPP_DEBUG_4( "MatrixRow<%d>::get_parent_index(int = %d), parent_nrow = %d >> %d\n", RTYPE, i, parent_nrow, i*parent_nrow )
211-
return i * parent_nrow ;
209+
inline R_xlen_t get_parent_index(int i) const {
210+
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 )
211+
return static_cast<R_xlen_t>(i) * parent_nrow ;
212212
}
213213
} ;
214214

@@ -310,7 +310,7 @@ class ConstMatrixRow : public VectorBase< RTYPE, true, ConstMatrixRow<RTYPE> > {
310310
{} ;
311311

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

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

334-
inline int get_parent_index(int i) const {
335-
RCPP_DEBUG_4( "ConstMatrixRow<%d>::get_parent_index(int = %d), parent_nrow = %d >> %d\n", RTYPE, i, parent_nrow, i*parent_nrow )
336-
return i * parent_nrow ;
334+
inline R_xlen_t get_parent_index(int i) const {
335+
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 )
336+
return static_cast<R_xlen_t>(i) * parent_nrow ;
337337
}
338338
} ;
339339
}

inst/include/Rcpp/vector/Vector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ class Vector :
331331
}
332332

333333
inline iterator begin() { return cache.get() ; }
334-
inline iterator end() { return cache.get() + static_cast<int>(size()) ; }
334+
inline iterator end() { return cache.get() + size() ; }
335335
inline const_iterator begin() const{ return cache.get_const() ; }
336336
inline const_iterator end() const{ return cache.get_const() + size() ; }
337337
inline const_iterator cbegin() const{ return cache.get_const() ; }

0 commit comments

Comments
 (0)