Browse Source

Added sanity check for generated huffman codes

pull/1926/head
Dmitry Pentin 4 years ago
parent
commit
1694a5bf84
  1. 11
      src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanTable.cs
  2. 4
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

11
src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanTable.cs

@ -94,6 +94,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
code++;
}
// 'code' is now 1 more than the last code used for codelength 'si'
// in the valid worst possible case 'code' would have the least
// significant bit set to 1, e.g. 1111(0) +1 => 1111(1)
// but it must still fit in 'si' bits since no huffman code can be equal to all 1s
// if last code is all ones, e.g. 1111(1), then incrementing it by 1 would yield
// a new code which occupies one extra bit, e.g. 1111(1) +1 => (1)1111(0)
if (code >= (1 << si))
{
JpegThrowHelper.ThrowInvalidImageContentException("Bad huffman table.");
}
code <<= 1;
si++;
}

4
src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

@ -1110,13 +1110,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
// Types 0..1 DC..AC
if (tableType > 1)
{
JpegThrowHelper.ThrowInvalidImageContentException($"Bad huffman table type: {tableType}");
JpegThrowHelper.ThrowInvalidImageContentException($"Bad huffman table type: {tableType}.");
}
// Max tables of each type
if (tableIndex > 3)
{
JpegThrowHelper.ThrowInvalidImageContentException($"Bad huffman table index: {tableIndex}");
JpegThrowHelper.ThrowInvalidImageContentException($"Bad huffman table index: {tableIndex}.");
}
stream.Read(huffmanDataSpan, 0, 16);

Loading…
Cancel
Save