Skip to content

Commit 96d9e1c

Browse files
committed
Patch integer overflow in 'Rcpp::wrap(<Eigen::Matrix>)'
1 parent 106c26b commit 96d9e1c

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

inst/include/RcppEigenWrap.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,19 @@ namespace Rcpp{
8080

8181
// for plain dense objects
8282
template <typename T>
83-
SEXP eigen_wrap_plain_dense( const T& obj, Rcpp::traits::true_type ){
84-
typename Eigen::internal::conditional<T::IsRowMajor,
85-
Eigen::Matrix<typename T::Scalar,
86-
T::RowsAtCompileTime,
87-
T::ColsAtCompileTime>,
88-
const T&>::type objCopy(obj);
89-
int m = obj.rows(), n = obj.cols();
90-
R_xlen_t size = static_cast<R_xlen_t>(m) * n;
91-
SEXP ans = PROTECT(::Rcpp::wrap(objCopy.data(), objCopy.data() + size));
92-
if( T::ColsAtCompileTime != 1 ) {
83+
SEXP eigen_wrap_plain_dense( const T& obj, Rcpp::traits::true_type ) {
84+
typename Eigen::internal::conditional<
85+
T::IsRowMajor,
86+
Eigen::Matrix<typename T::Scalar,
87+
T::RowsAtCompileTime,
88+
T::ColsAtCompileTime>,
89+
const T&>::type objCopy(obj);
90+
R_xlen_t m = obj.rows(), n = obj.cols(), size = m * n;
91+
SEXP ans = PROTECT(::Rcpp::wrap(objCopy.data(), objCopy.data() + size));
92+
if ( T::ColsAtCompileTime != 1 ) {
93+
if (m > INT_MAX || n > INT_MAX) {
94+
throw std::runtime_error("array dimensions cannot exceed INT_MAX");
95+
}
9396
SEXP dd = PROTECT(::Rf_allocVector(INTSXP, 2));
9497
int *d = INTEGER(dd);
9598
d[0] = m;

0 commit comments

Comments
 (0)