BuildHuffmanTable: add an assert for offset[] bounds

And provide a clear comment explaining why the index of offset[] is
always checked within bounds.

Bug:webp:622
Change-Id: Id9b973a804b74c53dfb291f1a9dae649c0daed9d
This commit is contained in:
Jonathan Grant 2024-01-13 20:23:26 +00:00 committed by James Zern
parent 85e098e58d
commit fa6f56496a

View File

@ -124,6 +124,10 @@ static int BuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
const int symbol_code_length = code_lengths[symbol]; const int symbol_code_length = code_lengths[symbol];
if (code_lengths[symbol] > 0) { if (code_lengths[symbol] > 0) {
if (sorted != NULL) { if (sorted != NULL) {
assert(offset[symbol_code_length] < code_lengths_size);
// The following check is not redundant with the assert. It prevents a
// potential buffer overflow that the optimizer might not be able to
// rule out on its own.
if (offset[symbol_code_length] >= code_lengths_size) { if (offset[symbol_code_length] >= code_lengths_size) {
return 0; return 0;
} }