Test decompression bomb warnings

This commit is contained in:
hugovk 2014-05-27 12:40:52 +03:00
parent b853696ad5
commit fd05e9c756
2 changed files with 40 additions and 3 deletions

View File

@ -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

View File

@ -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