From 4567f1f82a72726b1c03cf0927212ade372c7f10 Mon Sep 17 00:00:00 2001 From: Dean Moldovan Date: Thu, 11 May 2017 15:38:39 +0200 Subject: [PATCH] Fix Eigen shape assertion error in dense matrix caster Missing conformability check was causing Eigen to create a 0x0 matrix with an error in debug mode and silent corruption in release mode. --- include/pybind11/eigen.h | 3 +++ tests/test_eigen.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/include/pybind11/eigen.h b/include/pybind11/eigen.h index 7698ae20..fc074dbc 100644 --- a/include/pybind11/eigen.h +++ b/include/pybind11/eigen.h @@ -265,6 +265,9 @@ struct type_caster::value>> { return false; auto fits = props::conformable(buf); + if (!fits) + return false; + // Allocate the new type, then build a numpy reference into it value = Type(fits.rows, fits.cols); auto ref = reinterpret_steal(eigen_ref_array(value)); diff --git a/tests/test_eigen.py b/tests/test_eigen.py index 4548fb0e..f9ad3a5f 100644 --- a/tests/test_eigen.py +++ b/tests/test_eigen.py @@ -63,6 +63,16 @@ def test_partially_fixed(): np.testing.assert_array_equal( partial_copy_four_cm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :]) + # TypeError should be raise for a shape mismatch + functions = [partial_copy_four_rm_r, partial_copy_four_rm_c, + partial_copy_four_cm_r, partial_copy_four_cm_c] + matrix_with_wrong_shape = [[1, 2], + [3, 4]] + for f in functions: + with pytest.raises(TypeError) as excinfo: + f(matrix_with_wrong_shape) + assert "incompatible function arguments" in str(excinfo.value) + def test_mutator_descriptors(): from pybind11_tests import fixed_mutator_r, fixed_mutator_c, fixed_mutator_a