Skip to content

Commit 60cc436

Browse files
committed
Image: handle images with zero columns or rows.
Two methods in the _image extension module now raise an exception if the number of rows or columns is zero; previously they could segfault. A particular circumstance that can cause this (setting xlim completely outside the image extent) is handled directly in the image module without raising an exception. Closes #3091.
1 parent e2c5918 commit 60cc436

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

lib/matplotlib/image.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ def _draw_unsampled_image(self, renderer, gc):
327327
im.reset_matrix()
328328
numrows, numcols = im.get_size()
329329

330+
if numrows <= 0 or numcols <= 0:
331+
return
330332
im.resize(numcols, numrows) # just to create im.bufOut that
331333
# is required by backends. There
332334
# may be better solution -JJL

src/_image.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,10 @@ Image::resize(const Py::Tuple& args, const Py::Dict& kwargs)
374374
int numcols = Py::Int(args[0]);
375375
int numrows = Py::Int(args[1]);
376376

377-
if (numcols < 0 || numrows < 0)
377+
if (numcols <= 0 || numrows <= 0)
378378
{
379-
throw Py::RuntimeError(
380-
"Width and height must have non-negative values");
379+
throw Py::RuntimeError(
380+
"Width and height must have positive values");
381381
}
382382

383383
colsOut = numcols;

src/_image.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ class Image : public Py::PythonExtension<Image>
4545
inline Py::Object flipud_out(const Py::Tuple& args)
4646
{
4747
args.verify_length(0);
48+
if (colsOut <= 0 || rowsOut <= 0)
49+
{
50+
throw Py::RuntimeError(
51+
"Width and height must have positive values");
52+
}
53+
4854
int stride = rbufOut->stride();
4955
//std::cout << "flip before: " << rbufOut->stride() << std::endl;
5056
rbufOut->attach(bufferOut, colsOut, rowsOut, -stride);

0 commit comments

Comments
 (0)