Jon Grant | 10 Aug 2009 14:06

Re: linux strlen man page accepts NULL?

Hello

2009/8/7 Michael Kerrisk <mtk.manpages@...>:
> Jon,
>
> On Fri, Aug 7, 2009 at 1:55 PM, Jon Grant<jg@...> wrote:
>> Looking at this man page:
>>
>> http://linux.die.net/man/3/strlen
>>
>> Should it not mention that a NULL address is a valid param? Or is it
>> not a valid param?
>
> What makes you think it is a valid parameter?

NULL points to 0x0, which could be mapped to something. On my embedded
platform it is the beginning of the boot ROM. However typically 0x0 is
an invalid address, in which case strlen should check for NULL, and
return 0

e.g.:

size_t strlen(const char *str)
{
        const char *s;

if(str == NULL)
{
    return 0;
}
(Continue reading)

Michael Kerrisk | 20 Sep 2009 04:41

Re: linux strlen man page accepts NULL?

Hello Jon,

On Mon, Aug 10, 2009 at 2:06 PM, Jon Grant <jg@...> wrote:
> Hello
>
> 2009/8/7 Michael Kerrisk <mtk.manpages@...>:
>> Jon,
>>
>> On Fri, Aug 7, 2009 at 1:55 PM, Jon Grant<jg@...> wrote:
>>> Looking at this man page:
>>>
>>> http://linux.die.net/man/3/strlen
>>>
>>> Should it not mention that a NULL address is a valid param? Or is it
>>> not a valid param?
>>
>> What makes you think it is a valid parameter?
>
> NULL points to 0x0, which could be mapped to something.

D'oh! Sorry -- now I'm with you. Yes, it could.

> On my embedded
> platform it is the beginning of the boot ROM. However typically 0x0 is
> an invalid address, in which case strlen should check for NULL, and
> return 0

I don't think it should check for this. If the addres is invalid, it
should be treated like any other invalid address -- usually a SIGSEGV
results.
(Continue reading)

Mike Frysinger | 22 Sep 2009 08:43
Picon
Favicon
Gravatar

Re: linux strlen man page accepts NULL?

On Saturday 19 September 2009 22:41:41 Michael Kerrisk wrote:
> On Mon, Aug 10, 2009 at 2:06 PM, Jon Grant wrote:
> > On my embedded
> > platform it is the beginning of the boot ROM. However typically 0x0 is
> > an invalid address, in which case strlen should check for NULL, and
> > return 0
> 
> I don't think it should check for this. If the addres is invalid, it
> should be treated like any other invalid address -- usually a SIGSEGV
> results.

right.  if 0 is a valid address, then strlen() should work.  if it isnt valid 
and you called strlen() on it anyways, then your code sucks and it should 
crash.  POSIX does not require special handling of any address, so neither 
should Linux.
-mike

Gmane