public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jeffrey Walton <noloader@gmail.com>
To: Agner Fog <agner@agner.org>
Cc: gcc-help@gcc.gnu.org
Subject: Re: Is it OK that gcc optimizes away overflow check?
Date: Sat, 23 Jul 2011 21:06:00 -0000	[thread overview]
Message-ID: <CAH8yC8=cwCuoP5bh8dPVUA12KwpqT2-OobhRGt6XD+BzA83pTQ@mail.gmail.com> (raw)
In-Reply-To: <4E2B2B72.9050504@agner.org>

On Sat, Jul 23, 2011 at 4:13 PM, Agner Fog <agner@agner.org> wrote:
> I have a program where I check for integer overflow. The program failed, and
> I found that gcc has optimized away the overflow check. I filed a bug report
> and got the answer:
>>
>> Integer overflow is undefined. You have to check before the fact, or
>> compile
>> >  with -fwrapv.
>
> ( http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49820 )
>
> I disagree for several reasons:
>
> 1). It is often easier and more logical to check for overflow after it
> happens than before. It can be quite complicated to write a code that
> predicts an overflow before it happens, in a portable way that works with
> all integer sizes. Checking for overflow after it happens is the only way
> that is sure to work in a hypothetical system that uses something else than
> 2's complement representation.
>
> 2). This is a security problem. It takes a very twisted mind to predict that
> your code is not safe when you are actually checking for overflow.
>
> 3). I think that you are interpreting the C/C++ standard in an over-pedantic
> way. There are good reasons why the standard says that the behavior in case
> of integer overflow is undefined. 2's complement wrap-around is not the only
> possible behavior in case of overflow. Other possibilities are: saturate,
> signed-magnitude wrap-around, reserve a bit pattern for overflow, throw an
> exception. If a future implementation uses internal floating point
> representation for integers then an overflow might variously cause loss of
> precision, INF, NAN, or throw an exception. I guess this is what is meant
> when the standard says the behavior is undefined. What the gcc compiler is
> doing is practically denying the existence of overflow (
> http://www.mail-archive.com/pgsql-hackers@postgresql.org/msg105239.html ) to
> the point where it can optimize away an explicit check for overflow. I
> refuse to believe that this is what the standard-writers intended. There
> must be a sensible compromize that allows the optimizer to make certain
> assumptions that rely on overflow not occurring without going to the extreme
> of optimizing away an overflow check.
>
> 4). The bug in my case disappears if I compile with -fwrapv or
> -fno-strict-overflow or without -O2, but this is not my point. My point is
> that gcc should be useful to a programmer with average skills.
>
> 5). I have tested many different C++ compilers, and gcc turned out to be the
> one that optimizes best. You guys are doing a fantastic job! Gcc has the
> potential to beat the expensive commercial compilers. But one obstackle to
> its use is that it has a well-deserved reputation for being over-pedantic.

Its really too bad that GCC does not offer something for overflow and
carry. There's been a couple of feature requests for overflow and
carry testing when the CPU supports it, but I don't believe its gained
any momentum (what does that say about secure programming practices?):
 * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48580
 * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49467

Jeff

  reply	other threads:[~2011-07-23 21:06 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-23 20:14 Agner Fog
2011-07-23 21:06 ` Jeffrey Walton [this message]
2011-07-25  6:07   ` Ian Lance Taylor
2011-07-25  6:04 ` Ian Lance Taylor
2011-07-25  8:32   ` Agner Fog
2011-07-25 17:18     ` me22
2011-07-25 17:50     ` Ian Lance Taylor
2011-07-26  9:39   ` Agner Fog
2011-07-26 10:35     ` Andrew Haley
2011-07-26 17:31       ` Andrew Haley
2011-07-27 15:03         ` Agner Fog
2011-07-26 14:55     ` Jeffrey Walton
     [not found]   ` <4E2E6CC6.3040106@agner.org>
2011-07-26 14:44     ` Ian Lance Taylor
2011-07-26 16:24       ` Agner Fog
2011-07-26 18:17         ` Ian Lance Taylor
2011-07-25  9:43 ` Andrew Haley
2011-07-25 15:38   ` Agner Fog
2011-07-25 16:22     ` Andrew Haley
2011-07-30 23:30       ` Vincent Lefevre
2011-08-01  8:59         ` Andrew Haley

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='CAH8yC8=cwCuoP5bh8dPVUA12KwpqT2-OobhRGt6XD+BzA83pTQ@mail.gmail.com' \
    --to=noloader@gmail.com \
    --cc=agner@agner.org \
    --cc=gcc-help@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).