Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is NULL a valid parameter for the strcmp()?

1 view
Skip to first unread message

Vladimir Grigoriev

unread,
Jul 22, 2008, 1:04:13 PM7/22/08
to
Is NULL a valid parameter for the strcmp() function according to the C
standard?

I found the following code and I have doubts that it is valid

char *parm = NULL;
....
if (strcmp(parm, NULL))
bla,bla,bla

I think that it would be simpler and more correctly to write

if ( parm == NULL )
bla,bla,bla

MS Visual C++ 2005 EE abnormally terminates if the original if statement is
used.

Vladimir Grigoriev


Sergiy Kanilo

unread,
Jul 22, 2008, 1:22:36 PM7/22/08
to
Vladimir Grigoriev wrote:
> Is NULL a valid parameter for the strcmp() function according to the C
> standard?
>
> I found the following code and I have doubts that it is valid
>
> char *parm = NULL;
> ....
> if (strcmp(parm, NULL))
> bla,bla,bla

From ISO/IEC 9899:1999 (E):
7.1.4 Use of library functions
1 [...] If an argument to a function has an invalid value (such as [...]
a null pointer [...] the behavior is undefined.

Cheers,
Serge


Alan Bellingham

unread,
Jul 22, 2008, 1:19:37 PM7/22/08
to
"Vladimir Grigoriev" <vlad....@mail.ru> wrote:

>Is NULL a valid parameter for the strcmp() function according to the C
>standard?

No. The standard documents strcmp() as comparing two strings as pointed
to by its parameters. If the pointers don't point to strings, then you
aren't comparing strings, and the function is fully entitled to fail.

NULL never points to a string, and strcmp() is fully permitted to blow
up.

If the pointer points to random garbage outside the memory area you own,
then again it's not pointing to a string, and strcmp() is fully
permitted to blow up.

If the pointer happens to be pointing into your memory area, but there
is no string terminator before the end of your memory area, then again,
it's not pointing to a string, and strcmp() is fully permitted to blow
up.

Alan Bellingham
--
Team Browns
ACCU Conference 2009: to be announced

Vladimir Grigoriev

unread,
Jul 22, 2008, 1:40:38 PM7/22/08
to
"Sergiy Kanilo" <ska...@hotmail.com> wrote in message
news:488617df$1...@newsgroups.borland.com...
Thanks Alan and Serge. I thought so that the code is invalid. It is an old
C-code and it was kept to our days and nobody take it into account.:)

Vladimir Grigoriev


Alan Bellingham

unread,
Jul 23, 2008, 5:10:04 AM7/23/08
to
"Vladimir Grigoriev" <vlad....@mail.ru> wrote:

>Thanks Alan and Serge. I thought so that the code is invalid. It is an old
>C-code and it was kept to our days and nobody take it into account.:)

It wasn't just horrible, it was stupid too.

It's permissible for an implementation of strcmp() to not blow up:
behaviour is merely undefined, and that can actually do something useful
without *technically* breaking the rules. If a version of strcmp()
decides that NULL is equivalent to "" for string comparison purposes,
then that's legal. It's just deeply unfortunate if anyone ever relies on
it and then someone ports the code.

It may be that the original code was looking not just for NULL, but for
empty strings, so ' if ((parm == 0) || (*parm == 0)) {...}'.

Eliot Frank

unread,
Jul 23, 2008, 12:32:18 PM7/23/08
to
Vladimir Grigoriev wrote:
> I found the following code and I have doubts that it is valid
>
> char *parm = NULL;
> ....
> if (strcmp(parm, NULL))
> bla,bla,bla
>
> I think that it would be simpler and more correctly to write
>
> if ( parm == NULL )
> bla,bla,bla
>

It's certainly invalid. I seem to recall however that BSD Unix running
on a VAX, that address 0 (NULL) was actually mapped into user memory and
pointed to a zero filled data word and so a NULL char pointer would be
equivalent to "\0". Needless to say coding to that behavior leads to
portability issues.

The intent of the code may be

if ((parm == NULL) || (*parm == '\0'))
bla,bla,bla

i.e., checking for an "empty" string.

-Eliot

Sergiy Kanilo

unread,
Jul 23, 2008, 3:09:44 PM7/23/08
to
Eliot Frank wrote:
> Vladimir Grigoriev wrote:
>> I found the following code and I have doubts that it is valid
>>
>> char *parm = NULL;
>> ....
>> if (strcmp(parm, NULL))
>> bla,bla,bla
[...]

> The intent of the code may be
>
> if ((parm == NULL) || (*parm == '\0'))
> bla,bla,bla
>
> i.e., checking for an "empty" string.

IMHO it should be "checking for non"empty" string",
as strcmp returns 0 if strings are equal

Cheers,
Serge

0 new messages