Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 11 May 1997 02:20:02 -0700 (PDT)
From:      Bruce Evans <bde@zeta.org.au>
To:        freebsd-bugs
Subject:   Re: misc/3575: Eliminate spurious warning in /usr/src/lib/libc/stdlib/strtoq.c
Message-ID:  <199705110920.CAA19857@hub.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR misc/3575; it has been noted by GNATS.

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



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