Static code analyzer warning
wcout opened this issue · 10 comments
I've probed the clang analyzer on the code and it gives one warning. As the code is .. hm .. complex .. I can't judge if this is a valid one, so I simply show it here:
Seems like I broke the Clang analyzer! Yay…
- [50] cannot be false right away, as
ctbl
equals at least 4 (and at most 256) at the start, see line 102. code
gets initialized with exactlyctbl
zero values, starting at the address ofcode[0]
, and then gets appended 1 element each SP/MP iteration, until either its size (and the value ofctbl
which drives that process) equalsGIF_CLEN
or it gets invalidated, see line 118.prev
never exceedsctbl
between lines 109 and 130, asprev
equals 0 at the start and then gets its value fromcurr
at each iteration, whereascurr
exceedingctbl
terminates the function with the return value of –5 (see line 141), beforeprev
ever gets a chance to become greater thanctbl
.- when
ctbl
reachesGIF_CLEN
and thus cannot further expandcode
, its correspondingmask
preventscurr
from becoming greater thanGIF_CLEN – 1
, see line 118.
Maybe it`s worth filing a bug to Clang analyzer…
[UPD:]
However, there is one case where prev
might point to an uninitialized code
element. This happens right after a table drop, so prev
equals the table drop code, and ctbl
equals the end-of-stream code. Both codes cannot be used as color codes, so the garbage contained there cannot be used to color anything.
But still, to be fully garbage-free, we just need to correct one line. Please change
for (curr = ctbl++; curr; code[--curr] = 0); /** actual color codes **/
to
for (curr = ++ctbl; curr; code[--curr] = 0); /** actual color codes **/
and see if that makes Clang analyzer happy.
If it does, please also apply the analyzer to the latest commit, 0ed5be0, and see if it finds anything.
It still gives the one same warning with the latest commit.
Well, OK. Does it also complain if the loop goes as follows?
for (curr = ++ctbl; curr; )
code[--curr] = 0;
Yes
Please show me the report.
Well, now that does seem like a bug in the analyzer.
The loop condition is now obviously true.
May I close this ticket?
Yes please.
Your analysis seems profound, so it is really most probably a clang analyzer issue.
(Though I hoped, that fixing the memory corruptions would make this warning go away too).