Skip to content

Commit c512b14

Browse files
committed
If alpha length is greater then colorTable, slice length to colorTable length
1 parent 9c9b612 commit c512b14

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,10 +1253,10 @@ private static void AssignColorPalette(ReadOnlySpan<byte> palette, ReadOnlySpan<
12531253
ReadOnlySpan<Rgb24> rgbTable = MemoryMarshal.Cast<byte, Rgb24>(palette);
12541254
Color.FromPixel(rgbTable, colorTable);
12551255

1256+
// The tRNS chunk must not contain more alpha values than there are palette entries.
12561257
if (alpha.Length > colorTable.Length)
12571258
{
1258-
throw new InvalidImageContentException(
1259-
"The tRNS chunk contains more alpha values than there are palette entries.");
1259+
alpha = alpha.Slice(0, colorTable.Length);
12601260
}
12611261

12621262
if (alpha.Length > 0)

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public void Decode_InternationalText_WithTruncatedDataAfterLanguageTag_DoesNotTh
162162
}
163163

164164
[Fact]
165-
public void Decode_tRnsChunk_WithAlphaLengthGreaterColorTableLength_ExceptionIsThrown()
165+
public void Decode_tRnsChunk_WithAlphaLengthGreaterColorTableLength_ShouldNotThrowException()
166166
{
167167
byte[] payload = [137, 80, 78, 71, 13, 10, 26, 10, // PNG signature
168168
0, 0, 0, 13, // chunk length 13 bytes
@@ -181,9 +181,7 @@ public void Decode_tRnsChunk_WithAlphaLengthGreaterColorTableLength_ExceptionIsT
181181
120, 156, 99, 96, 0, 0, 0, 2, 0, 1, 72, 175, 164, 113]; // alpha.Length > colorTable.Length
182182

183183
using MemoryStream stream = new(payload);
184-
InvalidImageContentException exception = Assert.Throws<InvalidImageContentException>(() => Image.Load<Rgba32>(stream));
185-
186-
Assert.Equal("The tRNS chunk contains more alpha values than there are palette entries.", exception.Message);
184+
using Image<Rgba32> image = Image.Load<Rgba32>(stream);
187185
}
188186

189187
private static string GetChunkTypeName(uint value)

0 commit comments

Comments
 (0)