@@ -717,6 +717,34 @@ def test_truncated_chunks(
717717 monkeypatch .setattr (ImageFile , "LOAD_TRUNCATED_IMAGES" , True )
718718 png .call (cid , 0 , 0 )
719719
720+ @pytest .mark .parametrize ("mode" , ("1" , "L" , "I;16" , "RGB" ))
721+ def test_truncated_trns_chunk (
722+ self , mode : str , monkeypatch : pytest .MonkeyPatch
723+ ) -> None :
724+ fp = BytesIO ()
725+ with PngImagePlugin .PngStream (fp ) as png :
726+ png .im_mode = mode
727+ with pytest .raises (ValueError , match = "Truncated tRNS chunk" ):
728+ png .call (b"tRNS" , 0 , 0 )
729+
730+ monkeypatch .setattr (ImageFile , "LOAD_TRUNCATED_IMAGES" , True )
731+ png .call (b"tRNS" , 0 , 0 )
732+
733+ def test_truncated_trns_chunk_in_file (
734+ self , monkeypatch : pytest .MonkeyPatch
735+ ) -> None :
736+ # HEAD declares a truecolour image, so tRNS must carry 6 bytes
737+ data = HEAD + chunk (b"tRNS" , bytes (4 )) + TAIL
738+
739+ with pytest .raises (ValueError , match = "Truncated tRNS chunk" ):
740+ with Image .open (BytesIO (data )):
741+ pass
742+
743+ monkeypatch .setattr (ImageFile , "LOAD_TRUNCATED_IMAGES" , True )
744+ with Image .open (BytesIO (data )) as im :
745+ assert im .mode == "RGB"
746+ assert "transparency" not in im .info
747+
720748 @pytest .mark .parametrize ("save_all" , (True , False ))
721749 def test_specify_bits (self , save_all : bool , tmp_path : Path ) -> None :
722750 im = hopper ("P" )
0 commit comments