[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: programming



On Mon, 5 Nov 2001 07:15:40 -0800 (PST), Andrew Agno <agno@AI.SRI.COM> wrote:

> J.A.Serralheiro writes:
>  > right. It should be strlen(buff )
> 
> Just a word of warning: strlen can be a problem if the buffer is not
> filled with stuff that you can validate--ie: if buff isn't a NULL
> terminated string, then it looks for the NULL byte off the end of the
> buffer, which can lead to much strangeness, and probably core dumps,
> later on.  So even if you get a length using strlen, you should make
> sure it doesn't go beyond the end of the buffer.

fgets() is guaranteed to NUL terminate the array provided it returns
a pointer other than NULL.  Otherwise, it will leave the buffer
unchanged.  If you can't guarantee the array is NUL terminated
before a call to strlen(), you shouldn't use it -- and you should
fix the string as all functions dealing with strings follow that
convention.

Technically it's NUL ('\0' or 0) vs. NULL ((void*)0), although there's
not much practical difference (Warning: converting pointer to integer...).

-- 
Eric G. Miller <egm2@jps.net>



Reply to: