From 9930b05a331d70ad40fb12dafbe0e5cfd03c2bfc Mon Sep 17 00:00:00 2001 From: homm Date: Tue, 15 Sep 2015 04:06:51 +0300 Subject: [PATCH] fix tiff exif loading in case when file is empty or ended --- PIL/JpegImagePlugin.py | 2 +- Tests/test_file_tiff_metadata.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/PIL/JpegImagePlugin.py b/PIL/JpegImagePlugin.py index 3d01c5fc1..550276c02 100644 --- a/PIL/JpegImagePlugin.py +++ b/PIL/JpegImagePlugin.py @@ -428,7 +428,7 @@ def _getexif(self): # exif field 0x8825 is an offset pointer to the location # of the nested embedded gps exif ifd. # It should be a long, but may be corrupted. - file.seek(exif[0x8825]) + file.seek(exif[0x8825]) except (KeyError, TypeError): pass else: diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index 1b5e05b4f..f2197ad04 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -1,5 +1,8 @@ from __future__ import division +import io +import struct + from helper import unittest, PillowTestCase, hopper from PIL import Image, TiffImagePlugin, TiffTags @@ -136,6 +139,15 @@ class TestFileTiffMetadata(PillowTestCase): self.assertEqual(tag_ids['MakerNoteSafety'], 50741) self.assertEqual(tag_ids['BestQualityScale'], 50780) + def test_empty_metadata(self): + f = io.BytesIO(b'II*\x00\x08\x00\x00\x00') + head = f.read(8) + info = TiffImagePlugin.ImageFileDirectory(head) + try: + self.assert_warning(UserWarning, lambda: info.load(f)) + except struct.error: + self.fail("Should not be struct errors there.") + if __name__ == '__main__': unittest.main()