Skip to content

Commit 49a662c

Browse files
committed
check transform mesh shape in _get_transform_mesh
1 parent ea157d7 commit 49a662c

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

lib/matplotlib/tests/test_image.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,23 @@ def test__resample_valid_output():
16771677
resample(np.zeros((9, 9)), out)
16781678

16791679

1680+
def test__resample_nonaffine_mesh_shape():
1681+
# A non-affine transform whose inverse returns the wrong number of mesh
1682+
# points must be rejected rather than read past the mesh buffer.
1683+
class BadMeshTransform(Transform):
1684+
input_dims = output_dims = 2
1685+
1686+
def inverted(self):
1687+
return self
1688+
1689+
def transform(self, values):
1690+
return np.zeros((1, 2))
1691+
1692+
with pytest.raises(RuntimeError, match="mesh array should have shape"):
1693+
mpl._image.resample(np.zeros((9, 9)), np.zeros((9, 9)),
1694+
BadMeshTransform())
1695+
1696+
16801697
@pytest.mark.parametrize("data, interpolation, expected",
16811698
[(np.array([[0.1, 0.3, 0.2]]), mimage.NEAREST,
16821699
np.array([[0.1, 0.1, 0.1, 0.3, 0.3, 0.3, 0.3, 0.2, 0.2, 0.2]])),

src/_image_wrapper.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ _get_transform_mesh(const py::object& transform, const py::ssize_t *dims)
8686
output_mesh_array.ndim()));
8787
}
8888

89+
if (output_mesh_array.shape(0) != mesh_dims[0] ||
90+
output_mesh_array.shape(1) != mesh_dims[1]) {
91+
throw std::runtime_error(
92+
"Inverse transformed mesh array should have shape ({}, {}) not ({}, {})"_s.format(
93+
mesh_dims[0], mesh_dims[1],
94+
output_mesh_array.shape(0), output_mesh_array.shape(1)));
95+
}
96+
8997
return output_mesh_array;
9098
}
9199

0 commit comments

Comments
 (0)