public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
To: Joe Buck <jbuck@synopsys.com>
Cc: gcc@gcc.gnu.org
Subject: Re: Suggested warning: "negating an expression of unsigned type does not yield a negative value"
Date: Mon, 06 Oct 2003 16:11:00 -0000	[thread overview]
Message-ID: <87pthauz1i.fsf@student.uni-tuebingen.de> (raw)
In-Reply-To: <20031006085958.A12894@synopsys.com>

Joe Buck <jbuck@synopsys.com> writes:

> On Mon, Oct 06, 2003 at 02:46:43PM +0200, Falk Hueffner wrote:
> > I just found yet another bug of the kind:
> > 
> > int f (int *p, unsigned x) { return p[-x]; }
> > 
> > which only manifests on 64 bit platforms, because most (all?)
> > platforms have wrapping address arithmetic.
> 
> The C and C++ standards require that unsigned values obey modulo 2**N
> arithmetic, so the value of -x is rigorously defined.

Sure it is. But it is not what is intended. Example: x = 5, then
-x=4294967291, i.e., p will be advanced by 4294967291 bytes, which is
way beyond the legal range of p, but happens to work anyway on 32 bit
architectures (but not on 64 bit architectures).

> >   char *namestart;
> >   size_t namelen;
> >   [...]
> >   for (namelen = 1; !ISSPACE (namestart[-namelen]); namelen++)
> > 
> > This looks actually invalid to me, although it will probably work
> > everywhere.
> 
> It's valid everywhere.

I'm pretty sure it's not. -namelen is, again, something like
4294967291 (or 18446744073709551611), which is not a legal array
index.

> > In fold_const.c, there's
> > 
> > case RSHIFT_EXPR:
> >       int2l = -int2l;
> > 
> > also "invalid but works" since it's later passed to a function taking
> > int.
> 
> Again, this is valid everywhere.

No, this produces an unsigned value which cannot be represented in a
signed value of same width, but is converted to signed, which is
undefined according to the standard.

> > Then there's everybody's favourite idiom "x &= -x", but it can be
> > expressed clearer as "x &= ~x + 1".
> 
> Again, it's fine as is.

I agree with that.

-- 
	Falk

  reply	other threads:[~2003-10-06 16:11 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-06 12:46 Falk Hueffner
2003-10-06 16:00 ` Joe Buck
2003-10-06 16:11   ` Falk Hueffner [this message]
2003-10-06 17:23     ` Jamie Lokier
2003-10-06 17:48     ` Joe Buck
2003-10-06 17:52       ` Falk Hueffner

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=87pthauz1i.fsf@student.uni-tuebingen.de \
    --to=falk.hueffner@student.uni-tuebingen.de \
    --cc=gcc@gcc.gnu.org \
    --cc=jbuck@synopsys.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).