Skip to content

Commit 4567f1f

Browse files
committed
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.
1 parent 94d0a9f commit 4567f1f

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

include/pybind11/eigen.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ struct type_caster<Type, enable_if_t<is_eigen_dense_plain<Type>::value>> {
265265
return false;
266266

267267
auto fits = props::conformable(buf);
268+
if (!fits)
269+
return false;
270+
268271
// Allocate the new type, then build a numpy reference into it
269272
value = Type(fits.rows, fits.cols);
270273
auto ref = reinterpret_steal<array>(eigen_ref_array<props>(value));

tests/test_eigen.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ def test_partially_fixed():
6363
np.testing.assert_array_equal(
6464
partial_copy_four_cm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :])
6565

66+
# TypeError should be raise for a shape mismatch
67+
functions = [partial_copy_four_rm_r, partial_copy_four_rm_c,
68+
partial_copy_four_cm_r, partial_copy_four_cm_c]
69+
matrix_with_wrong_shape = [[1, 2],
70+
[3, 4]]
71+
for f in functions:
72+
with pytest.raises(TypeError) as excinfo:
73+
f(matrix_with_wrong_shape)
74+
assert "incompatible function arguments" in str(excinfo.value)
75+
6676

6777
def test_mutator_descriptors():
6878
from pybind11_tests import fixed_mutator_r, fixed_mutator_c, fixed_mutator_a

0 commit comments

Comments
 (0)