Skip to content

Commit bd19151

Browse files
Merge pull request #3094 from SixLabors/bp/Issue3093
PNG: Throw InvalidImageContentException when frame control chunk does not have enough data
2 parents 36334cd + 4e81c02 commit bd19151

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/ImageSharp/Formats/Png/Chunks/FrameControl.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,13 @@ public void WriteTo(Span<byte> buffer)
147147
/// <param name="data">The data to parse.</param>
148148
/// <returns>The parsed fcTL.</returns>
149149
public static FrameControl Parse(ReadOnlySpan<byte> data)
150-
=> new(
150+
{
151+
if (data.Length < Size)
152+
{
153+
PngThrowHelper.ThrowInvalidImageContentException("The frame control chunk does not contain enough data!");
154+
}
155+
156+
return new(
151157
sequenceNumber: BinaryPrimitives.ReadUInt32BigEndian(data[..4]),
152158
width: BinaryPrimitives.ReadUInt32BigEndian(data[4..8]),
153159
height: BinaryPrimitives.ReadUInt32BigEndian(data[8..12]),
@@ -157,4 +163,5 @@ public static FrameControl Parse(ReadOnlySpan<byte> data)
157163
delayDenominator: BinaryPrimitives.ReadUInt16BigEndian(data[22..24]),
158164
disposalMode: (FrameDisposalMode)(data[24] + 1),
159165
blendMode: (FrameBlendMode)data[25]);
166+
}
160167
}

tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.Chunks.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ public void Decode_TruncatedPhysChunk_ExceptionIsThrown()
9292
Assert.Equal("pHYs chunk is too short", exception.Message);
9393
}
9494

95+
// https://github.com/SixLabors/ImageSharp/issues/3093
96+
[Fact]
97+
public void Decode_TruncatedFrameControlChunk_ExceptionIsThrown()
98+
{
99+
// PNG signature + truncated frame control chunk
100+
byte[] payload = Convert.FromHexString(
101+
"89504e470d0a1a0a424d3a00000000007f000000000028030405060000000100" +
102+
"000101002000000000000000000000000000ff00006663544cff190000000000" +
103+
"010000424d000100000101002000000000");
104+
105+
using MemoryStream stream = new(payload);
106+
InvalidImageContentException exception = Assert.Throws<InvalidImageContentException>(() => Image.Load<Rgba32>(stream));
107+
108+
Assert.Equal("The frame control chunk does not contain enough data!", exception.Message);
109+
}
110+
95111
// https://github.com/SixLabors/ImageSharp/issues/3079
96112
[Fact]
97113
public void Decode_CompressedTxtChunk_WithTruncatedData_DoesNotThrow()

0 commit comments

Comments
 (0)