Skip to content

Commit ba01f9c

Browse files
committed
Add check in ReadCompressedTextChunk() for enough data after keyword end
1 parent 533ed51 commit ba01f9c

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,26 +1402,31 @@ private void ReadCompressedTextChunk(ImageMetadata baseMetadata, PngMetadata met
14021402
return;
14031403
}
14041404

1405-
int zeroIndex = data.IndexOf((byte)0);
1406-
if (zeroIndex is < PngConstants.MinTextKeywordLength or > PngConstants.MaxTextKeywordLength)
1405+
int keywordEnd = data.IndexOf((byte)0);
1406+
if (keywordEnd is < PngConstants.MinTextKeywordLength or > PngConstants.MaxTextKeywordLength)
14071407
{
14081408
return;
14091409
}
14101410

1411-
byte compressionMethod = data[zeroIndex + 1];
1411+
if (keywordEnd < 0 || keywordEnd + 2 > data.Length)
1412+
{
1413+
return; // Not enough data for keyword + null + compression method.
1414+
}
1415+
1416+
byte compressionMethod = data[keywordEnd + 1];
14121417
if (compressionMethod != 0)
14131418
{
14141419
// Only compression method 0 is supported (zlib datastream with deflate compression).
14151420
return;
14161421
}
14171422

1418-
ReadOnlySpan<byte> keywordBytes = data[..zeroIndex];
1423+
ReadOnlySpan<byte> keywordBytes = data[..keywordEnd];
14191424
if (!TryReadTextKeyword(keywordBytes, out string name))
14201425
{
14211426
return;
14221427
}
14231428

1424-
ReadOnlySpan<byte> compressedData = data[(zeroIndex + 2)..];
1429+
ReadOnlySpan<byte> compressedData = data[(keywordEnd + 2)..];
14251430

14261431
if (this.TryDecompressTextData(compressedData, PngConstants.Encoding, out string? uncompressed)
14271432
&& !TryReadTextChunkMetadata(baseMetadata, name, uncompressed))

0 commit comments

Comments
 (0)