diff --git a/Tests/test_file_bmp.py b/Tests/test_file_bmp.py index 15b4b3670..bfe24e000 100644 --- a/Tests/test_file_bmp.py +++ b/Tests/test_file_bmp.py @@ -139,6 +139,13 @@ def test_rle8(): with Image.open("Tests/images/hopper_rle8_row_overflow.bmp") as im: assert_image_similar_tofile(im.convert("RGB"), "Tests/images/hopper.bmp", 12) + # Signal end of bitmap before the image is finished + with open("Tests/images/bmp/g/pal8rle.bmp", "rb") as fp: + data = fp.read(1063) + b"\x01" + with Image.open(io.BytesIO(data)) as im: + with pytest.raises(ValueError): + im.load() + def test_offset(): # This image has been hexedited diff --git a/src/PIL/BmpImagePlugin.py b/src/PIL/BmpImagePlugin.py index 60617dce3..7221d7f06 100644 --- a/src/PIL/BmpImagePlugin.py +++ b/src/PIL/BmpImagePlugin.py @@ -282,7 +282,7 @@ class BmpRleDecoder(ImageFile.PyDecoder): def decode(self, buffer): data = bytearray() x = 0 - while True: + while len(data) < self.state.xsize * self.state.ysize: num_pixels = self.fd.read(1)[0] byte = self.fd.read(1) if num_pixels: