From krw@tcn.net  Sat May 10 11:28:49 1997
Received: from Pkrw.tcn.net (Pkrw.tcn.net [199.166.4.58])
          by hub.freebsd.org (8.8.5/8.8.5) with ESMTP id LAA15309
          for <FreeBSD-gnats-submit@freebsd.org>; Sat, 10 May 1997 11:28:45 -0700 (PDT)
Received: (from krw@localhost)
	by Pkrw.tcn.net (8.8.5/8.8.5) id OAA00445;
	Sat, 10 May 1997 14:28:18 -0400 (EDT)
Message-Id: <199705101828.OAA00445@Pkrw.tcn.net>
Date: Sat, 10 May 1997 14:28:18 -0400 (EDT)
From: "Kenneth R. Westerback" <krw@tcn.net>
Reply-To: krw@tcn.net
To: FreeBSD-gnats-submit@freebsd.org
Subject: Eliminate spurious warning in /usr/src/lib/libc/stdlib/strtoq.c
X-Send-Pr-Version: 3.2

>Number:         3575
>Category:       misc
>Synopsis:       compilation of strtoq.c produces unnecessary warning
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    steve
>State:          closed
>Quarter:
>Keywords:
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat May 10 11:30:01 PDT 1997
>Closed-Date:    Sat Aug 23 16:51:46 PDT 1997
>Last-Modified:  Sat Aug 23 16:53:10 PDT 1997
>Originator:     Kenneth R. Westerback
>Release:        FreeBSD 2.2-STABLE i386
>Organization:
>Environment:

	FreeBSD 2-2-STABLE make world'ed through src-2.2.0217

>Description:

	During a make world the compilation of 
	
	/usr/src/lib/libc/stdlib/strtoq.c

	produces a warning message


	/usr/src/lib/libc/stdlib/strtoq.c:108: warning: integer overflow in expression

>How-To-Repeat:

	make world or just compile strtoq.c

>Fix:
	
	Apply following one line patch:

--- /usr/src/lib/libc/stdlib/strtoq.c	Sat May 10 11:55:38 1997
+++ strtoq.c	Sat May 10 14:10:02 1997
@@ -105,7 +105,7 @@
 	 * overflow.
 	 */
 	qbase = (unsigned)base;
-	cutoff = neg ? -(u_quad_t)QUAD_MIN : QUAD_MAX;
+	cutoff = (u_quad_t)(neg ? QUAD_MIN : QUAD_MAX);
 	cutlim = cutoff % qbase;
 	cutoff /= qbase;
 	for (acc = 0, any = 0;; c = *s++) {

>Release-Note:
>Audit-Trail:

From: Bruce Evans <bde@zeta.org.au>
To: FreeBSD-gnats-submit@freebsd.org, krw@tcn.net
Cc:  Subject: Re: misc/3575: Eliminate spurious warning in /usr/src/lib/libc/stdlib/strtoq.c
Date: Sun, 11 May 1997 19:13:09 +1000

 >	During a make world the compilation of 
 >	
 >	/usr/src/lib/libc/stdlib/strtoq.c
 >
 >	produces a warning message
 >
 >
 >	/usr/src/lib/libc/stdlib/strtoq.c:108: warning: integer overflow in expression
 
 This warning is a compiler bug (there is no overflow for unsigned types)
 and isn't easy to fix.  Note that there is no warning for corresponding
 operation in strtol.c (the warning is for negating (u_quad_t)QUAD_MIN).
 
 >--- /usr/src/lib/libc/stdlib/strtoq.c	Sat May 10 11:55:38 1997
 >+++ strtoq.c	Sat May 10 14:10:02 1997
 >@@ -105,7 +105,7 @@
 > 	 * overflow.
 > 	 */
 > 	qbase = (unsigned)base;
 >-	cutoff = neg ? -(u_quad_t)QUAD_MIN : QUAD_MAX;
 >+	cutoff = (u_quad_t)(neg ? QUAD_MIN : QUAD_MAX);
 > 	cutlim = cutoff % qbase;
 > 	cutoff /= qbase;
 > 	for (acc = 0, any = 0;; c = *s++) {
 
 This change would unimprove portabilty.  E.g., for 64 bit 1's
 complement, QUAD_MIN is -0x7fffffffffffffffLL (which is represented as
 0x8000000000000000).  Conversion to u_quad_t gives 0x8000000000000001ULL.
 This must be negated (as in the original version) to give the correct
 cutoff of 0x7fffffffffffffffULL.
 
 For 1's complement the cutoff for the `neg' case can be expressed more
 naturally as (u_quad_t)(-QUAD_MIN).  This fails for 2's complement due
 to overflow.  I think the following expression works for 1's complement
 and 2's complement and avoids the compiler bug:
 
 	cutoff = neg ? (u_quad_t)-(QUAD_MIN + 1) + 1 : QUAD_MAX;
 
 This depends on QUAD_MIN not being too different from -QUAD_MAX.  It can
 be improved to:
 
 	cutoff = neg ? (u_quad_t)-(QUAD_MIN + QUAD_MAX) + QUAD_MAX : QUAD_MAX;
 
 I think fixing the compiler would be easier :-).
 
 Bruce
State-Changed-From-To: open->closed 
State-Changed-By: steve 
State-Changed-When: Sat Aug 23 16:51:46 PDT 1997 
State-Changed-Why:  
Bruce Evans' suggested fix (well the one that didn't 
require fixing the compiler :) applied in revision 1.4 
of strtoq.c. 
>Unformatted:
