[eigen] Assigning a dynamic matrix to a fixed size matrix const reference causes invalid read

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Valgrind shows we've been getting invalid reads when assigning a dynamically sized matrix to a const reference of a fixed size matrix. The content of the fixed size matrix is nothing useful after the assignment. How does gcc not detect I'm trying to assign a different type to a reference? Debugging showed the copy constructor is called, while from my understanding it should just fail (or just assign the reference pointer, but not call a constructor). Non-const references fail as expected. Assigning pointers fails as expected as well.

Here a piece of code that reproduces the behavior:

#include <Eigen/Dense>

#include <iostream>

int main() {

  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> dynMatr(2,2);

  //Should fail to compile because of type mismatch! 
  const Eigen::Matrix<double,5,5,0,5,5>& refFixed=dynMatr;

  std::cout << "Original:" << dynMatr(1,1) << std::endl;
  std::cout << "Matrix<5x5> reference type: " << refFixed(1,1) << std::endl;
  return 0;

The output is:

play% g++ -DNDEBUG basictest.cxx
play% ../a.out                                                                                                                                  
Matrix<5x5> reference type: 0

Thanks and best regards,

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/