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

Skip to content

FIX: segfault on truncated png #9257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 13, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
TST: add test of truncated files and buffers
  • Loading branch information
tacaswell committed Oct 2, 2017
commit a17787b93e763c04a6879f3df0907d83dba889a5
34 changes: 31 additions & 3 deletions lib/matplotlib/tests/test_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
unicode_literals)

import six

from six import BytesIO
import glob
import os

import shutil
import numpy as np
import pytest

from matplotlib.testing.decorators import image_comparison
from matplotlib import pyplot as plt
import matplotlib.cm as cm

import tempfile
import sys
on_win = (sys.platform == 'win32')

Expand Down Expand Up @@ -46,3 +47,30 @@ def test_imread_png_uint16():

assert (img.dtype == np.uint16)
assert np.sum(img.flatten()) == 134184960


def test_truncated_file():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to test both filenames and streams? (or rather, there doesn't seem to be a test of successfully reading from a stream (as opposed to a filename), which seems something more valuable to check...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because they go through different code paths.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but not in the erroring part right? (at that point every possible input has been converted to an object with a read method)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐑 Yup, I was confused be the _read_png_data in the first if / else block and thought that was reading the whole file, but is just pulling off the first 8 bytes.

d = tempfile.mkdtemp()
fname = os.path.join(d, 'test.png')
fname_t = os.path.join(d, 'test_truncated.png')
plt.savefig(fname)
with open(fname, 'rb') as fin:
buf = fin.read()
with open(fname_t, 'wb') as fout:
fout.write(buf[:20])

with pytest.raises(Exception):
plt.imread(fname_t)

shutil.rmtree(d)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in finally, or maybe use the tempdir pytest fixture.



def test_truncated_buffer():
b = BytesIO()
plt.savefig(b)
b.seek(0)
b2 = BytesIO(b.read(20))
b2.seek(0)

with pytest.raises(Exception):
plt.imread(b2)