public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "dominiq at lps dot ens.fr" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/56463] infinite loop when having integer overflow in a simple accumulator
Date: Tue, 26 Feb 2013 18:42:00 -0000	[thread overview]
Message-ID: <bug-56463-4-fcxrCoXawI@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-56463-4@http.gcc.gnu.org/bugzilla/>


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56463

--- Comment #4 from Dominique d'Humieres <dominiq at lps dot ens.fr> 2013-02-26 18:41:56 UTC ---
> The compiler may assume that undefined behavior doesn't happen in the program.
> It is fine to have undefined behavior in code that will be never executed, but
> as soon as you hit it, the program can do anything.

IMO the anything should be based on the "quality of implementation".
The present behavior is

(1) inconsistent (see PR 54932), the following test

#include "stdio.h"
int main(void)
{
 int huge=2147483647;
 int i, j, k;
 int e;

 i=huge-10;
 j=0;
 k=0;
 do
   {
    j=j+1;
    k=k-1;
    e = (i == huge);
    i++;
    if (e) break;
   }
 while(1);
 printf("i=%d,  j=%d,  k=%d\n", i, j, k);

 return 0;
}

does not give an infinite loop, but skip the last iteration, while the
following test

#include "stdio.h"
int main(void)
{
 int huge=2147483647;
 int i, j, init;
 int e;

 init = 0;
 j = init;
 for(i=-(huge/2);i<=1+huge/2; i++)
   {
    e = (j == huge);
    j=j+1;
    if (e) break;
   }
 printf("i=%d,  j=%d\n", i, j);

 return 0;
}

gives an infinite loop when compiled with -O2 (but indeed works for any
init<0);

(2) easy to fool: PR 54932 again;

(3) nasty: an infinite loop can hardly be considered as an optimization and it
is done without warning.

And yes I know that a compiler can do anything with undefined behavior, even
issue a rm -rf * for the home directory (and there are standard fundamentalists
around who think that this is the only sensible behavior).


  parent reply	other threads:[~2013-02-26 18:42 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-26 16:06 [Bug c/56463] New: " nakacristo at hotmail dot com
2013-02-26 16:16 ` [Bug c/56463] " jakub at gcc dot gnu.org
2013-02-26 17:40 ` nakacristo at hotmail dot com
2013-02-26 17:54 ` jakub at gcc dot gnu.org
2013-02-26 18:42 ` dominiq at lps dot ens.fr [this message]
2013-02-27  8:19 ` nakacristo at hotmail dot com
2013-02-27  8:31 ` jakub at gcc dot gnu.org
2013-12-02 19:03 ` Joost.VandeVondele at mat dot ethz.ch
2013-12-02 19:04 ` Joost.VandeVondele at mat dot ethz.ch

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=bug-56463-4-fcxrCoXawI@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).