Block saving TIFF tag OSUBFILETYPE using libtiff

This commit is contained in:
Andrew Murray 2024-03-22 23:43:55 +11:00
parent ca973709a0
commit 3004c46683
2 changed files with 10 additions and 1 deletions

View File

@ -12,7 +12,7 @@ from pathlib import Path
import pytest import pytest
from PIL import Image, ImageFilter, ImageOps, TiffImagePlugin, TiffTags, features 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 ( from .helper import (
assert_image_equal, assert_image_equal,
@ -325,6 +325,12 @@ class TestFileLibTiff(LibTiffTestCase):
) )
TiffImagePlugin.WRITE_LIBTIFF = False 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: def test_subifd(self, tmp_path: Path) -> None:
outfile = str(tmp_path / "temp.tif") outfile = str(tmp_path / "temp.tif")
with Image.open("Tests/images/g4_orientation_6.tif") as im: with Image.open("Tests/images/g4_orientation_6.tif") as im:

View File

@ -74,6 +74,7 @@ MM = b"MM" # big-endian (Motorola style)
# Read TIFF files # Read TIFF files
# a few tag names, just to make the code below a bit more readable # a few tag names, just to make the code below a bit more readable
OSUBFILETYPE = 255
IMAGEWIDTH = 256 IMAGEWIDTH = 256
IMAGELENGTH = 257 IMAGELENGTH = 257
BITSPERSAMPLE = 258 BITSPERSAMPLE = 258
@ -1784,11 +1785,13 @@ def _save(im, fp, filename):
types = {} types = {}
# STRIPOFFSETS and STRIPBYTECOUNTS are added by the library # STRIPOFFSETS and STRIPBYTECOUNTS are added by the library
# based on the data in the strip. # based on the data in the strip.
# OSUBFILETYPE is deprecated.
# The other tags expect arrays with a certain length (fixed or depending on # The other tags expect arrays with a certain length (fixed or depending on
# BITSPERSAMPLE, etc), passing arrays with a different length will result in # BITSPERSAMPLE, etc), passing arrays with a different length will result in
# segfaults. Block these tags until we add extra validation. # segfaults. Block these tags until we add extra validation.
# SUBIFD may also cause a segfault. # SUBIFD may also cause a segfault.
blocklist += [ blocklist += [
OSUBFILETYPE,
REFERENCEBLACKWHITE, REFERENCEBLACKWHITE,
STRIPBYTECOUNTS, STRIPBYTECOUNTS,
STRIPOFFSETS, STRIPOFFSETS,