|
|
Subscribe / Log in / New account

Re: [PATCH] Use NULL instead of integer 0 in security/selinux/

From:  Linus Torvalds <torvalds-AT-osdl.org>
To:  Herbert Xu <herbert-AT-gondor.apana.org.au>
Subject:  Re: [PATCH] Use NULL instead of integer 0 in security/selinux/
Date:  Wed, 7 Jul 2004 22:19:53 -0700 (PDT)
Cc:  Chris Wright <chrisw-AT-osdl.org>, akpm-AT-osdl.org, linux-kernel-AT-vger.kernel.org, sds-AT-epoch.ncsc.mil, jmorris-AT-redhat.com, mika-AT-osdl.org



On Thu, 8 Jul 2004, Herbert Xu wrote:
>
> Chris Wright <chrisw@osdl.org> wrote:
> > Fixup another round of sparse warnings of the type:
> >        warning: Using plain integer as NULL pointer
> 
> What's wrong with using 0 as the NULL pointer? In contexts where
> a plain 0 is unsafe, NULL is usually unsafe as well.

It's not about "unsafe". It's about being WRONG.

The fact is, people who write "0" are either living in the stone age, or 
are not sure about the type. "0" is an _integer_. It's not a pointer. It 
may be legal C, but that doesn't make it right anyway. "0" also happens to 
be one of the more _common_ integers, so mistakes happen.

Looking at the code, people that used "0" for NULL pointers quite often 
obviously were NOT aware of the types. The code just happened to pass 
through the compiler without warnings.

The same is true the other way too. I've seen too many damn people who use 
NULL in an integer context, and any compiler system that makes NULL be 
just a plain "0" is frigging _broken_.  NULL is _not_ an integer. Never 
has been, and if the compiler doesn't warn loudly about obviously idiotic 
code, then the compiler is broken.

In other words:

	char * p = 0;	/* IS WRONG! DAMMIT! */
	int i = NULL;	/* THIS IS WRONG TOO! */

and anybody who writes code like the above either needs to get out of the 
kernel, or needs to get transported to the 21st century. 

		Linus
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/



to post comments

Re: [PATCH] Use NULL instead of integer 0 in security/selinux/

Posted Jul 22, 2004 19:35 UTC (Thu) by h.j.thomassen (guest, #15232) [Link]

I support Linus' view that NULL should be written if a pointer is meant to be zero. But his statement "NULL is _not_ an integer. Never has been,..." is historically incorrect. Read K&R, 2nd Ed, page 102 where it clearly says the opposite.

int i = NULL; is wrong, and always has been.
char *p = 0; may be undesirable, but is syntactically correct.

Since the second statement is syntactically correct, no compiler will complain. Since compilers will not force you to write "char *p = NULL;" it deserves a place in the kernel coding standards.


Copyright © 2004, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds