Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #19950 > unrolled thread

(un)signed warning

Started byGuillaume Dargaud <use_the_contact_form@www.gdargaud.net>
First post2012-04-26 11:17 +0200
Last post2012-04-28 04:23 -0700
Articles 8 — 5 participants

Back to article view | Back to comp.lang.c


Contents

  (un)signed warning Guillaume Dargaud <use_the_contact_form@www.gdargaud.net> - 2012-04-26 11:17 +0200
    Re: (un)signed warning James Kuyper <jameskuyper@verizon.net> - 2012-04-26 06:47 -0400
      Re: (un)signed warning Guillaume Dargaud <use_the_contact_form@www.gdargaud.net> - 2012-04-26 16:45 +0200
        Re: (un)signed warning James Kuyper <jameskuyper@verizon.net> - 2012-04-26 11:39 -0400
        Re: (un)signed warning Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-04-26 09:35 -0700
          Re: (un)signed warning Fred K <fred.l.kleinschmidt@gmail.com> - 2012-04-26 11:19 -0700
            Re: (un)signed warning ImpalerCore <jadill33@gmail.com> - 2012-04-26 12:01 -0700
            Re: (un)signed warning Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-04-28 04:23 -0700

#19950 — (un)signed warning

FromGuillaume Dargaud <use_the_contact_form@www.gdargaud.net>
Date2012-04-26 11:17 +0200
Subject(un)signed warning
Message-ID<jnb3rm$kcn$1@ccpntc8.in2p3.fr>
Hello all,
can someone please explain this warning to me:

#define GPS_MAX_LEN 300
unsigned char Str[GPS_MAX_LEN]="@@";
int n, RL;

RL=SomeValue();
n = read(fd, Str, GPS_MAX_LEN);
if (n>=GPS_MAX_LEN)  printf(...);
else if (n<0)	printf(...);
else if (n==0) printf(...);
else if (n<RL) printf(...);
else if (Str[0]!='@' or Str[1]!='@') printf(...);
else printf(...);

The line (n<RL) gives me the following warning:
warning: assuming signed overflow does not occur when assuming that (X - c) 
> X is always false [-Wstrict-overflow]

Thanks.
-- 
Guillaume Dargaud
http://www.gdargaud.net/

[toc] | [next] | [standalone]


#19952

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-04-26 06:47 -0400
Message-ID<jnb939$b32$1@dont-email.me>
In reply to#19950
On 04/26/2012 05:17 AM, Guillaume Dargaud wrote:
> Hello all,
> can someone please explain this warning to me:
> 
> #define GPS_MAX_LEN 300
> unsigned char Str[GPS_MAX_LEN]="@@";
> int n, RL;
> 
> RL=SomeValue();
> n = read(fd, Str, GPS_MAX_LEN);
> if (n>=GPS_MAX_LEN)  printf(...);
> else if (n<0)	printf(...);
> else if (n==0) printf(...);
> else if (n<RL) printf(...);
> else if (Str[0]!='@' or Str[1]!='@') printf(...);
> else printf(...);
> 
> The line (n<RL) gives me the following warning:
> warning: assuming signed overflow does not occur when assuming that (X - c) 
>> X is always false [-Wstrict-overflow]

I filled in the details of your code to the minimal extent needed to
produce a compilable file, using -fstrict-overflow and
-Wstrict-overflow=4, without duplicating your warning message. This
means that something that you didn't tell us is responsible for that
message.

Please create a version of your code that has been modified to be as
small and simple as possible, while still demonstrating the problem.
Confirm that it still demonstrates the problem! Then cut and paste the
full exact text of your source code into the next message you send us.
Also cut-and-paste the exact command line that you used to compile the
code, and the full output of compiler when you issued that command.
Don't re-type anything - retyping often introduces errors that are
different from the ones that you were asking about.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#19961

FromGuillaume Dargaud <use_the_contact_form@www.gdargaud.net>
Date2012-04-26 16:45 +0200
Message-ID<jnbn1t$73g$1@ccpntc8.in2p3.fr>
In reply to#19952
> Please create a version of your code that has been modified to be as
> small and simple as possible, while still demonstrating the problem.
> Confirm that it still demonstrates the problem! Then cut and paste the
> full exact text of your source code into the next message you send us.
> Also cut-and-paste the exact command line that you used to compile the
> code, and the full output of compiler when you issued that command.
> Don't re-type anything - retyping often introduces errors that are
> different from the ones that you were asking about.

Sorry about that.
I understood the issue when simplifying the code:

#include <stdio.h>
#include <stdlib.h>

#define GPS_MAX_LEN 300

int GpsResponseLength(void) { return rand(); }

int main(void) {
int n, RL;

n=RL=GpsResponseLength()-4;
n+=4;
	
if (n>=GPS_MAX_LEN)  printf("Serial answer too long");
else if (n<0)	printf("Serial error");
else if (n==0) printf("No reply");
else if (n<RL) printf("Reply too short");
else 	printf("Read %d chars", n);

return n;
}

$ gcc -Wall -O2 TestWarn.c
TestWarn.c: In function ‘main’:
TestWarn.c:20:10: warning: assuming signed overflow does not occur when 
assuming that (X - c) > X is always false [-Wstrict-overflow]

But no warning in -O1 or -O0
That's a useless 'if' branch, removed by the optimizer.
It's not a but per-se on my part but a remnant of various ways to read the 
serial data (at once, in several shots, etc), selected by various #if, so I 
needed this test in the past.
-- 
Guillaume Dargaud
http://www.gdargaud.net/

[toc] | [prev] | [next] | [standalone]


#19965

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-04-26 11:39 -0400
Message-ID<4F996C2F.4050007@verizon.net>
In reply to#19961
On 04/26/2012 10:45 AM, Guillaume Dargaud wrote:
>> Please create a version of your code that has been modified to be as
>> small and simple as possible, while still demonstrating the problem.
>> Confirm that it still demonstrates the problem! Then cut and paste the
>> full exact text of your source code into the next message you send us.
>> Also cut-and-paste the exact command line that you used to compile the
>> code, and the full output of compiler when you issued that command.
>> Don't re-type anything - retyping often introduces errors that are
>> different from the ones that you were asking about.
> 
> Sorry about that.

I've given out essentially the same advice to many different people over
the years; I can't remember the last time anyone actually followed it.
Thank you for making me feel like someone was actually listening.

> I understood the issue when simplifying the code:

In my experience, that is very commonly the case.

...
> n=RL=GpsResponseLength()-4;
> n+=4;

That is VERY different from what you wrote in your original message:

> n = read(fd, Str, GPS_MAX_LEN);

I can see the reason for the warning in this version of the code. I'm
curious about what your original code looked like, that would cause you
to summarize it the way that you did in your first message, yet still
generated this warning message? I think it must have been a very poor
summary.

...
> else if (n<RL) printf("Reply too short");
...

> TestWarn.c:20:10: warning: assuming signed overflow does not occur when 
> assuming that (X - c) > X is always false [-Wstrict-overflow]
> 
> But no warning in -O1 or -O0

When you set the optimization level for gcc high enough, it performs
certain analyses of the code in support of those optimizations, and it
gives you warnings about some things it finds during that analysis that
it doesn't even notice at lower optimization levels.

[toc] | [prev] | [next] | [standalone]


#19967

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2012-04-26 09:35 -0700
Message-ID<13669218.840.1335458136446.JavaMail.geo-discussion-forums@ynej8>
In reply to#19961
בתאריך יום חמישי, 26 באפריל 2012 15:45:16 UTC+1, מאת Guillaume Dargaud:
> 
> int n, RL;
> 
> n=RL=GpsResponseLength()-4;
> n+=4;
> 	
> else if (n<RL) printf("Reply too short");
>
n must always be larget than RL, unless an arithemtic overflow occurs. Since n and Rl are signed, this generates undefined behaviour (the program could crash out with an arithemtic overflow message, or it could set N to a very high negative number, or, as in this case, it could simply optimise out the comparison to RL and assume that n is bigger than it). The compiler is warning you that it has taken the last option, which might not be what you want, since the redundant test might well be a erroneous test for overflow. It can't read your mind.
-- 
Read my website. Lots of C programming resources.
http://www.malcommclean.site11.com/www
  

[toc] | [prev] | [next] | [standalone]


#19971

FromFred K <fred.l.kleinschmidt@gmail.com>
Date2012-04-26 11:19 -0700
Message-ID<15395227.5.1335464381460.JavaMail.geo-discussion-forums@pbcgx8>
In reply to#19967
On Thursday, April 26, 2012 9:35:36 AM UTC-7, Malcolm McLean wrote:
> בתאריך יום חמישי, 26 באפריל 2012 15:45:16 UTC+1, מאת Guillaume Dargaud:
> > 
> > int n, RL;
> > 
> > n=RL=GpsResponseLength()-4;
> > n+=4;
> > 	
> > else if (n<RL) printf("Reply too short");
> >
> n must always be larget than RL, unless an arithemtic overflow occurs. Since n and Rl are signed, this generates undefined behaviour (the program could crash out with an arithemtic overflow message, or it could set N to a very high negative number, or, as in this case, it could simply optimise out the comparison to RL and assume that n is bigger than it). The compiler is warning you that it has taken the last option, which might not be what you want, since the redundant test might well be a erroneous test for overflow. It can't read your mind.
> -- 
> Read my website. Lots of C programming resources.
> http://www.malcommclean.site11.com/www

What C programming resources? That site seems to be an ad for "free" web hosting.

[toc] | [prev] | [next] | [standalone]


#19973

FromImpalerCore <jadill33@gmail.com>
Date2012-04-26 12:01 -0700
Message-ID<28e5d94b-b2e9-4c99-b1e2-e740f1250782@q13g2000vbd.googlegroups.com>
In reply to#19971
On Apr 26, 2:19 pm, Fred K <fred.l.kleinschm...@gmail.com> wrote:
> On Thursday, April 26, 2012 9:35:36 AM UTC-7, Malcolm McLean wrote:
> > בתאריך יום חמישי, 26 באפריל 2012 15:45:16 UTC+1, מאת Guillaume Dargaud:
>
> > > int n, RL;
>
> > > n=RL=GpsResponseLength()-4;
> > > n+=4;
>
> > > else if (n<RL) printf("Reply too short");
>
> > n must always be larget than RL, unless an arithemtic overflow occurs. Since n and Rl are signed, this generates undefined behaviour (the program could crash out with an arithemtic overflow message, or it could set N to a very high negative number, or, as in this case, it could simply optimise out the comparison to RL and assume that n is bigger than it). The compiler is warning you that it has taken the last option, which might not be what you want, since the redundant test might well be a erroneous test for overflow. It can't read your mind.
> > --
> > Read my website. Lots of C programming resources.
> >http://www.malcommclean.site11.com/www
>
> What C programming resources? That site seems to be an ad for "free" web hosting.

I get the same ad when I click on his link.  I had to copy/paste the
address to get to the site.

http://www.malcolmmclean.site11.com/www/

Without the last '/', it appears that it goes to the ad.

[toc] | [prev] | [next] | [standalone]


#20024

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2012-04-28 04:23 -0700
Message-ID<32134849.778.1335612236926.JavaMail.geo-discussion-forums@vbki8>
In reply to#19971
בתאריך יום חמישי, 26 באפריל 2012 19:19:41 UTC+1, מאת Fred K:
> > > -- 
> > Read my website. Lots of C programming resources.
> > http://www.malcommclean.site11.com/www
> 
> What C programming resources? That site seems to be an ad for "free" web hosting.
>
That's a nuisance. The link should point to my personal website, which has the C programming material. I managed to misspell my own name, so it reverted to the hoster. (They are very good and genuinely free, but there's no intention to spam the ng on their behalf).
-- 
Try this
http://www.malcolmmclean.site11.com/www/index.html

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web