public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "mikpe at it dot uu dot se" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/45034] [4.3/4.4/4.5/4.6 Regression] "safe" conversion from unsigned to signed char gives broken code
Date: Tue, 27 Jul 2010 22:18:00 -0000	[thread overview]
Message-ID: <20100727221827.4340.qmail@sourceware.org> (raw)
In-Reply-To: <bug-45034-7665@http.gcc.gnu.org/bugzilla/>



------- Comment #8 from mikpe at it dot uu dot se  2010-07-27 22:18 -------
(In reply to comment #7)
> In fact, it seems that the error is already there at the very
> beginning: the .original dump shows
> 
> fixnum_neg
> {
>   ux = (unsigned char) x;
>   uy = (unsigned char) -(signed char) ux;
>   ...
> }
> 
> That is, the negation of unsigned char value is implemented by casting it to
> signed char, which introduces signed overflow if the value of x is -128.  As
> far as I understand the C standard, this seems incorrect.

It depends on how GCC interprets that cast and negation:
- if the cast has C semantics, then (signed char)ux causes overflow
- if the cast wraps, then it is fine and yields (signed char)-128
- if the negation has C semantics, then (signed char)-128 is widened to int and
then negated to 128
- if the negation maps signed char to signed char, then it causes overflow

IMO, a serious problem with the C standard is that

    signed char x = -1;
    signed char y = (signed char)(unsigned char)x;

triggers signed overflow causing undefined behaviour.

This comes from an asymmetry between cast to unsigned and cast to signed:
- cast from signed to unsigned is a total and injective function
- cast from unsigned to signed is a partial function with range from 0 to the
maximum of the signed type (inclusive), which excludes values converted from
negative signed values

(I'd be happy to be proven wrong about this, if anyone can cite relevant
sections from n1124 (C99 TC2) or n1494 (C1x draft) to the contrary.)

Personally I think GCC should treat source-level casts as wrapping, regardless
of -fstrict-overflow and -fno-wrapv.  Perhaps it intends to, and we're just
seeing the effects of bugs.


-- 


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


  parent reply	other threads:[~2010-07-27 22:18 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-22 21:13 [Bug c/45034] New: " mikpe at it dot uu dot se
2010-07-22 21:14 ` [Bug c/45034] " mikpe at it dot uu dot se
2010-07-22 21:26 ` [Bug tree-optimization/45034] " pinskia at gcc dot gnu dot org
2010-07-23  4:14 ` hjl dot tools at gmail dot com
2010-07-23  8:52 ` [Bug tree-optimization/45034] [4.3/4.4/4.5/4.6 Regression] " rguenth at gcc dot gnu dot org
2010-07-24 18:47 ` mikpe at it dot uu dot se
2010-07-25 10:56 ` mikpe at it dot uu dot se
2010-07-26 14:47 ` rakdver at gcc dot gnu dot org
2010-07-27 22:18 ` mikpe at it dot uu dot se [this message]
2010-07-27 22:43 ` joseph at codesourcery dot com
2010-07-27 23:09 ` rakdver at kam dot mff dot cuni dot cz
2010-07-28  9:32 ` rguenth at gcc dot gnu dot org
2010-07-28 12:24 ` rguenth at gcc dot gnu dot org
2010-07-28 14:14 ` mikpe at it dot uu dot se
2010-07-28 15:38 ` mikpe at it dot uu dot se
2010-07-28 23:32 ` mikpe at it dot uu dot se
2010-07-29 11:00 ` rguenth at gcc dot gnu dot org
2010-07-29 11:00 ` [Bug tree-optimization/45034] [4.3/4.4/4.5 " rguenth at gcc dot gnu dot org
2010-08-08 15:50 ` rguenth at gcc dot gnu dot org
2010-08-08 15:51 ` [Bug tree-optimization/45034] [4.3/4.4 " rguenth at gcc dot gnu dot 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=20100727221827.4340.qmail@sourceware.org \
    --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).