Thread: remove obsolete NULL casts
In any modern dialect of C, casting the "NULL" pointer literal to a specific pointer type is unnecessary. For example: char *foo; foo = malloc(...); if (foo == (char *) NULL) {...} The cast on the 3rd line serves no useful purpose. Hence, this patch removes all such instances of NULL-pointer casting from the backend. I've attached it in gzip'ed format, as it is 145KB uncompressed. Unless anyone objects, I intend to apply this within 48 hours. -Neil
Attachment
On Mon, Jan 05, 2004 at 04:31:40PM -0500, Neil Conway wrote: > In any modern dialect of C, casting the "NULL" pointer literal to a > specific pointer type is unnecessary. For example: > > char *foo; > foo = malloc(...); > if (foo == (char *) NULL) {...} In src/backend/port/darwin/system.c you replaced: execl(_PATH_BSHELL, "sh", "-c", command, (char *) NULL); By: execl(_PATH_BSHELL, "sh", "-c", command, NULL); I think that is one of the exceptions where you do have to cast it, because the type is unknown. You can only remove the cast when you actually know what pointer type it is. Kurt
Kurt Roeckx <Q@ping.be> writes: > In src/backend/port/darwin/system.c you replaced: > > execl(_PATH_BSHELL, "sh", "-c", command, (char *) NULL); > > By: > execl(_PATH_BSHELL, "sh", "-c", command, NULL); > > I think that is one of the exceptions where you do have to cast it, > because the type is unknown. You can only remove the cast when you > actually know what pointer type it is. Ah, good catch! Upon further investigation, you're completely correct: technically, you need to cast the NULL literal to a pointer type in a function call if (a) there is no prototype for the function, or (b) the function is a varargs function (which execl() is). I'd imagine it would only make a difference on a machine where the null pointer is represented by a non-zero bit pattern (i.e. it shouldn't make a difference on any modern machine), but we should do this correctly, of course. Interesting -- you learn something new every day :-) I won't bother posting an updated patch, but I'll include your fix in the patch when I apply it. Thanks again. -Neil
Neil Conway <neilc@samurai.com> writes: > Ah, good catch! Upon further investigation, you're completely correct: > technically, you need to cast the NULL literal to a pointer type in a > function call if (a) there is no prototype for the function, or (b) > the function is a varargs function (which execl() is). > I'd imagine it would only make a difference on a machine where the > null pointer is represented by a non-zero bit pattern (i.e. it > shouldn't make a difference on any modern machine), Actually, I think it would be likely to be important on any machine where sizeof(pointer) != sizeof(int), which is reasonably common in 64-bit-pointer land. C99 saith An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. So one plausible definition of NULL is just "#define NULL 0", and without the explicit cast it would probably be taken as an integer argument, which would be the wrong size. > but we should do this correctly, of course. Check. > I won't bother posting an updated patch, but I'll include your fix in > the patch when I apply it. Thanks again. Sounds good. regards, tom lane