GNU bug report logs - #8732
uinttostr: comparison of unsigned expression < 0 is always false

Previous Next

Package: coreutils;

Reported by: "Voelker, Bernhard" <bernhard.voelker <at> siemens-enterprise.com>

Date: Wed, 25 May 2011 18:20:02 UTC

Severity: normal

Done: Eric Blake <eblake <at> redhat.com>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 8732 in the body.
You can then email your comments to 8732 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#8732; Package coreutils. (Wed, 25 May 2011 18:20:03 GMT) Full text and rfc822 format available.

Acknowledgement sent to "Voelker, Bernhard" <bernhard.voelker <at> siemens-enterprise.com>:
New bug report received and forwarded. Copy sent to bug-coreutils <at> gnu.org. (Wed, 25 May 2011 18:20:03 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: "Voelker, Bernhard" <bernhard.voelker <at> siemens-enterprise.com>
To: "bug-coreutils <at> gnu.org" <bug-coreutils <at> gnu.org>
Subject: uinttostr: comparison of unsigned expression < 0 is always false
Date: Wed, 25 May 2011 09:23:50 +0200
Hi *,

building coreutils-8.12 with '--enable-gcc-warnings' fails on my SLES 10.3 server:

  CC       uinttostr.o
cc1: warnings being treated as errors
In file included from uinttostr.c:3:
anytostr.c: In function 'uinttostr':
anytostr.c:39: warning: comparison of unsigned expression < 0 is always false

gcc --version
gcc (GCC) 4.1.2 20070115 (SUSE Linux)

Have a nice day,
Berny



Information forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#8732; Package coreutils. (Wed, 25 May 2011 19:56:01 GMT) Full text and rfc822 format available.

Message #8 received at 8732 <at> debbugs.gnu.org (full text, mbox):

From: Eric Blake <eblake <at> redhat.com>
To: "Voelker, Bernhard" <bernhard.voelker <at> siemens-enterprise.com>
Cc: 8732 <at> debbugs.gnu.org
Subject: Re: bug#8732: uinttostr: comparison of unsigned expression < 0 is
	always	false
Date: Wed, 25 May 2011 13:54:54 -0600
[Message part 1 (text/plain, inline)]
On 05/25/2011 01:23 AM, Voelker, Bernhard wrote:
> Hi *,
> 
> building coreutils-8.12 with '--enable-gcc-warnings' fails on my SLES 10.3 server:
> 
>   CC       uinttostr.o
> cc1: warnings being treated as errors
> In file included from uinttostr.c:3:
> anytostr.c: In function 'uinttostr':
> anytostr.c:39: warning: comparison of unsigned expression < 0 is always false

It would be _really_ nice if there were a way to have gcc shut up that
particular warning when it is the result of a macro expansion that first
checks whether an expresssion is signed or unsigned.  That is, gcc is
issuing the warning for 'is_signed(e) ? (e < 0 ? a : b) : b', even
though the 'e < 0' sub-expression is provably dead code for an unsigned
expression and therefore the warning is spurious.  But we don't know how
to shut up gcc.

This also affects recent gnulib lib/intprops.h, so ideas are welcome.

In the meantime, configure with CFLAGS='-Wno-error' to allow warnings to
still be listed but not halt compilation.

-- 
Eric Blake   eblake <at> redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

[signature.asc (application/pgp-signature, attachment)]

Reply sent to Eric Blake <eblake <at> redhat.com>:
You have taken responsibility. (Wed, 25 May 2011 21:30:04 GMT) Full text and rfc822 format available.

Notification sent to "Voelker, Bernhard" <bernhard.voelker <at> siemens-enterprise.com>:
bug acknowledged by developer. (Wed, 25 May 2011 21:30:04 GMT) Full text and rfc822 format available.

Message #13 received at 8732-done <at> debbugs.gnu.org (full text, mbox):

From: Eric Blake <eblake <at> redhat.com>
Cc: 8732-done <at> debbugs.gnu.org, "Voelker,
	Bernhard" <bernhard.voelker <at> siemens-enterprise.com>
Subject: Re: bug#8732: uinttostr: comparison of unsigned expression < 0 is
	always	false
Date: Wed, 25 May 2011 15:29:12 -0600
[Message part 1 (text/plain, inline)]
tag 8732 wontfix
close 8732
done


On 05/25/2011 01:54 PM, Eric Blake wrote:
> On 05/25/2011 01:23 AM, Voelker, Bernhard wrote:
>> Hi *,
>>
>> building coreutils-8.12 with '--enable-gcc-warnings' fails on my SLES 10.3 server:
>>
>>   CC       uinttostr.o
>> cc1: warnings being treated as errors
>> In file included from uinttostr.c:3:
>> anytostr.c: In function 'uinttostr':
>> anytostr.c:39: warning: comparison of unsigned expression < 0 is always false
> 
> the warning is spurious.  But we don't know how
> to shut up gcc.

You're not the first to hit this, either:

http://lists.gnu.org/archive/html/bug-gnulib/2010-10/msg00435.html

Since our party line back then was to "upgrade gcc or quit using -Werror
with old gcc", I'm marking this as a wontfix; but if you can come up
with a patch, we can reopen it.

-- 
Eric Blake   eblake <at> redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

[signature.asc (application/pgp-signature, attachment)]

Information forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#8732; Package coreutils. (Wed, 25 May 2011 21:49:01 GMT) Full text and rfc822 format available.

Message #16 received at 8732 <at> debbugs.gnu.org (full text, mbox):

From: Eric Blake <eblake <at> redhat.com>
To: 8732 <at> debbugs.gnu.org, bug-gnulib <bug-gnulib <at> gnu.org>
Subject: Re: bug#8732: uinttostr: comparison of unsigned expression < 0 is
	always	false
Date: Wed, 25 May 2011 15:48:19 -0600
[Message part 1 (text/plain, inline)]
[adding bug-gnulib]

On 05/25/2011 03:29 PM, Eric Blake wrote:
>>> building coreutils-8.12 with '--enable-gcc-warnings' fails on my SLES 10.3 server:
>>>
>>>   CC       uinttostr.o
>>> cc1: warnings being treated as errors
>>> In file included from uinttostr.c:3:
>>> anytostr.c: In function 'uinttostr':
>>> anytostr.c:39: warning: comparison of unsigned expression < 0 is always false
>>
>> the warning is spurious.  But we don't know how
>> to shut up gcc.
> 
> You're not the first to hit this, either:
> 
> http://lists.gnu.org/archive/html/bug-gnulib/2010-10/msg00435.html
> 
> Since our party line back then was to "upgrade gcc or quit using -Werror
> with old gcc", I'm marking this as a wontfix; but if you can come up
> with a patch, we can reopen it.

Jim, Paul,

This patch silences the gcc warning even for gcc too old to honor the
pragma, but I can't help but feel that it might be too gross (it makes
functions like uinttostr larger in size).  What do you think?

diff --git i/lib/anytostr.c w/lib/anytostr.c
index e23746a..da57259 100644
--- i/lib/anytostr.c
+++ w/lib/anytostr.c
@@ -17,11 +17,6 @@

 /* Written by Paul Eggert */

-/* Tell gcc not to warn about the (i < 0) test, below.  */
-#if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
-# pragma GCC diagnostic ignored "-Wtype-limits"
-#endif
-
 #include <config.h>

 #include "inttostr.h"
@@ -34,15 +29,17 @@ char * __attribute_warn_unused_result__
 anytostr (inttype i, char *buf)
 {
   char *p = buf + INT_STRLEN_BOUND (inttype);
+  inttype ii = i;
   *p = 0;

-  if (i < 0)
+  if (i <= 0)
     {
       do
         *--p = '0' - i % 10;
       while ((i /= 10) != 0);

-      *--p = '-';
+      if (ii)
+        *--p = '-';
     }
   else
     {




The same trick of swapping '< 0' to '<= 0' to foil -Wtype-limits can be
used to reduce (but not eliminate) some of the other warnings generated
by intprops.h.  For example, this one's completely safe, and knocks out
a bunch of the warnings on test-intprops.c if you temporarily revert
9d196fad:

diff --git i/lib/intprops.h w/lib/intprops.h
index d722648..c2e6bf8 100644
--- i/lib/intprops.h
+++ w/lib/intprops.h
@@ -54,7 +54,7 @@

 /* Return 1 if the integer expression E, after integer promotion, has
    a signed type.  E should not have side effects.  */
-#define _GL_INT_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
+#define _GL_INT_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) <= 0)


 /* Minimum and maximum values for integer types and expressions.  These


-- 
Eric Blake   eblake <at> redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

[signature.asc (application/pgp-signature, attachment)]

Information forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#8732; Package coreutils. (Wed, 25 May 2011 22:22:02 GMT) Full text and rfc822 format available.

Message #19 received at 8732 <at> debbugs.gnu.org (full text, mbox):

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Eric Blake <eblake <at> redhat.com>
Cc: bug-gnulib <bug-gnulib <at> gnu.org>, 8732 <at> debbugs.gnu.org
Subject: Re: bug#8732: uinttostr: comparison of unsigned expression < 0 is
	always	false
Date: Wed, 25 May 2011 15:21:18 -0700
On 05/25/11 14:48, Eric Blake wrote:
> This patch silences the gcc warning even for gcc too old to honor the
> pragma, but I can't help but feel that it might be too gross (it makes
> functions like uinttostr larger in size).
> ...
> -      *--p = '-';
> +      if (ii)
> +        *--p = '-';

Yes, it's too gross.  Once we start down that path we'll have a big
job of making subtle changes to code that make it worse
and are put in only to pacify GCC.  Compilers should be our servants,
not our masters, and in this case GCC serves us better by being quiet
about a matter that it doesn't grok.

> -#define _GL_INT_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
> +#define _GL_INT_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) <= 0)

This change I don't mind so much, as any compiler that can't optimize
0 * e - 1 <= 0 is unlikely to optimize 0 * e - 1 < 0 either.  It'd
need a comment.  If this sort of thing silences all the warnings it
might be worth doing.




Message #20 received at 8732-done <at> debbugs.gnu.org (full text, mbox):

From: Jim Meyering <jim <at> meyering.net>
To: Eric Blake <eblake <at> redhat.com>
Cc: 8732-done <at> debbugs.gnu.org, bug-gnulib <bug-gnulib <at> gnu.org>
Subject: Re: bug#8732: uinttostr: comparison of unsigned expression < 0 is
	always	false
Date: Thu, 26 May 2011 10:34:53 +0200
Eric Blake wrote:
> [adding bug-gnulib]
>
> On 05/25/2011 03:29 PM, Eric Blake wrote:
>>>> building coreutils-8.12 with '--enable-gcc-warnings' fails on my
>>>> SLES 10.3 server:
>>>>
>>>>   CC       uinttostr.o
>>>> cc1: warnings being treated as errors
>>>> In file included from uinttostr.c:3:
>>>> anytostr.c: In function 'uinttostr':
>>>> anytostr.c:39: warning: comparison of unsigned expression < 0 is always false
>>>
>>> the warning is spurious.  But we don't know how
>>> to shut up gcc.
>>
>> You're not the first to hit this, either:
>>
>> http://lists.gnu.org/archive/html/bug-gnulib/2010-10/msg00435.html
>>
>> Since our party line back then was to "upgrade gcc or quit using -Werror
>> with old gcc", I'm marking this as a wontfix; but if you can come up
>> with a patch, we can reopen it.
>
> Jim, Paul,
>
> This patch silences the gcc warning even for gcc too old to honor the
> pragma, but I can't help but feel that it might be too gross (it makes
> functions like uinttostr larger in size).  What do you think?

Hi Eric,
Thanks for investigating, but

SLES 10.3's gcc is too old for --enable-gcc-warnings.

Coreutils' documentation (this originated as a report against
coreutils) is clear that you should use the --enable-gcc-warnings
option only with a "very recent version of gcc":
[from README-hacking]

    [*] The --enable-gcc-warnings option is useful only with glibc
    and with a very recent version of gcc.  You'll probably also have
    to use recent system headers.  If you configure with this option,
    and spot a problem, please be sure to send the report to the bug
    reporting address of this package, and not to that of gnulib, even
    if the problem seems to originate in a gnulib-provided file.

The solution is not to enable those warnings on anything but
"new enough" gcc+glibc.

I've closed the coreutils ticket.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Thu, 23 Jun 2011 11:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 14 years and 52 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.