-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7ff098b
FIX: segfault on truncated png
tacaswell 89bea54
STY: tab -> space conversion in c code
tacaswell 07633f3
STY: fix tab -> space and brace position
tacaswell a17787b
TST: add test of truncated files and buffers
tacaswell a647756
TST: use pytest tmpdir fixture instead of tempdir + shutil
tacaswell 6ebcb11
STY: lower case error messages
tacaswell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
TST: add test of truncated files and buffers
- Loading branch information
commit a17787b93e763c04a6879f3df0907d83dba889a5
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
|
||
|
@@ -46,3 +47,30 @@ def test_imread_png_uint16(): | |
|
||
assert (img.dtype == np.uint16) | ||
assert np.sum(img.flatten()) == 134184960 | ||
|
||
|
||
def test_truncated_file(): | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be in |
||
|
||
|
||
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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...)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.