From: Olaf Titz <olaf@bigred.inka.de>
To: linux-kernel@vger.kernel.org
Subject: Re: [PATCH] Use NULL instead of integer 0 in security/selinux/
Date: Sun, 11 Jul 2004 23:47:58 +0200 [thread overview]
Message-ID: <E1BjmAw-0005MS-00@bigred.inka.de> (raw)
In-Reply-To: m1smc09p6m.fsf@ebiederm.dsl.xmission.com
> Only because the definition of the semantics of ``if'' is in terms of
> comparisons with ``0'', and I am familiar enough with the C
> programming language that, that is how I read it. It is still
> the case that because the comparison happens in pointer context the
> ``0'' referred to is the null pointer constant.
>
> For some of us who are extremely familiar with C your argument is
> confusing. You make statements that sound like they are about the
> definition of the C programming language when in fact they are
> criticism of a given C programming style.
>
> Since I am already making distinctions 0 as the integer value and
> 0 as the pointer constant when 0 is implicitly introduced. It is
> really not confusing to me in the case of manifest constants.
So the real question is why C has no "null" token like Java or Pascal
and re-uses the "0" token (which is really no token by itself but a
numeric-constant token which happens to have a special value).
If your argument holds that "0" in a pointer context really is a
special token like Java's "null" (which is explicitly defined by the
standard as a pointer different from any other pointer) then it would
be possible to implement a compiler which not only defines NULL to
-1L, as someone mentioned here, but actually generates an all-ones bit
pattern out of the constant 0 when used in a pointer context, yet
generates an all-zeros bit pattern when used in an integer context.
It also would have to implement the implicit null-comparison in a
boolean context appropriately.
This probably would work with all programs which make a clear
distinction between pointers and integer values, but you have to be
really pedantic about these "stylistic" issues to always get it right
in C. (Worse in C++ where usage of NULL is discouraged, I've always
wondered about the reasons.)
The real problem, however, is that this "stylistic" issue may quickly
become a _correctness_ issue as soon as the actual bit pattern of a
pointer in memory is taken to have any meaning. I.e. it already starts
when you initialize a structure with memset().
And this is the reason why really strongly typed languages never allow
assignment of a pointer to or from any other data type. (Java is that
strict, Pascal has a possible backdoor in the "record case" structure
if implemented as overlays like C's unions but otherwise is as strict
too, not sure about Ada.)
_The bit pattern of a pointer must have no meaning to the program_.
To answer the question from the first paragraph, it is "because C does
_not_ strongly distinguish between pointer and non-pointer values".
And for this reason people have invented the NULL constant, and the
convention that
"if (x)" means "if (x != 0)" in numeric context, and
"if (x)" means "if (x != NULL)" in pointer context.
This resolves all the ambiguities and allows people to use C as a
strongly typed language, but can break with program(mer)s taking
pointers as equivalent to numbers.
Olaf
PS. I wonder how many bugs have been avoided in the Linux kernel by
this kind of style pedantery vs. how many bugs have crept into other
systems where people are more sloppy.
next prev parent reply other threads:[~2004-07-12 17:47 UTC|newest]
Thread overview: 75+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-07-07 19:25 [PATCH] Use NULL instead of integer 0 in security/selinux/ Chris Wright
2004-07-08 3:12 ` Herbert Xu
2004-07-08 3:27 ` David S. Miller
2004-07-08 3:43 ` Miles Bader
2004-07-08 5:22 ` Linus Torvalds
2004-07-08 5:35 ` Miles Bader
2004-07-08 15:58 ` Linus Torvalds
2004-07-08 16:23 ` Dave Jones
2004-07-08 17:57 ` Geert Uytterhoeven
2004-07-09 11:23 ` Roman Zippel
2004-07-10 19:41 ` Geert Uytterhoeven
2004-07-11 10:29 ` Geert Uytterhoeven
2004-07-11 10:54 ` viro
2004-07-11 16:14 ` Linus Torvalds
2004-07-11 20:05 ` H. Peter Anvin
2004-07-12 15:58 ` Florian Weimer
2004-07-12 16:34 ` H. Peter Anvin
2004-07-08 11:18 ` Herbert Xu
2004-07-08 13:10 ` Andreas Schwab
2004-07-08 13:56 ` Herbert Xu
2004-07-08 14:13 ` Andreas Schwab
2004-07-08 5:19 ` Linus Torvalds
2004-07-08 16:09 ` Timothy Miller
2004-07-08 16:21 ` Linus Torvalds
2004-07-08 16:27 ` Linus Torvalds
2004-07-08 16:52 ` Timothy Miller
[not found] ` <200407090056.51084.vda@port.imtp.ilyichevsk.odessa.ua>
2004-07-12 15:17 ` Timothy Miller
2004-07-12 16:12 ` Andreas Schwab
2004-07-12 16:51 ` Andrew Pimlott
2004-07-15 16:15 ` Timothy Miller
2004-07-09 20:36 ` Eric W. Biederman
2004-07-09 23:49 ` Paul Jackson
2004-07-10 1:43 ` Kyle Moffett
2004-07-10 1:47 ` Herbert Xu
2004-07-10 21:53 ` Alexandre Oliva
2004-07-10 6:18 ` Linus Torvalds
2004-07-10 6:23 ` Linus Torvalds
2004-07-10 6:58 ` Andrea Arcangeli
2004-07-10 9:48 ` Eric W. Biederman
2004-07-10 15:39 ` Roland Dreier
2004-07-11 2:45 ` Eyal Lebedinsky
2004-07-11 21:19 ` Olaf Titz
2004-07-10 9:39 ` Eric W. Biederman
2004-07-10 9:56 ` Herbert Xu
2004-07-11 21:47 ` Olaf Titz [this message]
2004-07-13 8:43 ` Geert Uytterhoeven
2004-07-14 3:12 ` Alexandre Oliva
2004-07-13 18:53 ` Elladan
2004-07-14 3:15 ` Alexandre Oliva
2004-07-10 12:11 ` Roman Zippel
2004-07-10 21:59 ` Alexandre Oliva
2004-07-11 4:40 ` Linus Torvalds
2004-07-10 9:31 ` Eyal Lebedinsky
2004-07-10 22:07 ` Alexandre Oliva
2004-07-10 23:52 ` Paul Jackson
2004-07-11 0:12 ` Tim Wright
2004-07-12 22:03 ` Bill Davidsen
2004-07-10 8:18 ` Florian Weimer
2004-07-08 11:10 ` Richard B. Johnson
2004-07-08 11:28 ` Michael Buesch
2004-07-08 11:44 ` Martin Zwickel
2004-07-08 12:06 ` Michael Buesch
2004-07-08 12:38 ` [OT] NULL versus 0 (Re: [PATCH] Use NULL instead of integer 0 in security/selinux/) Kari Hurtta
2004-07-08 12:42 ` Michael Buesch
2004-07-08 12:57 ` Kari Hurtta
2004-07-08 15:25 ` Herbert Xu
2004-07-10 8:22 ` [PATCH] Use NULL instead of integer 0 in security/selinux/ Florian Weimer
2004-07-08 11:43 ` P. Benie
2004-07-08 14:32 ` Richard B. Johnson
2004-07-08 15:00 ` Michael Poole
2004-07-08 15:30 ` P. Benie
2004-07-08 15:55 ` Andreas Schwab
2004-07-10 7:09 ` Andrea Arcangeli
2004-07-10 7:02 ` Andrea Arcangeli
2004-07-10 6:59 ` Andrea Arcangeli
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=E1BjmAw-0005MS-00@bigred.inka.de \
--to=olaf@bigred.inka.de \
--cc=linux-kernel@vger.kernel.org \
/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