From: Derek M Jones <derek@knosof.co.uk>
To: Al Viro <viro@ZenIV.linux.org.uk>
Cc: Josh Triplett <josh@freedesktop.org>,
Linus Torvalds <torvalds@linux-foundation.org>,
Harvey Harrison <harvey.harrison@gmail.com>,
Andrew Morton <akpm@linux-foundation.org>,
LKML <linux-kernel@vger.kernel.org>,
linux-sparse@vger.kernel.org
Subject: Re: [PATCH 7/7] asm-generic: suppress sparse warning in ioctl.h
Date: Fri, 04 Apr 2008 15:08:01 +0100 [thread overview]
Message-ID: <47F63641.4090103@knosof.co.uk> (raw)
In-Reply-To: <20080404011951.GC9785@ZenIV.linux.org.uk>
Al,
> * typedef *can* be variably-modified, but only in block scope.
> Warning: this can get sticky for us - all sizes are evaluated when
> typedef is reached. IOW,
> typedef int a[n];
> a x;
> if (n++ == 5) {
> a y;
> int z[n];
> }
> will have size of y equal to that of x, but *not* equal to that of z.
An opportunity for Sparse to issue a warning :-)
> Another thing to keep in mind is that sizeof(VLA) is not a constant
> expression *and* that sizeof argument is evaluated. IOW, not only
> sizeof(int [n = f()])
> has side effects, but in
> int (*p)[n];
> ...
> sizeof(*(g(), p))
> will have g() evaluated. Note that if p had been declared as
> int (*p)[4];
> the same sizeof() would *NOT* have called anything. This, BTW, is where
> the rules for what an integer constant expression is are getting bloody
> important:
Any side effect appearing in a sizeof operand ought to be flagged.
There are people out there who think that the side effects occur (even
in C90).
Sentence 1122: http://c0x.coding-guidelines.com/6.5.3.4.html
"If the type of the operand is a variable length array type, the operand
is evaluated;"
But watch out for sentence 1584:
http://c0x.coding-guidelines.com/6.7.5.2.html
"Where a size expression is part of the operand of a sizeof operator and
changing the value of the size expression would not affect the result of
the operator, it is unspecified whether or not the size expression is
evaluated."
> int n = 1;
> int f(void)
> {
> int (*p)[1 + (0 && n)];
> return sizeof(*(g(), p));
> }
>
> _must_ call g() according to C99, while the same with
> enum {n = 1;} must not, even though n won't be even looked at in either
> case *and* any sane compiler will find return value of f() at compile
> time, turning it to
Any sane compiler that performs the analysis needed to deduce that
the expression inside the parenthesis always evaluated to zero
will turn it into....
> One more thing: use of sizeof(variably-modified type) may or may not
> evaluate size expressions in there if they do not affect the result.
> IOW, it's unspecified whether
> sizeof(int (*)[n++])
> increments n; different compilers are broken in different ways and it
This language feature came about because at least one vendor on the
WG14 committee had a compiler that optimized away subexpressions
within a sizeof that did not contribute to the result of the
evaluation. My attempt to stop the behavior being unspecified
did not succeed :-(
--
Derek M. Jones tel: +44 (0) 1252 520 667
Knowledge Software Ltd mailto:derek@knosof.co.uk
Applications Standards Conformance Testing http://www.knosof.co.uk
next prev parent reply other threads:[~2008-04-04 14:30 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1207182818.5740.26.camel@brick>
[not found] ` <alpine.LFD.1.00.0804021752040.14670@woody.linux-foundation.org>
[not found] ` <20080403013420.GV9785@ZenIV.linux.org.uk>
[not found] ` <alpine.LFD.1.00.0804021848140.14670@woody.linux-foundation.org>
[not found] ` <20080403024117.GZ9785@ZenIV.linux.org.uk>
2008-04-03 20:31 ` [PATCH 7/7] asm-generic: suppress sparse warning in ioctl.h Josh Triplett
2008-04-04 1:19 ` Al Viro
2008-04-04 2:00 ` Al Viro
2008-04-04 14:08 ` Derek M Jones [this message]
2008-04-04 18:42 ` Al Viro
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=47F63641.4090103@knosof.co.uk \
--to=derek@knosof.co.uk \
--cc=akpm@linux-foundation.org \
--cc=harvey.harrison@gmail.com \
--cc=josh@freedesktop.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sparse@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=viro@ZenIV.linux.org.uk \
/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 NNTP newsgroup(s).