Skip to content

Commit 4b9eaae

Browse files
authored
Merge pull request matplotlib#32036 from meeseeksmachine/auto-backport-of-pr-31894-on-v3.11.x
Backport PR matplotlib#31894 on branch v3.11.x (check transform mesh shape in _get_transform_mesh)
2 parents 85948f8 + ca2fb2e commit 4b9eaae

2 files changed

Lines changed: 26 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
@@ -1681,6 +1681,23 @@ def test__resample_valid_output():
16811681
resample(np.zeros((9, 9)), out)
16821682

16831683

1684+
def test__resample_nonaffine_mesh_shape():
1685+
# A non-affine transform whose inverse returns the wrong number of mesh
1686+
# points must be rejected rather than read past the mesh buffer.
1687+
class BadMeshTransform(Transform):
1688+
input_dims = output_dims = 2
1689+
1690+
def inverted(self):
1691+
return self
1692+
1693+
def transform(self, values):
1694+
return np.zeros((1, 2))
1695+
1696+
with pytest.raises(RuntimeError, match="mesh array should have shape"):
1697+
mpl._image.resample(np.zeros((9, 9)), np.zeros((9, 9)),
1698+
BadMeshTransform())
1699+
1700+
16841701
@pytest.mark.parametrize("data, interpolation, expected",
16851702
[(np.array([[0.1, 0.3, 0.2]]), mimage.NEAREST,
16861703
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ _get_transform_mesh(const py::object& transform, const py::ssize_t *dims)
8686
output_mesh_array.ndim()));
8787
}
8888

89+
// An undersized mesh would be read out of bounds by the resampler.
90+
if (output_mesh_array.shape(0) != mesh_dims[0] ||
91+
output_mesh_array.shape(1) != mesh_dims[1]) {
92+
throw std::runtime_error(
93+
"Inverse transformed mesh array should have shape ({}, {}) not ({}, {})"_s.format(
94+
mesh_dims[0], mesh_dims[1],
95+
output_mesh_array.shape(0), output_mesh_array.shape(1)));
96+
}
97+
8998
return output_mesh_array;
9099
}
91100

0 commit comments

Comments
 (0)