Browse Source

If alpha length is greater then colorTable, slice length to colorTable length

pull/3084/head
Brian Popow 7 months ago
parent
commit
c512b142f9
  1. 4
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  2. 6
      tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.Chunks.cs

4
src/ImageSharp/Formats/Png/PngDecoderCore.cs

@ -1253,10 +1253,10 @@ internal sealed class PngDecoderCore : ImageDecoderCore
ReadOnlySpan<Rgb24> rgbTable = MemoryMarshal.Cast<byte, Rgb24>(palette); ReadOnlySpan<Rgb24> rgbTable = MemoryMarshal.Cast<byte, Rgb24>(palette);
Color.FromPixel(rgbTable, colorTable); Color.FromPixel(rgbTable, colorTable);
// The tRNS chunk must not contain more alpha values than there are palette entries.
if (alpha.Length > colorTable.Length) if (alpha.Length > colorTable.Length)
{ {
throw new InvalidImageContentException( alpha = alpha.Slice(0, colorTable.Length);
"The tRNS chunk contains more alpha values than there are palette entries.");
} }
if (alpha.Length > 0) if (alpha.Length > 0)

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

@ -162,7 +162,7 @@ public partial class PngDecoderTests
} }
[Fact] [Fact]
public void Decode_tRnsChunk_WithAlphaLengthGreaterColorTableLength_ExceptionIsThrown() public void Decode_tRnsChunk_WithAlphaLengthGreaterColorTableLength_ShouldNotThrowException()
{ {
byte[] payload = [137, 80, 78, 71, 13, 10, 26, 10, // PNG signature byte[] payload = [137, 80, 78, 71, 13, 10, 26, 10, // PNG signature
0, 0, 0, 13, // chunk length 13 bytes 0, 0, 0, 13, // chunk length 13 bytes
@ -181,9 +181,7 @@ public partial class PngDecoderTests
120, 156, 99, 96, 0, 0, 0, 2, 0, 1, 72, 175, 164, 113]; // alpha.Length > colorTable.Length 120, 156, 99, 96, 0, 0, 0, 2, 0, 1, 72, 175, 164, 113]; // alpha.Length > colorTable.Length
using MemoryStream stream = new(payload); using MemoryStream stream = new(payload);
InvalidImageContentException exception = Assert.Throws<InvalidImageContentException>(() => Image.Load<Rgba32>(stream)); using Image<Rgba32> image = Image.Load<Rgba32>(stream);
Assert.Equal("The tRNS chunk contains more alpha values than there are palette entries.", exception.Message);
} }
private static string GetChunkTypeName(uint value) private static string GetChunkTypeName(uint value)

Loading…
Cancel
Save