public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Pointer wraparound warning
@ 2008-04-29  8:47 Udo A. Steinberg
  2008-05-03  0:51 ` Ian Lance Taylor
  0 siblings, 1 reply; 3+ messages in thread
From: Udo A. Steinberg @ 2008-04-29  8:47 UTC (permalink / raw)
  To: gcc-help

Hi,

With the addition of the following patch
http://gcc.gnu.org/ml/gcc-patches/2008-04/msg00565.html

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

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

    return 0;
}

However, it does not complain if the code is changed as follows:

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

The description included in the patch states:

  This option also allows the compiler to assume strict pointer
  semantics: given a pointer to an object, if adding an offset to that
  pointer does not produce a pointer to the same object, the addition is
  undefined.  This permits the compiler to conclude that @code{p + i >
  p} is always true for a pointer @code{p} and integer @code{i}.  This
  assumption is only valid if pointer wraparound is undefined, as the
  expression is false if @code{p + i} overflows.

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

Cheers,

	- Udo

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Pointer wraparound warning
  2008-04-29  8:47 Pointer wraparound warning Udo A. Steinberg
@ 2008-05-03  0:51 ` Ian Lance Taylor
  2008-05-04 17:40   ` Udo A. Steinberg
  0 siblings, 1 reply; 3+ messages in thread
From: Ian Lance Taylor @ 2008-05-03  0:51 UTC (permalink / raw)
  To: Udo A. Steinberg; +Cc: gcc-help

"Udo A. Steinberg" <us15@os.inf.tu-dresden.de> 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Pointer wraparound warning
  2008-05-03  0:51 ` Ian Lance Taylor
@ 2008-05-04 17:40   ` Udo A. Steinberg
  0 siblings, 0 replies; 3+ messages in thread
From: Udo A. Steinberg @ 2008-05-04 17:40 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: gcc-help

On Fri, 02 May 2008 17:50:53 -0700 Ian Lance Taylor (ILT) wrote:

ILT> "Udo A. Steinberg" <us15@os.inf.tu-dresden.de> writes:
ILT> 
ILT> > gcc now complains about the following code snippet, saying...
ILT> > warning: assuming pointer wraparound does not occur when comparing
ILT> > P +- C1 with P +- C2
ILT> 
ILT> Presumably only when you use -Wstrict-overflow=N where N >= 3.  At
ILT> that level false positives are likely.

Actually it warns at N >= 2. I'm using gcc version 4.4.0 20080428
(experimental) via: g++ -Os -Wstrict-overflow=2 foo.cpp

ILT> > In the first snippet ptr never points outside x[], so the compiler
ILT> > shouldn't warn. What am I missing here?
ILT> 
ILT> Nothing.  The compiler could be smarter here.  pointer_may_wrap_p in
ILT> fold-const.c could handle TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE.

That would be nice.

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

[...]

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

Ahh ok! Thanks for the explanation.

Cheers,

	- Udo

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-05-04 17:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-29  8:47 Pointer wraparound warning Udo A. Steinberg
2008-05-03  0:51 ` Ian Lance Taylor
2008-05-04 17:40   ` Udo A. Steinberg

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).