NULL to strlen function

S

sam_cit

Hi Everyone,

I tried the following program unit in Microsoft Visual c++ 6.0 and the
program caused unexpected behavior,

#include <stdio.h>
#include <string.h>

int main()
{
char *p;
p = NULL;
printf("%d\n",strlen(p));
}

Is it that strlen function can't handle NULL pointers?
 
R

Richard Heathfield

(e-mail address removed) said:
Hi Everyone,

I tried the following program unit in Microsoft Visual c++ 6.0 and the
program caused unexpected behavior,

Undefined behaviour.
#include <stdio.h>
#include <string.h>

int main()
{
char *p;
p = NULL;
printf("%d\n",strlen(p));
}

Is it that strlen function can't handle NULL pointers?

Or is it that strlen returns size_t and your format specifier requires int?

It is impossible to tell which of these two produces the undefined behaviour
you have noticed.

Any good C book will explain the semantics of strlen.
 
B

Barry

Hi Everyone,

I tried the following program unit in Microsoft Visual c++ 6.0 and the
program caused unexpected behavior,

#include <stdio.h>
#include <string.h>

int main()
{
char *p;
p = NULL;
printf("%d\n",strlen(p));
}

Is it that strlen function can't handle NULL pointers?

My interpretation would be by definition strlen cannot
properly handle a NULL pointer.

Returns

The strlen function returns the number of characters that precede the
terminating null
character.
 
F

Francois Grieu

I tried the following program unit in Microsoft Visual c++ 6.0
and the program caused unexpected behavior,

#include <stdio.h>
#include <string.h>

int main()
{
char *p;
p = NULL;
printf("%d\n",strlen(p));
}
Is it that strlen function can't handle NULL pointers?

There is no requirement that it does.
NULL is not a valid argument for a char* or const char*
in most functions of <string.h>.
This is hinted in by the lack of explict allowance for
NULL in 7.21.1 (String functions conventions) of
ISO/IEC 9899:1999

For an empty string, try
p = "";


Francois Grieu
 
B

Barry

Francois Grieu said:
#include <stdio.h>
#include <string.h>

int main()
{
char *p;
p = NULL;
printf("%d\n",strlen(p));
}


There is no requirement that it does.
NULL is not a valid argument for a char* or const char*
in most functions of <string.h>.
This is hinted in by the lack of explict allowance for
NULL in 7.21.1 (String functions conventions) of
ISO/IEC 9899:1999

For an empty string, try
p = "";


Francois Grieu

This doesn't need to be hinted by anything.
strlen() returns a size_t i.e. an unsigned integer.
 
C

Chris Dollin

Barry said:
This doesn't need to be hinted by anything.
strlen() returns a size_t i.e. an unsigned integer.

Even if that is fixed, it doesn't matter. `strlen` is a
Standard library function, and hence, unless otherwise
specified, is undefined when a pointer argument is null.

As someone else said: NULL isn't a string. So asking for
its string-length is like asking custard for its display
resolution.
 
B

Barry

Chris Dollin said:
Even if that is fixed, it doesn't matter. `strlen` is a
Standard library function, and hence, unless otherwise
specified, is undefined when a pointer argument is null.

As someone else said: NULL isn't a string. So asking for
its string-length is like asking custard for its display
resolution.

The OP often posts questions that demonstrate he/she
hasn't applied what he/she should have taken away from
previous responses.

I thought (perhaps mistakenly) the question was trying to
be more thoughtful and the OP meant to ask something like:

Why doesn't strlen check for a NULL pointer and return.
 
C

Clark S. Cox III

Barry said:
I thought (perhaps mistakenly) the question was trying to be more
thoughtful and the OP meant to ask something like:

Why doesn't strlen check for a NULL pointer and return.

If I pass NULL to strlen, then it was a mistake; simply returning would
only server to mask that error, and potentially hide it until my product
was already released. If strlen attempts to dereference NULL then it
will interrupt the program (on every implementation that I use, and yes,
I test this assumption before relying on it on every platform for which
I code).

I prefer my errors to be caught *before* release as much as possible.
 
D

David T. Ashley

Hi Everyone,

I tried the following program unit in Microsoft Visual c++ 6.0 and the
program caused unexpected behavior,

#include <stdio.h>
#include <string.h>

int main()
{
char *p;
p = NULL;
printf("%d\n",strlen(p));
}

Is it that strlen function can't handle NULL pointers?

You're apparently a new programmer, so let me add this:

A NULL pointer and a string of length zero are not the same thing.

A NULL pointer traditionally has the value of 0, which is usually an
unsuitable pointer; but in any case it is a reserved value defined to be a
non-functional pointer.

A string of length 0 has a non-NULL pointer, but the area of memory that is
pointed to has its first byte as 0.

The two are very different.
 
C

Christopher Benson-Manica

Clark S. Cox III said:
If I pass NULL to strlen, then it was a mistake

On the contrary, I think it's certainly possible to imagine code that
could profitably take advantage of the hypothetical ability of
strlen() to deal reasonably with a null pointer. Given how the
language is defined, of course, it's a mistake.
only server to mask that error, and potentially hide it until my product
was already released. If strlen attempts to dereference NULL then it
will interrupt the program (on every implementation that I use, and yes,
I test this assumption before relying on it on every platform for which
I code).

If you find a platform for which your assumption does not hold, what
are your plans for testing your code?
 
D

David T. Ashley

Christopher Benson-Manica said:
On the contrary, I think it's certainly possible to imagine code that
could profitably take advantage of the hypothetical ability of
strlen() to deal reasonably with a null pointer. Given how the
language is defined, of course, it's a mistake.


If you find a platform for which your assumption does not hold, what
are your plans for testing your code?

I think Clark's comments are reasonable. It is no different than generously
using assert(), except he is relying on the [hardware/OS] platform to check
the assertions.
 
B

Barry

Clark S. Cox III said:
If I pass NULL to strlen, then it was a mistake; simply returning would
only server to mask that error, and potentially hide it until my product
was already released. If strlen attempts to dereference NULL then it
will interrupt the program (on every implementation that I use, and yes,
I test this assumption before relying on it on every platform for which
I code).

I prefer my errors to be caught *before* release as much as possible.

Your inability to read an entire message or thread and reply is
laughable.
 
B

Barry

Clark S. Cox III said:
If I pass NULL to strlen, then it was a mistake; simply returning would
only server to mask that error, and potentially hide it until my product
was already released. If strlen attempts to dereference NULL then it
will interrupt the program (on every implementation that I use, and yes,
I test this assumption before relying on it on every platform for which
I code).

I prefer my errors to be caught *before* release as much as possible.

The NEXT time you choose to pick a sentence out of context you
should properly apologize in the same forum.
 
C

Clark S. Cox III

Christopher said:
On the contrary, I think it's certainly possible to imagine code that
could profitably take advantage of the hypothetical ability of
strlen() to deal reasonably with a null pointer. Given how the
language is defined, of course, it's a mistake.

Other than assert'ing that the parameter is non-NULL, what would you
consider a reasonable way for strlen to deal with NULL?
If you find a platform for which your assumption does not hold, what
are your plans for testing your code?

If I find a platform where that assumption doesn't hold, then, as I said
above, I won't rely on it. The last platform on which I did any
extensive coding that didn't, upon a read from NULL, cause programs to
immediately crash, interrupt, drop into a debugger, etc. was the old
(i.e. pre MacOSX, pre-VM) MacOS. However, even there, it was a simple
matter to configure the debugger to watch for access to that memory
location.
 
C

Clark S. Cox III

Barry said:
The NEXT time you choose to pick a sentence out of context you
should properly apologize in the same forum.

What sentence did I take out of context? (I don't mean to sound snarky,
I am honestly confused as to how you think that I misconstrued what you
said). You stated what you thought the OP meant to ask, and I answered
that question.

Additionally, I am not sure why you responded to me twice.
 
C

Christopher Benson-Manica

Clark S. Cox III said:
Other than assert'ing that the parameter is non-NULL, what would you
consider a reasonable way for strlen to deal with NULL?

To me, it wouldn't strike me as unreasonable for strlen(NULL) to
return 0. I presume the reasons that it doesn't are a) performance, and
b) not all the other str* functions could have reasonable behavior
when passed NULL, so none of them do.
If I find a platform where that assumption doesn't hold, then, as I said
above, I won't rely on it. The last platform on which I did any
extensive coding that didn't, upon a read from NULL, cause programs to
immediately crash, interrupt, drop into a debugger, etc. was the old
(i.e. pre MacOSX, pre-VM) MacOS. However, even there, it was a simple
matter to configure the debugger to watch for access to that memory
location.

That's reasonable, I was just curious.
 
I

Ingo Menger

Barry said:
Why doesn't strlen check for a NULL pointer and return.

Return WHAT?
Perhaps 0, so the caller is led to believe that there is an empty
string?
Or -1. No wait, this is an unsigned return type, so it couldn't.
How about 999999999?
Surely, nobody ever has such a long string?
 
N

Nelu

Barry said:
Why doesn't strlen check for a NULL pointer and return.

C99, 7.21.6.3-3: "The strlen function returns the number of characters
that precede the terminating null character."

I don't think it's possible to return any value such that it satisfies
this condition if NULL is passed to the function. The return value is
size_t so using negative values is not possible.
 
C

Clever Monkey

David said:
You're apparently a new programmer, so let me add this:

A NULL pointer and a string of length zero are not the same thing.

A NULL pointer traditionally has the value of 0, which is usually an
unsuitable pointer; but in any case it is a reserved value defined to be a
non-functional pointer.

A string of length 0 has a non-NULL pointer, but the area of memory that is
pointed to has its first byte as 0.

The two are very different.
Good description.

I'll add to this by stating that folks often conflate the null pointer
and the null (or NUL, if that's how you roll) byte at the end of a C-string.

I can see that one might assume that a "null string" and a "null pointer
to an empty string" would have correspondence. The term "null" is used
in quite an overloaded manner in casual conversation, and it can be easy
to conflate the two.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top