From waterman@ricochet.net  Mon Nov 30 23:07:07 1998
Received: from rgate2.ricochet.net (rgate2.ricochet.net [204.179.143.3])
          by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id XAA23141
          for <FreeBSD-gnats-submit@freebsd.org>; Mon, 30 Nov 1998 23:07:05 -0800 (PST)
          (envelope-from waterman@ricochet.net)
Received: from home (mg137-019.ricochet.net [204.179.137.19])
	by rgate2.ricochet.net (8.8.8/8.8.8) with ESMTP id BAA26118
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 1 Dec 1998 01:06:47 -0600 (CST)
Received: (from waterman@localhost)
	by home (8.8.8/8.8.8) id XAA11274;
	Mon, 30 Nov 1998 23:10:50 -0800 (PST)
	(envelope-from waterman)
Message-Id: <199812010710.XAA11274@home>
Date: Mon, 30 Nov 1998 23:10:50 -0800 (PST)
From: waterman@acm.org
Reply-To: waterman@acm.org
To: FreeBSD-gnats-submit@freebsd.org
Subject: negative time values for csh 'time' built-in
X-Send-Pr-Version: 3.2

>Number:         8913
>Category:       bin
>Synopsis:       negative time values for csh 'time' built-in
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Nov 30 23:10:00 PST 1998
>Closed-Date:    Thu Aug 2 12:27:23 PDT 2001
>Last-Modified:  Thu Aug 02 12:27:54 PDT 2001
>Originator:     TS Waterman
>Release:        FreeBSD 2.2.7-STABLE i386
>Organization:
PricewaterhouseCoopers
>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);
 

>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: jon 
State-Changed-When: Thu Aug 2 12:27:23 PDT 2001 
State-Changed-Why:  
csh is no longer used, and tcsh does not exhibit this problem. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=8913 
>Unformatted:
