diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 8ec89965929..a99134431d1 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -1482,6 +1482,22 @@ def test_realloc_overflow(self, monkeypatch: pytest.MonkeyPatch) -> None: with pytest.raises(OSError, match="decoder error -9"): im.load() + def test_rgba_rows_per_strip_greater_than_height(self) -> None: + # 1000x1 Deflate YCbCr strip with RowsPerStrip of 1,000,000 + data = ( + b"SUkqAAgAAAALAAABBAABAAAA6AMAAAEBBAABAAAAAQAAAAIBAwADAAAAkgAA" + b"AAMBAwABAAAACAAAAAYBAwABAAAABgAAABEBBAABAAAAmAAAABUBAwABAAAA" + b"AwAAABYBBAABAAAAQEIPABcBBAABAAAAGgAAABwBAwABAAAAAQAAABICAwAC" + b"AAAAAQABAAAAAAAIAAgACAB42u3BMQEAAADCoOqLbg0PoAAAgHcDDP7cTA==" + ) + with Image.open(io.BytesIO(base64.b64decode(data))) as im: + assert isinstance(im, TiffImagePlugin.TiffImageFile) + assert im.tag_v2[TiffImagePlugin.ROWSPERSTRIP] == 1_000_000 + + # The decoding buffer is limited to the image height + im.load() + assert im.getpixel((0, 0)) == (128, 128, 128) + def test_unpacker_row_wider_than_libtiff_row(self) -> None: for data in ( # 16x16 8-bit greyscale tile, with a PhotometricInterpretation tag that diff --git a/src/libImaging/TiffDecode.c b/src/libImaging/TiffDecode.c index 137ea0052e4..ba9fe10685c 100644 --- a/src/libImaging/TiffDecode.c +++ b/src/libImaging/TiffDecode.c @@ -248,7 +248,8 @@ _decodeAsRGBA(Imaging im, ImagingCodecState state, TIFF *tiff) { ret = TIFFGetFieldDefaulted(tiff, TIFFTAG_ROWSPERSTRIP, &rows_per_block); } - if (ret != 1 || rows_per_block == (UINT32)(-1)) { + if (ret != 1 || rows_per_block > (UINT32)state->ysize) { + // No more than the image height is needed for the buffer rows_per_block = state->ysize; }