Discussion:
Is NULL a valid parameter for the strcmp()?
(too old to reply)
Vladimir Grigoriev
2008-07-22 17:04:13 UTC
Permalink
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
Alan Bellingham
2008-07-22 17:19:37 UTC
Permalink
Post by Vladimir Grigoriev
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
Sergiy Kanilo
2008-07-22 17:22:36 UTC
Permalink
Post by Vladimir Grigoriev
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
Vladimir Grigoriev
2008-07-22 17:40:38 UTC
Permalink
Post by Sergiy Kanilo
Post by Vladimir Grigoriev
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
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
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
2008-07-23 09:10:04 UTC
Permalink
Post by Vladimir Grigoriev
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)) {...}'.

Alan Bellingham
--
Team Browns
ACCU Conference 2009: to be announced
Eliot Frank
2008-07-23 16:32:18 UTC
Permalink
Post by Vladimir Grigoriev
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
2008-07-23 19:09:44 UTC
Permalink
Post by Eliot Frank
Post by Vladimir Grigoriev
I found the following code and I have doubts that it is valid
char *parm = NULL;
....
if (strcmp(parm, NULL))
bla,bla,bla
[...]
Post by Eliot Frank
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

Loading...