[PATCH] top: do not overflow the scratch buffer when displaying processes with very large VSZ

William Pitcock nenolod at dereferenced.org
Tue Mar 6 19:28:21 UTC 2018


---
 procps/top.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/procps/top.c b/procps/top.c
index b777c494e..fac832833 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -694,10 +694,14 @@ static NOINLINE void display_process_list(int lines_rem, int scr_width)
 		CALC_STAT(pcpu, (s->pcpu*pcpu_scale + pcpu_half) >> pcpu_shift);
 #endif
 
-		if (s->vsz >= 100000)
-			sprintf(vsz_str_buf, "%6ldm", s->vsz/1024);
+		if ((s->vsz / (1024 * 1024)) >= 100000)
+			snprintf(vsz_str_buf, sizeof(vsz_str_buf), "%6ldt", s->vsz/(1024 * 1024 * 1024));
+		else if ((s->vsz / 1024) >= 100000)
+			snprintf(vsz_str_buf, sizeof(vsz_str_buf), "%6ldg", s->vsz/(1024 * 1024));
+		else if (s->vsz >= 100000)
+			snprintf(vsz_str_buf, sizeof(vsz_str_buf), "%6ldm", s->vsz/1024);
 		else
-			sprintf(vsz_str_buf, "%7lu", s->vsz);
+			snprintf(vsz_str_buf, sizeof(vsz_str_buf), "%7lu", s->vsz);
 		/* PID PPID USER STAT VSZ %VSZ [%CPU] COMMAND */
 		col = snprintf(line_buf, scr_width,
 				"\n" "%5u%6u %-8.8s %s%s" FMT
-- 
2.16.2



More information about the busybox mailing list