Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 30 Nov 1998 23:10:50 -0800 (PST)
From:      waterman@acm.org
To:        FreeBSD-gnats-submit@FreeBSD.ORG
Subject:   bin/8913: negative time values for csh 'time' built-in
Message-ID:  <199812010710.XAA11274@home>

next in thread | raw e-mail | index | archive | help

>Number:         8913
>Category:       bin
>Synopsis:       negative time values for csh 'time' built-in
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:
>Keywords:
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Nov 30 23:10:00 PST 1998
>Last-Modified:
>Originator:     TS Waterman
>Organization:
PricewaterhouseCoopers
>Release:        FreeBSD 2.2.7-STABLE i386
>Environment:

	
	running /bin/csh

>Description:

	
	Long process reports negative time using csh builtin 'time' cmd.

	for example:
        37809.2u 178.2s 11:08:56.05 -12.-3% 184+294k 54+425io 2009pf+0w
				    ^^^^^^^


>How-To-Repeat:

	
	Run a process over 10 hours or so, in csh, using 'time'

>Fix:
	
	
	It looks like an int overflow on a time value in the percent
	calculation.

in /usr/src/bin/csh/time.c:

            case 'P':           /* percent time spent running */
                /* check if it did not run at all */
-               i = (ms == 0) ? 0 : (t * 1000 / ms);
-               /* nn.n% */
-               (void) fprintf(cshout, "%ld.%01ld%%", i / 10, i % 10);
                break;

declarations:

time_t t;  /* time_t ==> _BSD_TIME_T_ ==> long,  really */
long i;
int ms;
where 'i' is a long, but 'ms' is an int.

In the line      i = (ms == 0) ? 0 : (t * 1000 / ms);

In my example: t = 37809 seconds * 1000   //converted to milliseconds
The extra factor of 1000 added in the percent calculation
(t*1000 == 3.78e10) is enough to overflow an int,
but not a long. 't' is a long, so this calc should be computed 
as a long, right?

What's broken here?

The easy solution is to convert to float calculations (this isn't at all
time critical).  notice the change to a factor of 100:

+               float ft = (ms == 0) ? 0 : ((float)t * 100 / (float)ms);
+               /* nn.n% */
+               (void) fprintf(cshout, "%4.1f%%", ft);

The bigger question is why the first implementation doesn't work,
but it's not a life threatening matter.

perhaps just casting the original calculation to long internally would
also work:
  i = (ms == 0) ? 0 : ((long)t * 1000 / (long)ms);
 

>Audit-Trail:
>Unformatted:

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199812010710.XAA11274>