diff --git a/Tests/test_decompression_bomb.py b/Tests/test_decompression_bomb.py index 4e3c26377..b0b9430d4 100644 --- a/Tests/test_decompression_bomb.py +++ b/Tests/test_decompression_bomb.py @@ -6,6 +6,7 @@ TEST_FILE = "Tests/images/hopper.ppm" ORIGINAL_LIMIT = Image.MAX_IMAGE_PIXELS +from PIL.Image import DecompressionBombWarning, DecompressionBombError class TestDecompressionBomb(PillowTestCase): @@ -61,6 +62,28 @@ class TestDecompressionCrop(PillowTestCase): self.assert_warning(Image.DecompressionBombWarning, self.src.crop, box) + def test_crop_decompression_checks(self): + + im = Image.new("RGB", (100, 100)) + + good_values = ((-9999, -9999, -9990, -9990), + (-999, -999, -990, -990)) + + warning_values = ((-160, -160, 99, 99), + (160, 160, -99, -99)) + + error_values = ((-99909, -99990, 99999, 99999), + (99909, 99990, -99999, -99999)) + + for value in good_values: + self.assertEqual(im.crop(value).size, (9,9)) + + for value in warning_values: + self.assert_warning(DecompressionBombWarning, im.crop, value) + + for value in error_values: + with self.assertRaises(DecompressionBombError): + im.crop(value) if __name__ == '__main__': unittest.main() diff --git a/Tests/test_image_crop.py b/Tests/test_image_crop.py index fe92dd865..27a7f07d1 100644 --- a/Tests/test_image_crop.py +++ b/Tests/test_image_crop.py @@ -2,7 +2,6 @@ from helper import unittest, PillowTestCase, hopper from PIL import Image - class TestImageCrop(PillowTestCase): def test_crop(self): diff --git a/src/PIL/Image.py b/src/PIL/Image.py index c58952657..9e73f901d 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1104,12 +1104,9 @@ class Image(object): x0, y0, x1, y1 = map(int, map(round, box)) - if x1 < x0: - x1 = x0 - if y1 < y0: - y1 = y0 + absolute_values = (abs(x1 - x0), abs(y1 - y0)) - _decompression_bomb_check((x1, y1)) + _decompression_bomb_check(absolute_values) return im.crop((x0, y0, x1, y1))