diff --git a/PIL/Image.py b/PIL/Image.py index f8977944b..5ffa5b64b 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -2104,7 +2104,7 @@ _fromarray_typemap[((1, 1), _ENDIAN + "i4")] = ("I", "I") _fromarray_typemap[((1, 1), _ENDIAN + "f4")] = ("F", "F") -def _compression_bomb_check(size): +def _decompression_bomb_check(size): if MAX_IMAGE_PIXELS is None: return @@ -2157,7 +2157,7 @@ def open(fp, mode="r"): fp.seek(0) # return factory(fp, filename) im = factory(fp, filename) - _compression_bomb_check(im.size) + _decompression_bomb_check(im.size) return im except (SyntaxError, IndexError, TypeError): #import traceback @@ -2173,7 +2173,7 @@ def open(fp, mode="r"): fp.seek(0) # return factory(fp, filename) im = factory(fp, filename) - _compression_bomb_check(im.size) + _decompression_bomb_check(im.size) return im except (SyntaxError, IndexError, TypeError): #import traceback diff --git a/Tests/test_decompression_bomb.py b/Tests/test_decompression_bomb.py new file mode 100644 index 000000000..1b3873c1b --- /dev/null +++ b/Tests/test_decompression_bomb.py @@ -0,0 +1,37 @@ +from tester import * + +from PIL import Image + +test_file = "Images/lena.ppm" + + +def test_no_warning_small_file(): + # Implicit assert: no warning. + # A warning would cause a failure. + Image.open(test_file) + + +def test_no_warning_no_limit(): + # Arrange + # Turn limit off + Image.MAX_IMAGE_PIXELS = None + assert_equal(Image.MAX_IMAGE_PIXELS, None) + + # Act / Assert + # Implicit assert: no warning. + # A warning would cause a failure. + Image.open(test_file) + + +def test_warning(): + # Arrange + # Set limit to a low, easily testable value + Image.MAX_IMAGE_PIXELS = 10 + assert_equal(Image.MAX_IMAGE_PIXELS, 10) + + # Act / Assert + assert_warning( + RuntimeWarning, + lambda: Image.open(test_file)) + +# End of file