Need strnlen() (???)
Joseph S. D. Yao
jsdy at hadron.UUCP
Wed Nov 27 17:44:13 AEST 1985
In article <715 at dedalus.UUCP> roger at dedalus.UUCP (Roger L. Cordes Jr.) writes:
> What is the big deal here? How about:
>extern int strlen();
>static int _nstrnlen_; /* to avoid two calls to strlen() */
>#define strnlen(S,N) ( (_nstrnlen_=strlen(S)) > (N) ? (N) : _nstrnlen_ )
Once again: doing a strlen() on something not guaranteed to be
NUL-terminated is guaranteed to net you a core dump sooner or later.
You can declare str to be char str[MAXSIZE+1] and always do a two-step:
str[MAXSIZE] = NUL; n = strlen(str); or you can use the deplorable
function I put on the net to try and end this line of discussion; or
you can give the people who use your code fun trying to figure out
"Oh no! How did I break the computer? Where did the core dump, and
can I sweep it under the rug before morning?" ;-)
Incidentally, Doug's suggestion to use memchr() is great, once we
all have ANSI compilers. ;-}
--
Joe Yao hadron!jsdy at seismo.{CSS.GOV,ARPA,UUCP}
More information about the Comp.lang.c
mailing list