Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site hadron.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxn!ihnp4!qantel!lll-crg!seismo!rlgvax!hadron!jsdy From: jsdy@hadron.UUCP (Joseph S. D. Yao) Newsgroups: net.lang.c Subject: Re: Need strnlen() (???) Message-ID: <93@hadron.UUCP> Date: Wed, 27-Nov-85 02:44:13 EST Article-I.D.: hadron.93 Posted: Wed Nov 27 02:44:13 1985 Date-Received: Fri, 29-Nov-85 21:19:32 EST References: <715@dedalus.UUCP> Reply-To: jsdy@hadron.UUCP (Joseph S. D. Yao) Organization: Hadron, Inc., Fairfax, VA Lines: 20 Keywords: segmentation violation rah rah rah Summary: Stamp out core dumps! In article <715@dedalus.UUCP> roger@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@seismo.{CSS.GOV,ARPA,UUCP}