Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3814c77

Browse filesBrowse files
committed
Merge pull request #5515 from mdboom/png-fishiness
Fix some theoretical problems with png reading
1 parent 071fc5c commit 3814c77
Copy full SHA for 3814c77

File tree

Expand file treeCollapse file tree

3 files changed

+9
-6
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+9
-6
lines changed

‎lib/matplotlib/backends/backend_agg.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_agg.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ def print_png(self, filename_or_obj, *args, **kwargs):
525525
close = True
526526
else:
527527
close = False
528+
528529
try:
529530
_png.write_png(renderer._renderer, filename_or_obj, self.figure.dpi)
530531
finally:

‎lib/matplotlib/image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/image.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,8 +1279,8 @@ def pilread(fname):
12791279
from PIL import Image
12801280
except ImportError:
12811281
return None
1282-
image = Image.open(fname)
1283-
return pil_to_array(image)
1282+
with Image.open(fname) as image:
1283+
return pil_to_array(image)
12841284

12851285
handlers = {'png': _png.read_png, }
12861286
if format is None:

‎src/_png.cpp

Copy file name to clipboardExpand all lines: src/_png.cpp
+6-4Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,12 @@ static void _read_png_data(PyObject *py_file_obj, png_bytep data, png_size_t len
241241
Py_ssize_t bufflen;
242242
if (read_method) {
243243
result = PyObject_CallFunction(read_method, (char *)"i", length);
244-
}
245-
if (PyBytes_AsStringAndSize(result, &buffer, &bufflen) == 0) {
246-
if (bufflen == (Py_ssize_t)length) {
247-
memcpy(data, buffer, length);
244+
if (PyBytes_AsStringAndSize(result, &buffer, &bufflen) == 0) {
245+
if (bufflen == (Py_ssize_t)length) {
246+
memcpy(data, buffer, length);
247+
} else {
248+
PyErr_SetString(PyExc_IOError, "read past end of file");
249+
}
248250
}
249251
}
250252
Py_XDECREF(read_method);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.