public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "pinskia at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/107890] UB on integer overflow impacts code flow
Date: Mon, 28 Nov 2022 08:26:49 +0000	[thread overview]
Message-ID: <bug-107890-4-5naF5de7WV@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-107890-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107890

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
There is always a trade off here.
We made the decision that signed integer overflow is undefined because we want
to do optimizations. Gcc does provide an option which makes them behave well
defined at runtime, -fwrapv . This is similar to strict aliasing with respect
to optimizations in the sense it is hard to decide if the optimizations is
being done will break what people assumptions are. This is part of the reason
why there is a specifications (standard) so people can write to it.
There are other undefined behavior that gcc has started to take advantage of
(e.g. in c++ if there is no return with a value in a function with that returns
non-void). The question is where do you draw the line with respect to undefined
behaviors, the answer is complex sometimes, especially if you are optimizing.

In this example the range of x is known to be positive so multiplying by
another positive # gives a positive result and then dividing by a positive
value still is positive. The check for negative result is optimized away.

  parent reply	other threads:[~2022-11-28  8:26 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-27 21:31 [Bug c/107890] New: " gcc at pkh dot me
2022-11-27 21:36 ` [Bug c/107890] " pinskia at gcc dot gnu.org
2022-11-28  1:02 ` redi at gcc dot gnu.org
2022-11-28  7:51 ` muecker at gwdg dot de
2022-11-28  8:26 ` pinskia at gcc dot gnu.org [this message]
2022-12-01  7:44 ` egallager at gcc dot gnu.org

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-107890-4-5naF5de7WV@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).