top: circumvent a false positive smatch error

The smatch error --------------
top.c +1414 calibrate_fields(78) error: buffer overflow 'Fieldstab' 39 <= 39

  The code -----------------------
if (P_MAXPFLGS < f) { w->endpflg = i; continue; }

  The background -----------------
The enum P_MAXPFLGS is strictly a fencepost and can *never* appear in
the arrays pflgsall or procflgs.  Thus it (39th element) cannot be used
in referencing Fieldstab.

However, two enums of higher value (X_XON=40 and X_XOF=41) *can* appear
in those arrays.  But the test against the fencepost ensures that those
two enums are *never* used in referencing Fieldstab.

When the analyzer sees the conditional using '<' and not '<='
it reports a false positive.

The source was changed to accommodate the tool's deficiency
This commit is contained in:
Jim Warner 2011-12-08 13:13:59 -06:00 committed by Craig Small
parent cd608f462e
commit 1e4447d171

2
top.c
View File

@ -1409,7 +1409,7 @@ static void calibrate_fields (void) {
for (i = w->totpflgs - 1; -1 < i; i--) { for (i = w->totpflgs - 1; -1 < i; i--) {
f = w->pflgsall[i]; f = w->pflgsall[i];
#ifndef USE_X_COLHDR #ifndef USE_X_COLHDR
if (P_MAXPFLGS < f) { w->endpflg = i; continue; } if (P_MAXPFLGS <= f) { w->endpflg = i; continue; }
#endif #endif
h = Fieldstab[f].head; h = Fieldstab[f].head;
if (Screen_cols < ((int)(s - w->columnhdr) + (int)strlen(h))) break; if (Screen_cols < ((int)(s - w->columnhdr) + (int)strlen(h))) break;