mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@osdl.org>
To: Rainer Weikusat <rainer.weikusat@sncag.com>
Cc: Alan Cox <alan@lxorguk.ukuu.org.uk>,
	Rainer Weikusat <rweikusat@sncag.com>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	akpm@osdl.org
Subject: Re: Implementation defined behaviour in read_write.c
Date: Wed, 22 Sep 2004 07:41:45 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.58.0409220731080.25656@ppc970.osdl.org> (raw)
In-Reply-To: <87k6umetg1.fsf@farside.sncag.com>



On Wed, 22 Sep 2004, Rainer Weikusat wrote:
> 
> 	6.3.1.3 Signed and unsigned integers
> 
> 	When a value with integer type is converted to another integer
> 	type other than _Bool, if the value can be represented by the
> 	new type, it is unchanged.
> 
> 	[...]
> 
> 	Otherwise, the new type is signed and the value cannot be
> 	represented in it; either the result is implementation-defined
> 	or an implementation-defined signal is raised.
> 
> The requirement for implementation defined is that the implementation
> documents the behaviour (which gcc at least up to 3.4.4 doesn't).

They don't, because they do the only thing they _can_ do. Bit-for-bit copy 
of a 2's complement value. Anything else would be basically impossible for 
an optimizing compiler to do unless it actively _tried_ to screw the user 
over. In other words, you're likely to see something else only on a C 
simulator interface that does strict conformance testing (as opposed to 
test for working).

And you are correct to point out the difference between implementation- 
defined and un-defined. Implementation-defined means that it has some 
well-defined semantics, and quite frankly, Linux _does_ depend on 2's 
complement. I don't expect to ever see anything else (where "ever" is a 
long time, although obviously not really "forever"), but if we do, we'll 
have to consider that architecture something very special indeed.

> This
> not a problem with the current compiler, but I happen to know by
> coincedence that some people of unknown relations to the gcc team
> (like the person who wrote this advisory:
> <URL:http://cert.uni-stuttgart.de/advisories/c-integer-overflow.php>)
> would like to turn it into a problem, because they strongly believe it
> is "the right thing to do"

There are tons of people who have theoretical concerns that they try to
push on the real world. Too many of them have talked to the gcc people,
but I think the gcc people are basically sane. So I wouldn't worry _too_ 
much.

That said, there are other cases where signed integer arithmetic should be 
avoided. The signed<->unsigned conversions are safe due to their 
implementation-defined behaviour (and only one sane way to do them), but 
there _are_ cases like signed integer overflow that really is undefined, 
and where a compiler can actually generate code that differs because it 
"knows" that signed integers cannot overflow.

However, in this case that is not what is happening in read_write.c. All 
the arithmetic is done in proper unsigned types, and only the last check 
is done with a (well-defined) signed conversion.

Btw, to see other places where we do depend on this 2's complement 
behaviour, just look at "time_before()" and friends.

		Linus

      reply	other threads:[~2004-09-22 14:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-09-20 15:54 Rainer Weikusat
2004-09-21  5:43 ` Rainer Weikusat
2004-09-21 10:57 ` Alan Cox
2004-09-22  5:58   ` Rainer Weikusat
2004-09-22 14:41     ` Linus Torvalds [this message]

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.58.0409220731080.25656@ppc970.osdl.org \
    --to=torvalds@osdl.org \
    --cc=akpm@osdl.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rainer.weikusat@sncag.com \
    --cc=rweikusat@sncag.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

all inboxes | Powered by JetHome®