Raise an error when performing a negative crop

This commit is contained in:
Andrew Murray 2022-01-18 16:38:00 +11:00
parent 83d369a347
commit af90dd773b
3 changed files with 14 additions and 22 deletions

View File

@ -86,21 +86,12 @@ class TestDecompressionCrop:
pytest.warns(Image.DecompressionBombWarning, 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:
for value in ((-9999, -9999, -9990, -9990), (-999, -999, -990, -990)):
assert im.crop(value).size == (9, 9)
for value in warning_values:
pytest.warns(Image.DecompressionBombWarning, im.crop, value)
pytest.warns(Image.DecompressionBombWarning, im.crop, (-160, -160, 99, 99))
for value in error_values:
with pytest.raises(Image.DecompressionBombError):
im.crop(value)
with pytest.raises(Image.DecompressionBombError):
im.crop((-99909, -99990, 99999, 99999))

View File

@ -47,16 +47,12 @@ def test_wide_crop():
assert crop(-25, 75, 25, 125) == (1875, 625)
def test_negative_crop():
# Check negative crop size (@PIL171)
@pytest.mark.parametrize("box", ((8, 2, 2, 8), (2, 8, 8, 2), (8, 8, 2, 2)))
def test_negative_crop(box):
im = Image.new("RGB", (10, 10))
im = Image.new("L", (512, 512))
im = im.crop((400, 400, 200, 200))
assert im.size == (0, 0)
assert len(im.getdata()) == 0
with pytest.raises(IndexError):
im.getdata()[0]
with pytest.raises(ValueError):
im.crop(box)
def test_crop_float():

View File

@ -1145,6 +1145,11 @@ class Image:
if box is None:
return self.copy()
if box[2] < box[0]:
raise ValueError("Region right less than region left")
elif box[3] < box[1]:
raise ValueError("Region lower less than region upper")
self.load()
return self._new(self._crop(self.im, box))