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

Skip to content

Commit 84f3a6f

Browse files
committed
Merge pull request #6666 from mdboom/limit-image-size
Guard against too-large figures
1 parent 588b748 commit 84f3a6f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/matplotlib/tests/test_agg.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import numpy as np
1010
from numpy.testing import assert_array_almost_equal
1111

12+
from nose.tools import assert_raises
13+
1214
from matplotlib.image import imread
1315
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
1416
from matplotlib.figure import Figure
@@ -155,6 +157,13 @@ def test_long_path():
155157
fig.savefig(buff, format='png')
156158

157159

160+
@cleanup
161+
def test_too_large_image():
162+
fig = plt.figure(figsize=(300, 1000))
163+
buff = io.BytesIO()
164+
assert_raises(ValueError, fig.savefig, buff)
165+
166+
158167
if __name__ == "__main__":
159168
import nose
160169
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

src/_backend_agg_wrapper.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ static int PyRendererAgg_init(PyRendererAgg *self, PyObject *args, PyObject *kwd
177177
return -1;
178178
}
179179

180+
if (width >= 1 << 16 || height >= 1 << 16) {
181+
PyErr_Format(
182+
PyExc_ValueError,
183+
"Image size of %dx%d pixels is too large. "
184+
"It must be less than 2^16 in each direction.",
185+
width, height);
186+
return -1;
187+
}
188+
180189
CALL_CPP_INIT("RendererAgg", self->x = new RendererAgg(width, height, dpi))
181190

182191
return 0;

0 commit comments

Comments
 (0)