Skip to content
Snippets Groups Projects
Commit a57998b2 authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Cap the search at LZH_ROOT (avoiding the sentry)

This should take care of the last three overflow and truncation
concerns that Coverity has.
parent ad1fddb9
Branches
Tags
No related merge requests found
Pipeline #7629 failed
...@@ -437,20 +437,27 @@ static bool lzh_update(huffman_t* huff, uint16_t c) ...@@ -437,20 +437,27 @@ static bool lzh_update(huffman_t* huff, uint16_t c)
/* /*
* If we now have a greater freq than the next node... * If we now have a greater freq than the next node...
* and are not already the last node * and are not already the last node
*
* The first comparison is not stricly necessary, but
* it shuts up Coverity since Coverity doesn't know that
* freq[LZH_TABLE_SZ] can never be less than tmp.
*
* It also prevents Coverity from thinking that c may
* be UINT16_MAX, so c + 1 overflows.
*/ */
if (tmp > huff->freq[c + 1]) { if (c < LZH_ROOT && tmp > huff->freq[c + 1]) {
/* /*
* Find the last node after the current one * Find the last node after the current one
* that has a lower frequency than our new one * that has a lower frequency than our new one
* *
* There's a sentry at LZH_TABLE_SZ, so we can't * There's a sentry at LZH_TABLE_SZ (one after
* "find" that one (and we don't actually need * LZH_ROOT), so we can't "find" that one (and
* the second test here) * we don't actually need the second test here)
* *
* We can do the range check second because * We can do the range check second because
* huff->freq[LZH_TABLE_SZ] is valid * huff->freq[LZH_TABLE_SZ] is valid
*/ */
for (l = c + 1; tmp > huff->freq[l + 1] && l < (LZH_TABLE_SZ - 1); l++) for (l = c + 1; tmp > huff->freq[l + 1] && l < LZH_ROOT; l++)
; ;
   
// Now swap nodes // Now swap nodes
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment