From 1e4447d171c46143840ea4ab5443323a87cc1668 Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Thu, 8 Dec 2011 13:13:59 -0600 Subject: [PATCH] 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 --- top.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/top.c b/top.c index 9ea19bb5..4b9fd4ec 100644 --- a/top.c +++ b/top.c @@ -1409,7 +1409,7 @@ static void calibrate_fields (void) { for (i = w->totpflgs - 1; -1 < i; i--) { f = w->pflgsall[i]; #ifndef USE_X_COLHDR - if (P_MAXPFLGS < f) { w->endpflg = i; continue; } + if (P_MAXPFLGS <= f) { w->endpflg = i; continue; } #endif h = Fieldstab[f].head; if (Screen_cols < ((int)(s - w->columnhdr) + (int)strlen(h))) break;