From 3004c46683db95ccfaeb02ecf64ee92b4356dcca Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 22 Mar 2024 23:43:55 +1100 Subject: [PATCH] Block saving TIFF tag OSUBFILETYPE using libtiff --- Tests/test_file_libtiff.py | 8 +++++++- src/PIL/TiffImagePlugin.py | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 908464a11b7..14504e58999 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -12,7 +12,7 @@ import pytest from PIL import Image, ImageFilter, ImageOps, TiffImagePlugin, TiffTags, features -from PIL.TiffImagePlugin import SAMPLEFORMAT, STRIPOFFSETS, SUBIFD +from PIL.TiffImagePlugin import OSUBFILETYPE, SAMPLEFORMAT, STRIPOFFSETS, SUBIFD from .helper import ( assert_image_equal, @@ -325,6 +325,12 @@ def check_tags( ) TiffImagePlugin.WRITE_LIBTIFF = False + def test_osubfiletype(self, tmp_path: Path) -> None: + outfile = str(tmp_path / "temp.tif") + with Image.open("Tests/images/g4_orientation_6.tif") as im: + im.tag_v2[OSUBFILETYPE] = 1 + im.save(outfile) + def test_subifd(self, tmp_path: Path) -> None: outfile = str(tmp_path / "temp.tif") with Image.open("Tests/images/g4_orientation_6.tif") as im: diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index b59139f5858..bcb3547eb8e 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -74,6 +74,7 @@ # Read TIFF files # a few tag names, just to make the code below a bit more readable +OSUBFILETYPE = 255 IMAGEWIDTH = 256 IMAGELENGTH = 257 BITSPERSAMPLE = 258 @@ -1784,11 +1785,13 @@ def _save(im, fp, filename): types = {} # STRIPOFFSETS and STRIPBYTECOUNTS are added by the library # based on the data in the strip. + # OSUBFILETYPE is deprecated. # The other tags expect arrays with a certain length (fixed or depending on # BITSPERSAMPLE, etc), passing arrays with a different length will result in # segfaults. Block these tags until we add extra validation. # SUBIFD may also cause a segfault. blocklist += [ + OSUBFILETYPE, REFERENCEBLACKWHITE, STRIPBYTECOUNTS, STRIPOFFSETS,