From: Josh Triplett <josh@freedesktop.org>
To: Al Viro <viro@ftp.linux.org.uk>
Cc: linux-sparse@vger.kernel.org,
Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: fun with ?:
Date: Tue, 22 May 2007 16:24:49 -0700 [thread overview]
Message-ID: <46537BC1.9000808@freedesktop.org> (raw)
In-Reply-To: <20070522224619.GI4095@ftp.linux.org.uk>
[-- Attachment #1: Type: text/plain, Size: 2918 bytes --]
Al Viro wrote:
> On Tue, May 22, 2007 at 02:40:11PM -0700, Josh Triplett wrote:
>>> What's going on here is pretty simple: there are two degenerate cases of
>>> conditional operator: pointer vs. null pointer constant and pointer vs.
>>> possibly qualified pointer to void. Look at these cases:
>>> n ? p : NULL => should be the same type as p
>>> n ? p : v => clearly const void * - pointer to void with union of
>>> qualifiers; in this case we obviously lose any information about the type
>>> of object being pointed to.
>> I didn't actually know about the special case for a null pointer constant.
>
> Rationale is pretty simple: normally if you have void * in the mix, you
> _can't_ expect more type information from the result; i.e. you are not
> promised that result of ?: will point to int. However, null pointer constant
> is a chameleon - it accepts whatever pointer type you might need in given
> context. So in that case you do _not_ lose the type information.
Makes sense, except that in C you can assign a void pointer to an arbitrary *
without a warning, so why can't conditional expressions do the equivalent?
>> In particular:
>>> n ? p : (void *)NULL int * => void *
>> Shouldn't this have type int * just like n ? p : NULL ?
>
> No. It's "void * and I _mean_ it". Well... actually (void *)(void *)0 if
> you want to be 100% portable and protect yourself against cretinous systems
> that define NULL to 0.
>
> Again, null pointer constant is not the same thing as null pointer to void.
I see. I find it very strange that (void *)0 and (void *)(void *)0 have
different behavior. I also find it strange that conditional expressions can't
convert void * to an arbitrary pointer as assignment can.
> BTW, there's another painful area: what do we do to somebody who uses
> (void *)(69 + 1 - 70) as null pointer constant? Currenly sparse doesn't
> recognize it as such; C standard does. IMO the right thing to do is
> to add a flag that would switch to full-blown standard rules in that area
> ("integer constant expression returning 0" instead of basically "0 in some
> layers of ()") and flame to the crisp any wanker caught at actually doing
> that. Any suggestions re sufficiently violent warning messages?
I didn't know that the C standard actually *required* constant folding.
Interesting. Would it add excessively to compilation time to apply the usual
Sparse constant folding here? If so, and if you really think this case
matters, let's have an option to turn on this constant folding, and warn
whenever we see it. I'll let you come up with the wording; flame away. :)
Anyone expecting that behavior has some serious dain-bramage. If constant
folding *wouldn't* add excessively to the compilation time, go ahead and
handle the insanity the standard way by default, but still warn for the insane
case.
- Josh Triplett
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 252 bytes --]
next prev parent reply other threads:[~2007-05-22 23:25 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-05-19 2:52 fun with ?: Al Viro
2007-05-22 21:40 ` Josh Triplett
2007-05-22 22:46 ` Al Viro
2007-05-22 23:24 ` Josh Triplett [this message]
2007-05-23 0:02 ` Al Viro
2007-05-23 0:25 ` Al Viro
2007-05-23 1:05 ` Josh Triplett
2007-05-23 4:53 ` Al Viro
2007-05-23 12:26 ` Morten Welinder
2007-05-23 1:03 ` Josh Triplett
2007-06-03 1:05 ` Al Viro
2007-05-23 14:25 ` Neil Booth
2007-05-23 14:32 ` Al Viro
2007-05-23 14:47 ` Neil Booth
2007-05-23 15:32 ` Al Viro
2007-05-23 23:01 ` Neil Booth
2007-05-24 0:10 ` Derek M Jones
2007-05-24 0:14 ` Al Viro
2007-05-23 21:16 ` Derek M Jones
2007-05-23 21:59 ` Linus Torvalds
2007-05-23 23:29 ` Derek M Jones
2007-05-24 0:02 ` Al Viro
2007-05-24 0:29 ` Linus Torvalds
2007-05-24 1:36 ` Brett Nash
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=46537BC1.9000808@freedesktop.org \
--to=josh@freedesktop.org \
--cc=linux-sparse@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=viro@ftp.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).