[procps] [RFC/PATCH] top: avoid array underflow warnings

  • From: Mike Frysinger <vapier@xxxxxxxxxx>
  • To: procps@xxxxxxxxxxxxx
  • Date: Thu, 26 May 2016 15:30:38 -0400

Commit e1cd74eec9f7dd6918476e963a3a57d99ae61e3d added a call to
make_str where it passed in an empty buffer.  When gcc sees this
with the snprintf logic, it throws a warning about a possible
array underflow:
top/top.c: In function 'task_show':
top/top.c:1528:10: warning: array subscript is below array bounds 
[-Warray-bounds]
       buf[width-1] = COLPLUSCH;

Add an explicit size check to fix that warning.
---
 top/top.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/top/top.c b/top/top.c
index e355747..4fe97cb 100644
--- a/top/top.c
+++ b/top/top.c
@@ -1510,7 +1510,8 @@ static inline const char *make_str (const char *str, int 
width, int justr, int c
    static char buf[SCREENMAX];
 
    if (width < snprintf(buf, sizeof(buf), "%s", str)) {
-      buf[width-1] = COLPLUSCH;
+      if (width > 0)
+        buf[width-1] = COLPLUSCH;
       AUTOX_COL(col);
    }
    return justify_pad(buf, width, justr);
-- 
2.8.2


Other related posts: