This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [LLVMdev] Handling of pointer difference in llvm-gcc and clang


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]