Subject: bin/13552: bin/ps stuffs up width calculation for time field
To: None <gnats-bugs@gnats.netbsd.org>
From: None <sparkles@primenet.com.au>
List: netbsd-bugs
Date: 07/25/2001 15:34:06
>Number:         13552
>Category:       bin
>Synopsis:       log10() usage failure in bin/ps/print.c
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Jul 24 22:31:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Geoff C. Wing
>Release:        20010724
>Organization:
Geoff Wing : <gcw@pobox.com>
Rxvt Stuff : <gcw@rxvt.org>
Zsh Stuff  : <gcw@zsh.org>
>Environment:
System: NetBSD c.primenet.com.au 1.5W NetBSD 1.5W (C) #0: Wed Jul 18 15:23:30 EST 2001 root@c.primenet.com.au:/usr/netbsd/src/sys/arch/i386/compile/C i386
Architecture: i386
Machine: i386
>Description:
	print.c:cputime() has:
	...
                 * Ugg, this is the only field where a value of 0 longer
                 * than the column title, and log10(0) isn't good enough.
                 * Use SECSPERMIN, because secs is divided by that when
                 * passed to log10().
                 */
                if (secs == 0 && v->longestp == 0)
                        secs = SECSPERMIN;
                if (secs > v->longestp) {
                        /* "+6" for the "%02ld.%02ld" in the printf() below */
                        fmtlen = (int)log10((double)secs / SECSPERMIN) + 1 + 6;
                        v->longestp = secs;
                        if (fmtlen > v->width)
                                v->width = fmtlen;
                }
	...
	1) The first comment doesn't make sense to me.  Yes, log10(0) is
	   useless to us but what's the other stuff mean?
	2) log10() of any non-zero value <= 1/10 gives a result <= -1 so
	   casting to int gives a negative number.  In this case,
	   if 1 <= secs <= 6 then (int)log10((double)secs / SECSPERMIN) == -1
	   and fmtlen gets stuffed up

>How-To-Repeat:
	Inspect cputime() in print.c
>Fix:
	exercise for reader
>Release-Note:
>Audit-Trail:
>Unformatted: