Re: Pointer wraparound warning

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"Udo A. Steinberg" <us15@xxxxxxxxxxxxxxxxxxxx> writes:

> gcc now complains about the following code snippet, saying...
> warning: assuming pointer wraparound does not occur when comparing
> P +- C1 with P +- C2

Presumably only when you use -Wstrict-overflow=N where N >= 3.  At
that level false positives are likely.


> char const x[] = "deadbeef";
> int main (void)
> {
>     for (char const *ptr = x; ptr < x + 4; ptr++)
>         putc (*ptr, stdout);
> 
>     return 0;
> }

> In the first snippet ptr never points outside x[], so the compiler shouldn't
> warn. What am I missing here?

Nothing.  The compiler could be smarter here.  pointer_may_wrap_p in
fold-const.c could handle TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE.


By the way, the compiler doesn't warn here:

> char const *x = "deadbeef";
> int main (void)
> {
>     for (char const *ptr = x; ptr < x + 4; ptr++)
>         putc (*ptr, stdout);
>     
>     return 0;
> }

because it doesn't know that putc doesn't change x, so it can't
optimize away the comparison.

Ian

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux