public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Joseph S. Myers" <joseph@codesourcery.com>
To: Richard Guenther <richard.guenther@gmail.com>
Cc: florian.merz@kit.edu, gcc@gcc.gnu.org
Subject: Re: [LLVMdev] Handling of pointer difference in llvm-gcc and clang
Date: Thu, 11 Aug 2011 18:58:00 -0000	[thread overview]
Message-ID: <Pine.LNX.4.64.1108111851570.20653@digraph.polyomino.org.uk> (raw)
In-Reply-To: <CAFiYyc329Ci7VA=TnK0YD-jCsY=ReLk4LOE8a4wR_XEN990Z6w@mail.gmail.com>

On Thu, 11 Aug 2011, Richard Guenther wrote:

> int x,y;
> int main ()
> {
>   char *a, *b;
>   __INTPTR_TYPE__ w;
>   if (x)
>     a = 0x7ffffffe;
>   else
>     a = 0x7fffffff;
>   if (y)
>     b = 0x80000001;
>   else
>     b = 0x80000000;
>   w = b - a;
>   return w;
> }
> 
> indeed traps with -ftrapv for me which suggests you are right.
> 
> Joseph?

Subtracting pointers via conversion to integers is wrong in a similar way 
to the pre-POINTER_PLUS_EXPR representation of pointer addition 
(converting the integer operand to a pointer type).  Unlike that 
representation it isn't actually nonsensical, but logically the operation 
of subtracting two pointers yielding an integer should be represented 
without needing to convert either pointer to an integer type.  In the 
absence of such a representation, then converting to an unsigned type is 
indeed safer.  -ftrapv and -fwrapv should have no effect on pointer 
subtraction.

-- 
Joseph S. Myers
joseph@codesourcery.com

  reply	other threads:[~2011-08-11 18:58 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-11 15:15 Fwd: " Florian Merz
2011-08-11 15:48 ` Richard Guenther
2011-08-11 16:05   ` Florian Merz
2011-08-11 17:11     ` Richard Guenther
2011-08-11 18:58       ` Joseph S. Myers [this message]
2011-08-11 20:16         ` Gabriel Dos Reis
2011-08-11 20:39           ` Joe Buck
2011-08-12  7:32             ` Richard Guenther
2011-08-12  7:59               ` Florian Merz
2011-08-11 17:13     ` Joe Buck
2011-08-11 17:15       ` Richard Guenther
2011-08-11 17:21         ` Florian Merz
2011-08-11 20:14         ` Gabriel Dos Reis

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=Pine.LNX.4.64.1108111851570.20653@digraph.polyomino.org.uk \
    --to=joseph@codesourcery.com \
    --cc=florian.merz@kit.edu \
    --cc=gcc@gcc.gnu.org \
    --cc=richard.guenther@gmail.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).