Skip to content

Commit 53318b5

Browse files
authored
Take libtiff scanline geometry tags from the image, not tiffinfo/exif (#10226)
2 parents 0a87417 + 8501adc commit 53318b5

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎Tests/test_file_libtiff.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,26 @@ def test_non_bytes(
395395
with pytest.raises(ValueError, match="Incorrect tag value type"):
396396
im.save(out, tiffinfo=ifd)
397397

398+
@pytest.mark.parametrize(
399+
"tiffinfo",
400+
(
401+
{TiffImagePlugin.IMAGEWIDTH: 1 << 27},
402+
{TiffImagePlugin.SAMPLESPERPIXEL: 3},
403+
),
404+
)
405+
def test_geometry_tags(
406+
self, tiffinfo: dict[int, int], monkeypatch: pytest.MonkeyPatch, tmp_path: Path
407+
) -> None:
408+
# libtiff reads a scanline of the size described by the IMAGEWIDTH,
409+
# SAMPLESPERPIXEL and BITSPERSAMPLE tags from a buffer that only holds one
410+
# row of the image, so a tag describing a larger row must be rejected.
411+
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", True)
412+
413+
out = tmp_path / "temp.tif"
414+
im = hopper("L")
415+
with pytest.raises(OSError, match="encoder error -8"):
416+
im.save(out, tiffinfo=tiffinfo)
417+
398418
def test_inknames_tag(
399419
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
400420
) -> None:

‎src/libImaging/TiffDecode.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,14 @@ ImagingLibTiffEncode(Imaging im, ImagingCodecState state, UINT8 *buffer, int byt
820820
TIFF *tiff = clientstate->tiff;
821821

822822
if (state->state == 0) {
823+
// state->buffer holds one row of the image, but TIFFWriteScanline reads
824+
// a scanline of the size described by the IMAGEWIDTH, SAMPLESPERPIXEL
825+
// and BITSPERSAMPLE tags, so these must not describe a larger row.
826+
if (TIFFScanlineSize64(tiff) > (uint64_t)state->bytes) {
827+
state->errcode = IMAGING_CODEC_CONFIG;
828+
return -1;
829+
}
830+
823831
// Encoding line by line
824832
while (state->y < state->ysize) {
825833
state->shuffle(

0 commit comments

Comments
 (0)