Replaced primitive "magic number" inside of JpegImagePlugin._accept() function by more correct version.

This commit is contained in:
Kirill Kuzminykh 2020-06-18 16:18:18 +03:00
parent d6be1331ce
commit f99e0b824b
2 changed files with 22 additions and 1 deletions

View File

@ -706,6 +706,26 @@ class TestFileJpeg:
with Image.open("Tests/images/icc-after-SOF.jpg") as im:
assert im.info["icc_profile"] == b"profile"
def test_reading_not_whole_file_for_define_it_type(self):
size = 1024 ** 2
buffer = BytesIO(b"\xFF" * size) # Many xFF bytes
buffer.max_pos = 0
orig_read = buffer.read
def read(n=-1):
res = orig_read(n)
buffer.max_pos = max(buffer.max_pos, buffer.tell())
return res
buffer.read = read
with pytest.raises(OSError):
Image.open(buffer)
# Only small part of file has been read.
# The upper limit of max_pos (8Kb) was chosen experimentally
# and increased approximately twice.
assert 0 < buffer.max_pos < 8 * 1024
@pytest.mark.skipif(not is_win32(), reason="Windows only")
@skip_unless_feature("jpg")

View File

@ -323,7 +323,8 @@ MARKER = {
def _accept(prefix):
return prefix[0:1] == b"\377"
# Magic number was taken from https://en.wikipedia.org/wiki/JPEG
return prefix[0:3] == b"\xFF\xD8\xFF"
##