MdeModulePkg BrotliDecompressLib: Add the checker to avoid array out of bound

This change is to pass static analysis.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Liming Gao <liming.gao@intel.com>
Reviewed-by: Star Zeng <star.zeng@intel.com>
This commit is contained in:
Liming Gao 2018-10-16 15:27:15 +08:00
parent eae7b476c2
commit 1c3399d73d
1 changed files with 8 additions and 5 deletions

View File

@ -858,6 +858,7 @@ static BROTLI_INLINE uint32_t ReadBlockLength(const HuffmanCode* table,
uint32_t code; uint32_t code;
uint32_t nbits; uint32_t nbits;
code = ReadSymbol(table, br); code = ReadSymbol(table, br);
ASSERT (code < BROTLI_NUM_BLOCK_LEN_SYMBOLS);
nbits = kBlockLengthPrefixCode[code].nbits; /* nbits == 2..24 */ nbits = kBlockLengthPrefixCode[code].nbits; /* nbits == 2..24 */
return kBlockLengthPrefixCode[code].offset + BrotliReadBits(br, nbits); return kBlockLengthPrefixCode[code].offset + BrotliReadBits(br, nbits);
} }
@ -910,6 +911,7 @@ static BROTLI_NOINLINE void InverseMoveToFrontTransform(
uint32_t upper_bound = state->mtf_upper_bound; uint32_t upper_bound = state->mtf_upper_bound;
uint32_t* mtf = &state->mtf[1]; /* Make mtf[-1] addressable. */ uint32_t* mtf = &state->mtf[1]; /* Make mtf[-1] addressable. */
uint8_t* mtf_u8 = (uint8_t*)mtf; uint8_t* mtf_u8 = (uint8_t*)mtf;
uint8_t* mtf_u8t = mtf_u8 - 1;
/* Load endian-aware constant. */ /* Load endian-aware constant. */
const uint8_t b0123[4] = {0, 1, 2, 3}; const uint8_t b0123[4] = {0, 1, 2, 3};
uint32_t pattern; uint32_t pattern;
@ -928,13 +930,13 @@ static BROTLI_NOINLINE void InverseMoveToFrontTransform(
for (i = 0; i < v_len; ++i) { for (i = 0; i < v_len; ++i) {
int index = v[i]; int index = v[i];
uint8_t value = mtf_u8[index]; uint8_t value = mtf_u8[index];
upper_bound |= v[i]; upper_bound |= (uint32_t) v[i];
v[i] = value; v[i] = value;
mtf_u8[-1] = value; mtf_u8t[0] = value;
do { while (index >= 0) {
mtf_u8t[index + 1] = mtf_u8t[index];
index--; index--;
mtf_u8[index + 1] = mtf_u8[index]; }
} while (index >= 0);
} }
/* Remember amount of elements to be reinitialized. */ /* Remember amount of elements to be reinitialized. */
state->mtf_upper_bound = upper_bound >> 2; state->mtf_upper_bound = upper_bound >> 2;
@ -1566,6 +1568,7 @@ static BROTLI_INLINE BROTLI_BOOL ReadCommandInternal(
BrotliBitReaderState memento; BrotliBitReaderState memento;
if (!safe) { if (!safe) {
cmd_code = ReadSymbol(s->htree_command, br); cmd_code = ReadSymbol(s->htree_command, br);
ASSERT (cmd_code < BROTLI_NUM_COMMAND_SYMBOLS);
} else { } else {
BrotliBitReaderSaveState(br, &memento); BrotliBitReaderSaveState(br, &memento);
if (!SafeReadSymbol(s->htree_command, br, &cmd_code)) { if (!SafeReadSymbol(s->htree_command, br, &cmd_code)) {