Re: [PATCH] top: do not overflow the scratch buffer when displaying processes with very large VSZ
Hi, On Tue, Mar 6, 2018 at 8:57 PM, Denys Vlasenko wrote: >> --- 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 >= 10) >> - sprintf(vsz_str_buf, "%6ldm", s->vsz/1024); >> + if ((s->vsz / (1024 * 1024)) >= 10) >> + snprintf(vsz_str_buf, sizeof(vsz_str_buf), "%6ldt", >> s->vsz/(1024 * 1024 * 1024)); >> + else if ((s->vsz / 1024) >= 10) >> + snprintf(vsz_str_buf, sizeof(vsz_str_buf), "%6ldg", >> s->vsz/(1024 * 1024)); >> + else if (s->vsz >= 10) >> + 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 > > I propose this instead: > > > - if (s->vsz >= 10) > - sprintf(vsz_str_buf, "%6ldm", s->vsz/1024); > - else > - sprintf(vsz_str_buf, "%7lu", s->vsz); > + smart_ulltoa5(s->vsz, vsz_str_buf, " mgtpezy"); > /* PID PPID USER STAT VSZ %VSZ [%CPU] COMMAND */ > col = snprintf(line_buf, scr_width, > - "\n" "%5u%6u %-8.8s %s%s" FMT > + "\n" "%5u%6u %-8.8s %s %.5s" FMT > IF_FEATURE_TOP_SMP_PROCESS(" %3d") > IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE(FMT) > " ", Sounds good to me. I wasn't aware of smart_ulltoa5(). William ___ busybox mailing list busybox@busybox.net http://lists.busybox.net/mailman/listinfo/busybox
Re: [PATCH] top: do not overflow the scratch buffer when displaying processes with very large VSZ
> --- 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 >= 10) > - sprintf(vsz_str_buf, "%6ldm", s->vsz/1024); > + if ((s->vsz / (1024 * 1024)) >= 10) > + snprintf(vsz_str_buf, sizeof(vsz_str_buf), "%6ldt", > s->vsz/(1024 * 1024 * 1024)); > + else if ((s->vsz / 1024) >= 10) > + snprintf(vsz_str_buf, sizeof(vsz_str_buf), "%6ldg", > s->vsz/(1024 * 1024)); > + else if (s->vsz >= 10) > + 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 I propose this instead: - if (s->vsz >= 10) - sprintf(vsz_str_buf, "%6ldm", s->vsz/1024); - else - sprintf(vsz_str_buf, "%7lu", s->vsz); + smart_ulltoa5(s->vsz, vsz_str_buf, " mgtpezy"); /* PID PPID USER STAT VSZ %VSZ [%CPU] COMMAND */ col = snprintf(line_buf, scr_width, - "\n" "%5u%6u %-8.8s %s%s" FMT + "\n" "%5u%6u %-8.8s %s %.5s" FMT IF_FEATURE_TOP_SMP_PROCESS(" %3d") IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE(FMT) " ", ___ busybox mailing list busybox@busybox.net http://lists.busybox.net/mailman/listinfo/busybox
[PATCH] top: do not overflow the scratch buffer when displaying processes with very large VSZ
--- 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 >= 10) - sprintf(vsz_str_buf, "%6ldm", s->vsz/1024); + if ((s->vsz / (1024 * 1024)) >= 10) + snprintf(vsz_str_buf, sizeof(vsz_str_buf), "%6ldt", s->vsz/(1024 * 1024 * 1024)); + else if ((s->vsz / 1024) >= 10) + snprintf(vsz_str_buf, sizeof(vsz_str_buf), "%6ldg", s->vsz/(1024 * 1024)); + else if (s->vsz >= 10) + 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 ___ busybox mailing list busybox@busybox.net http://lists.busybox.net/mailman/listinfo/busybox