Fix bug #884: No malloc for zero-sized matrices or for Ref without temporaries

This commit is contained in:
Christoph Hertzberg 2014-09-25 16:05:17 +02:00
parent 27d6b4daf9
commit 4ba8aa1482
2 changed files with 36 additions and 1 deletions

View File

@ -454,6 +454,8 @@ template<typename T, bool Align> inline T* conditional_aligned_realloc_new(T* pt
template<typename T, bool Align> inline T* conditional_aligned_new_auto(size_t size)
{
if(size==0)
return 0; // short-cut. Also fixes Bug 884
check_size_for_overflow<T>(size);
T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
if(NumTraits<T>::RequireInitialization)

View File

@ -165,6 +165,38 @@ void ctms_decompositions()
Eigen::JacobiSVD<Matrix> jSVD; jSVD.compute(A, ComputeFullU | ComputeFullV);
}
void test_zerosized() {
// default constructors:
Eigen::MatrixXd A;
Eigen::VectorXd v;
// explicit zero-sized:
Eigen::ArrayXXd A0(0,0);
Eigen::ArrayXd v0(0);
// assigning empty objects to each other:
A=A0;
v=v0;
}
template<typename MatrixType> void test_reference(const MatrixType& m) {
typedef typename MatrixType::Scalar Scalar;
enum { Flag = MatrixType::IsRowMajor ? Eigen::RowMajor : Eigen::ColMajor};
enum { TransposeFlag = !MatrixType::IsRowMajor ? Eigen::RowMajor : Eigen::ColMajor};
typename MatrixType::Index rows = m.rows(), cols=m.cols();
// Dynamic reference:
typedef Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Flag > > Ref;
typedef Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, TransposeFlag> > RefT;
Ref r1(m);
Ref r2(m.block(rows/3, cols/4, rows/2, cols/2));
RefT r3(m.transpose());
RefT r4(m.topLeftCorner(rows/2, cols/2).transpose());
VERIFY_RAISES_ASSERT(RefT r5(m));
VERIFY_RAISES_ASSERT(Ref r6(m.transpose()));
VERIFY_RAISES_ASSERT(Ref r7(Scalar(2) * m));
}
void test_nomalloc()
{
// check that our operator new is indeed called:
@ -175,5 +207,6 @@ void test_nomalloc()
// Check decomposition modules with dynamic matrices that have a known compile-time max size (ctms)
CALL_SUBTEST_4(ctms_decompositions<float>());
CALL_SUBTEST_5(test_zerosized());
CALL_SUBTEST_6(test_reference(Matrix<float,32,32>()));
}