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

fix overflow in MatrixColumn ctor #390

Merged
merged 1 commit into from
Nov 2, 2015
Merged

Conversation

thirdwing
Copy link
Member

This is for the error reported in mailing list: https://www.mail-archive.com/rcpp-devel@lists.r-forge.r-project.org/msg08582.html

#include <Rcpp.h>

// [[Rcpp::export]]
Rcpp::IntegerMatrix makeMat(int nrow=18, int ncol=136633572){
  Rcpp::IntegerMatrix mat(nrow, ncol);
  for (int i = 0; i < ncol; ++i) {
    Rcpp::MatrixColumn<INTSXP> col = mat.column(i);
    for (int j = 0; j < nrow; ++j){
      col[j] = 1;
    }
  }
  return mat;
}
/*
> Rcpp::sourceCpp("test.cpp")
> mat <- makeMat()

 *** caught segfault ***
address 0x7f9d542ee088, cause 'memory not mapped'

Traceback:
 1: .Primitive(".Call")(<pointer: 0x7fa1b368b2a0>, nrow, ncol)
 2: makeMat()
*/

In MatrixColumn's ctor, even i and n are both int, i * n can still cause an overflow, which will lead to a segfault.

@eddelbuettel
Copy link
Member

👍 from a casual glance -- and thanks for getting to it so quickly!

eddelbuettel added a commit that referenced this pull request Nov 2, 2015
fix overflow in MatrixColumn ctor
@eddelbuettel eddelbuettel merged commit 99ab658 into RcppCore:master Nov 2, 2015
eddelbuettel added a commit that referenced this pull request Nov 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants