66using SixLabors . ImageSharp . Formats ;
77using SixLabors . ImageSharp . Formats . Png ;
88using SixLabors . ImageSharp . PixelFormats ;
9+ using static SixLabors . ImageSharp . Tests . Memory . TestStructs ;
910
1011// ReSharper disable InconsistentNaming
1112namespace SixLabors . ImageSharp . Tests . Formats . Png ;
@@ -20,40 +21,40 @@ public partial class PngDecoderTests
2021 private static readonly byte [ ] Raw1X1PngIhdrAndpHYs =
2122 [
2223 // PNG Identifier
23- 0x89 , 0x50 , 0x4E , 0x47 , 0x0D , 0x0A , 0x1A , 0x0A ,
24+ 0x89 , 0x50 , 0x4E , 0x47 , 0x0D , 0x0A , 0x1A , 0x0A ,
2425
25- // IHDR
26- 0x00 , 0x00 , 0x00 , 0x0D , 0x49 , 0x48 , 0x44 , 0x52 , 0x00 ,
27- 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x01 , 0x08 , 0x02 ,
28- 0x00 , 0x00 , 0x00 ,
26+ // IHDR
27+ 0x00 , 0x00 , 0x00 , 0x0D , 0x49 , 0x48 , 0x44 , 0x52 , 0x00 ,
28+ 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x01 , 0x08 , 0x02 ,
29+ 0x00 , 0x00 , 0x00 ,
2930
30- // IHDR CRC
31- 0x90 , 0x77 , 0x53 , 0xDE ,
31+ // IHDR CRC
32+ 0x90 , 0x77 , 0x53 , 0xDE ,
3233
33- // pHYS
34- 0x00 , 0x00 , 0x00 , 0x09 , 0x70 , 0x48 , 0x59 , 0x73 , 0x00 ,
35- 0x00 , 0x0E , 0xC3 , 0x00 , 0x00 , 0x0E , 0xC3 , 0x01 ,
34+ // pHYS
35+ 0x00 , 0x00 , 0x00 , 0x09 , 0x70 , 0x48 , 0x59 , 0x73 , 0x00 ,
36+ 0x00 , 0x0E , 0xC3 , 0x00 , 0x00 , 0x0E , 0xC3 , 0x01 ,
3637
37- // pHYS CRC
38- 0xC7 , 0x6F , 0xA8 , 0x64
38+ // pHYS CRC
39+ 0xC7 , 0x6F , 0xA8 , 0x64
3940 ] ;
4041
4142 // Contains the png marker, IDAT and IEND chunks of a 1x1 pixel 32bit png 1 a single black pixel.
4243 private static readonly byte [ ] Raw1X1PngIdatAndIend =
4344 [
4445 // IDAT
45- 0x00 , 0x00 , 0x00 , 0x0C , 0x49 , 0x44 , 0x41 , 0x54 , 0x18 ,
46- 0x57 , 0x63 , 0x60 , 0x60 , 0x60 , 0x00 , 0x00 , 0x00 , 0x04 ,
47- 0x00 , 0x01 ,
46+ 0x00 , 0x00 , 0x00 , 0x0C , 0x49 , 0x44 , 0x41 , 0x54 , 0x18 ,
47+ 0x57 , 0x63 , 0x60 , 0x60 , 0x60 , 0x00 , 0x00 , 0x00 , 0x04 ,
48+ 0x00 , 0x01 ,
4849
49- // IDAT CRC
50- 0x5C , 0xCD , 0xFF , 0x69 ,
50+ // IDAT CRC
51+ 0x5C , 0xCD , 0xFF , 0x69 ,
5152
52- // IEND
53- 0x00 , 0x00 , 0x00 , 0x00 , 0x49 , 0x45 , 0x4E , 0x44 ,
53+ // IEND
54+ 0x00 , 0x00 , 0x00 , 0x00 , 0x49 , 0x45 , 0x4E , 0x44 ,
5455
55- // IEND CRC
56- 0xAE , 0x42 , 0x60 , 0x82
56+ // IEND CRC
57+ 0xAE , 0x42 , 0x60 , 0x82
5758 ] ;
5859
5960 [ Theory ]
@@ -77,6 +78,21 @@ public void Decode_IncorrectCRCForCriticalChunk_ExceptionIsThrown(uint chunkType
7778 }
7879 }
7980
81+ // https://github.com/SixLabors/ImageSharp/issues/3078
82+ [ Fact ]
83+ public void Decode_TruncatedPhysChunk_ExceptionIsThrown ( )
84+ {
85+ // 24 bytes — PNG signature + truncated pHYs chunk
86+ byte [ ] payload = Convert . FromHexString (
87+ "89504e470d0a1a0a3030303070485973" +
88+ "3030303030303030" ) ;
89+
90+ using MemoryStream stream = new ( payload ) ;
91+ InvalidImageContentException exception = Assert . Throws < InvalidImageContentException > ( ( ) => Image . Load < Rgba32 > ( stream ) ) ;
92+
93+ Assert . Equal ( "pHYs chunk is too short" , exception . Message ) ;
94+ }
95+
8096 private static string GetChunkTypeName ( uint value )
8197 {
8298 byte [ ] data = new byte [ 4 ] ;
0 commit comments