Merge pull request #3313 from dinkolubina/fix-img-crop

Fix _crop and tests
This commit is contained in:
Hugo 2018-09-26 16:50:54 +03:00 committed by GitHub
commit a3f7ce5b73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 5 deletions

View File

@ -61,6 +61,29 @@ 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(Image.DecompressionBombWarning, im.crop, value)
for value in error_values:
with self.assertRaises(Image.DecompressionBombError):
im.crop(value)
if __name__ == '__main__':
unittest.main()

View File

@ -1117,12 +1117,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))