Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0654800

Browse files
authored
Merge pull request #14478 from tacaswell/mnt_c_tkblit_bounds
MNT: protect from out-of-bounds data access at the c level
2 parents 2ff112b + 0f5fe5d commit 0654800

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

lib/matplotlib/testing/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ def mpl_test_settings(request):
4141
assert len(backend_marker.args) == 1, \
4242
"Marker 'backend' must specify 1 backend."
4343
backend, = backend_marker.args
44+
skip_on_importerror = backend_marker.kwargs.get(
45+
'skip_on_importerror', False)
4446
prev_backend = matplotlib.get_backend()
4547

4648
style = '_classic_test' # Default of cleanup and image_comparison too.
@@ -60,7 +62,7 @@ def mpl_test_settings(request):
6062
except ImportError as exc:
6163
# Should only occur for the cairo backend tests, if neither
6264
# pycairo nor cairocffi are installed.
63-
if 'cairo' in backend.lower():
65+
if 'cairo' in backend.lower() or skip_on_importerror:
6466
pytest.skip("Failed to switch to backend {} ({})."
6567
.format(backend, exc))
6668
else:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
import numpy as np
3+
from matplotlib import pyplot as plt
4+
5+
6+
@pytest.mark.backend('TkAgg', skip_on_importerror=True)
7+
def test_blit():
8+
from matplotlib.backends import _tkagg
9+
def evil_blit(photoimage, aggimage, offsets, bboxptr):
10+
data = np.asarray(aggimage)
11+
height, width = data.shape[:2]
12+
dataptr = (height, width, data.ctypes.data)
13+
_tkagg.blit(
14+
photoimage.tk.interpaddr(), str(photoimage), dataptr, offsets,
15+
bboxptr)
16+
17+
fig, ax = plt.subplots()
18+
for bad_boxes in ((-1, 2, 0, 2),
19+
(2, 0, 0, 2),
20+
(1, 6, 0, 2),
21+
(0, 2, -1, 2),
22+
(0, 2, 2, 0),
23+
(0, 2, 1, 6)):
24+
with pytest.raises(ValueError):
25+
evil_blit(fig.canvas._tkphoto,
26+
np.ones((4, 4, 4)),
27+
(0, 1, 2, 3),
28+
bad_boxes)

src/_tkagg.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ static PyObject *mpl_tk_blit(PyObject *self, PyObject *args)
6767
PyErr_SetString(PyExc_ValueError, "Failed to extract Tk_PhotoHandle");
6868
goto exit;
6969
}
70+
if (0 > y1 || y1 > y2 || y2 > height || 0 > x1 || x1 > x2 || x2 > width) {
71+
PyErr_SetString(PyExc_ValueError, "Attempting to draw out of bounds");
72+
goto exit;
73+
}
74+
7075
block.pixelPtr = data_ptr + 4 * ((height - y2) * width + x1);
7176
block.width = x2 - x1;
7277
block.height = y2 - y1;

0 commit comments

Comments
 (0)