mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@aerifal.cx>
To: musl@lists.openwall.com
Subject: Re: NULL
Date: Sat, 12 Jan 2013 08:31:14 -0500	[thread overview]
Message-ID: <20130112133114.GH20323@brightrain.aerifal.cx> (raw)
In-Reply-To: <1357973768.32505.5@driftwood>

On Sat, Jan 12, 2013 at 12:56:08AM -0600, Rob Landley wrote:
> >The original reason I left NULL with pointer type was to catch the
> >other idiotic error:
> >
> >    str[len]=NULL;
> >
> >i.e. confusion of NULL with ASCII NUL.
> 
> They're both 0. If the optimizer can't convert the type down when
> handed a constant assignment, the optimizer should be shot.

No. ASCII nul is an integer 0. NULL is a null pointer constant, which
may be an integer constant expression 0 or may be (void *)0. This has
nothing to do with the optimizer and everything to do with constraint
violations. The error was mainly made by C++ programmers (or C
programmers wrongly compiling their programs with C++ compilers...) on
implementations that used 0 as the definition of NULL; when compiled
on most proper C implementations, the code yields an error, because
assignment of a pointer to an integer is a constraint violation. (On
gcc, it's just a warning by default.)

I don't think there's a lot of value in catching this error anymore.

> >However, this raises a good
> >question: short of C11 _Generic, is it even possible for a program to
> >detect whether NULL has integer or pointer type?
> 
> The C99 standard says that NULL has pointer type. Thus when you pass

No it does not. We have addressed this multiple times already.

> it in varargs, it should be a long on any LP64 system which is
> basically "everything but windows" for about 20 years now.

Actually the type doesn't matter to correct programs. The question is
whether we want to coddle incorrect programs, and the answer folks
seem to be leaning towards is yes, in which case 0L would be the right
definition to accomplish this.

> >I know of one way, but it's very obscure:
> 
> You can do sizeof(NULL) and (char *)(NULL+1)-(char *)(NULL) to get
> the size of the type it points to?

NULL+1 is a constraint violation if NULL has pointer type (since the
only pointer type it's permitted to have is void *).

> Not sure what question you're asking...
> 
> >int null_is_ptr_type()
> >{
> >	char s[1][1+(int)NULL];
> >	int i = 0;
> >	return sizeof s[i++], i;
> >}
> 
> (int)NULL is 0 according to C99 so the NULL in there has no effect.

It does. (int)0 is an integer constant expression. (int)(void *)0
happens to be semantically constant, but it's not an integer constant
expression. Therefore, depending on the definition of NULL, s may be a
regular array type or a variable-length array type. In the latter
case, s[i++] has VLA type and thus sizeof is required to evaluate its
argument. GCC versions prior to 4.5 were buggy in this regard.

> And referring to "i++" and "i" in the same statement is explicitly
> undefined behavior (comma is not a sequence point, the compiler is

Comma is a sequence point.

> free to evaluate those in any order and different optimization flags
> _will_ change that order; I got bit by that many moons ago).

No, you were doing something else wrong. To my knowledge there has
never been a compiler that did not honor the comma operator sequence
point, certainly not any GCC or clang.

Rich


  parent reply	other threads:[~2013-01-12 13:31 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-09 11:02 NULL John Spencer
2013-01-09 12:18 ` NULL Szabolcs Nagy
2013-01-09 13:36   ` NULL John Spencer
2013-01-12  6:32     ` NULL Rob Landley
2013-01-12  6:46       ` NULL Rich Felker
2013-01-12  7:15         ` NULL Luca Barbato
2013-01-12 13:33           ` NULL Rich Felker
2013-01-12 11:39       ` NULL Jens Staal
2013-01-09 13:09 ` NULL croco
2013-01-09 13:47   ` NULL John Spencer
2013-01-09 14:49     ` NULL croco
2013-01-09 14:42 ` NULL Luca Barbato
2013-01-09 14:47   ` NULL Rich Felker
2013-01-09 15:03     ` NULL Luca Barbato
2013-01-09 15:18     ` NULL John Spencer
2013-01-09 15:36       ` NULL Rich Felker
2013-01-09 21:11         ` NULL Rob
2013-01-09 21:53           ` NULL Szabolcs Nagy
2013-01-09 22:17             ` NULL Rob
2013-01-09 23:42         ` NULL Szabolcs Nagy
2013-01-12  6:56         ` NULL Rob Landley
2013-01-12  7:07           ` NULL Bobby Bingham
2013-01-12 13:31           ` Rich Felker [this message]
2013-01-13 14:29             ` NULL Rob Landley
2013-01-13 14:56               ` NULL Luca Barbato
2013-01-13 16:29                 ` NULL Rob Landley
2013-01-13 17:14                   ` NULL Luca Barbato
2013-01-13 15:23               ` NULL Strake
2013-01-13 17:17                 ` NULL Luca Barbato
2013-01-13 17:47               ` NULL Szabolcs Nagy
2013-01-13 19:46                 ` NULL Rob Landley
2013-01-14  6:11                   ` NULL Rich Felker
2013-01-14  8:45                     ` musl as a framework to test applications' compatibility with POSIX (was: NULL) Vasily Kulikov
2013-01-14 14:03                       ` Rich Felker
2013-01-14 14:30                         ` Vasily Kulikov
2013-01-14 15:02                           ` Szabolcs Nagy
2013-01-14 15:14                           ` Rich Felker
2013-01-14 13:19                     ` NULL Rob Landley
2013-01-12  5:56 ` NULL Rob Landley
2013-01-12  6:42   ` NULL Rich Felker

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=20130112133114.GH20323@brightrain.aerifal.cx \
    --to=dalias@aerifal.cx \
    --cc=musl@lists.openwall.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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