public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jeffrey A Law <law@hurl.cygnus.com>
To: Charles G Waldman <cgw@alum.mit.edu>
Cc: egcs@egcs.cygnus.com
Subject: Re: [PATCH] Fix for short-enums comparison bug
Date: Wed, 10 Feb 1999 23:47:00 -0000	[thread overview]
Message-ID: <4691.918719200@hurl.cygnus.com> (raw)
In-Reply-To: Your message of 04 Feb 1999 14:11:21 MST.            <x490ee6l2e.fsf@janus.pgt.com>

  In message <x490ee6l2e.fsf@janus.pgt.com>you write:
  > The following message is a courtesy copy of an article
  > that has been posted to gnu.gcc,gnu.gcc.bug as well.
Thanks.


  > I provide below a one-line patch which fixes the bug; however I'd like
  > to provide a little bit of explanation about the nature of the bug and
  > how I found it.  My apologies if this is a bit long.
No need to apologize, it is precisely the kind of analysis that makes my
job of reviewing patches a whole lot easier :-)

  > Here's a very simple example which illustrates the bug in question:
  > 
  > typedef enum {A=0, B, C, D} T;
  > main(){
  >   T x;
  >   for (x=A; x<=D; ++x)
  >     printf("%d ", (int)x);
  >   putchar('\n');
  > }
  > 
  > If you compile this with no compiler flags set and run it will print 
  > 
  > 0 1 2 3  
  > 
  > as expected.  If you compile with -fshort-enums it will loop
  > indefinitely, printing all values from 0-255 over and over.
I'm not a language lawyer -- we really need one to determine what the behavior
of that code should be.

I don't have an ANSI C standard here, nor do I have a copy of the ISO C9X
standard.  Can someone who does dig into them and determine what happens for
an assignment out of the range of an enumerated type and what assumptions the
compiler can/can not make about the values an enumerated variable can have?


  > Try running this with and without the -fshort-enums flag.  If the
  > intent of -fshort-enums is to really restrict the value of an
  > enumerated type to lie within the min and max of that type, a warning
  > should be produced when we set x equal to 10.  But no such warning is
  > produced, just bizarre results (the program prints 10<=3 if
  > -fshort-enums is used).
Actually, whether or not a warning must be produced is a standards issue.  Even
if the standards don't demand it, a warning would be nice.  Someone would have
to grovel around the front-end though to produce the warning.  Once we've
left the language front end it's too late.


Jeff

WARNING: multiple messages have this Message-ID
From: Jeffrey A Law <law@hurl.cygnus.com>
To: Charles G Waldman <cgw@alum.mit.edu>
Cc: egcs@egcs.cygnus.com
Subject: Re: [PATCH] Fix for short-enums comparison bug
Date: Sun, 28 Feb 1999 22:53:00 -0000	[thread overview]
Message-ID: <4691.918719200@hurl.cygnus.com> (raw)
Message-ID: <19990228225300.E1AuNb8a3RZGuGYljqELvpHy4oMoGuO-fJxwjgQRPJQ@z> (raw)
In-Reply-To: <x490ee6l2e.fsf@janus.pgt.com>

  In message <x490ee6l2e.fsf@janus.pgt.com>you write:
  > The following message is a courtesy copy of an article
  > that has been posted to gnu.gcc,gnu.gcc.bug as well.
Thanks.


  > I provide below a one-line patch which fixes the bug; however I'd like
  > to provide a little bit of explanation about the nature of the bug and
  > how I found it.  My apologies if this is a bit long.
No need to apologize, it is precisely the kind of analysis that makes my
job of reviewing patches a whole lot easier :-)

  > Here's a very simple example which illustrates the bug in question:
  > 
  > typedef enum {A=0, B, C, D} T;
  > main(){
  >   T x;
  >   for (x=A; x<=D; ++x)
  >     printf("%d ", (int)x);
  >   putchar('\n');
  > }
  > 
  > If you compile this with no compiler flags set and run it will print 
  > 
  > 0 1 2 3  
  > 
  > as expected.  If you compile with -fshort-enums it will loop
  > indefinitely, printing all values from 0-255 over and over.
I'm not a language lawyer -- we really need one to determine what the behavior
of that code should be.

I don't have an ANSI C standard here, nor do I have a copy of the ISO C9X
standard.  Can someone who does dig into them and determine what happens for
an assignment out of the range of an enumerated type and what assumptions the
compiler can/can not make about the values an enumerated variable can have?


  > Try running this with and without the -fshort-enums flag.  If the
  > intent of -fshort-enums is to really restrict the value of an
  > enumerated type to lie within the min and max of that type, a warning
  > should be produced when we set x equal to 10.  But no such warning is
  > produced, just bizarre results (the program prints 10<=3 if
  > -fshort-enums is used).
Actually, whether or not a warning must be produced is a standards issue.  Even
if the standards don't demand it, a warning would be nice.  Someone would have
to grovel around the front-end though to produce the warning.  Once we've
left the language front end it's too late.


Jeff

       reply	other threads:[~1999-02-10 23:47 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <x490ee6l2e.fsf@janus.pgt.com>
1999-02-10 23:47 ` Jeffrey A Law [this message]
     [not found]   ` < 4691.918719200@hurl.cygnus.com >
1999-02-11  6:55     ` Gavin Romig-Koch
     [not found]       ` < 14018.60839.325601.426391@cetus.cygnus.com >
1999-02-11  7:25         ` Joern Rennecke
1999-02-28 22:53           ` Joern Rennecke
1999-02-11  8:12         ` Charles G Waldman
1999-02-28 22:53           ` Charles G Waldman
1999-02-11  9:33         ` Jeffrey A Law
     [not found]           ` < 5858.918754318@hurl.cygnus.com >
1999-02-11 11:40             ` Gavin Romig-Koch
1999-02-28 22:53               ` Gavin Romig-Koch
1999-02-11 11:44             ` Joe Buck
     [not found]               ` < 199902111942.LAA16560@atrus.synopsys.com >
1999-02-11 11:55                 ` Jeffrey A Law
     [not found]                   ` < 6271.918762821@hurl.cygnus.com >
1999-02-11 12:07                     ` Joe Buck
1999-02-28 22:53                       ` Joe Buck
1999-02-11 14:16                     ` Tim Hollebeek
     [not found]                       ` < 199902112216.RAA27319@wagner.Princeton.EDU >
1999-02-11 14:19                         ` Jeffrey A Law
     [not found]                           ` < 6754.918771465@hurl.cygnus.com >
1999-02-11 17:03                             ` Joe Buck
1999-02-28 22:53                               ` Joe Buck
1999-02-28 22:53                           ` Jeffrey A Law
1999-02-28 22:53                       ` Tim Hollebeek
1999-02-28 22:53                   ` Jeffrey A Law
1999-02-28 22:53               ` Joe Buck
1999-02-28 22:53           ` Jeffrey A Law
1999-02-28 22:53       ` Gavin Romig-Koch
1999-02-28 22:53   ` Jeffrey A Law
1999-02-11 12:12 Mike Stump
1999-02-28 22:53 ` Mike Stump

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4691.918719200@hurl.cygnus.com \
    --to=law@hurl.cygnus.com \
    --cc=cgw@alum.mit.edu \
    --cc=egcs@egcs.cygnus.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).