Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: Never use strncpy! Date: Fri, 30 Sep 2022 16:43:11 -0700 Organization: None to speak of Lines: 27 Message-ID: <87wn9kl980.fsf@nosuchdomain.example.com> References: <874jx0fi2w.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Info: reader01.eternal-september.org; posting-host="061fb2a259e6ca7edd5ef8a69428d610"; logging-data="1204428"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/EwtcNp49osw2ql2KD+RN2" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:B9KIvcvX0UkZwvDAZjzuxolLvqY= sha1:lVIiO0fARMCtjEywCGgTbYIPado= Xref: csiph.com comp.lang.c++:86745 Manfred writes: > On 9/22/2022 1:01 AM, Keith Thompson wrote: >> Juha Nieminen writes: >>> Well, *almost* never, at least. >>> > [...] >> strncpy() is not poorly designed. > [...] >> It is poorly *named*. > > Agreed. > > The name implies that, as strncat is a "safer" >> strcat, strncpy is a "safer" strcpy. Both strncat and strncpy let you >> specify the size of the target array, avoiding writing past the end of >> it, but strncpy treats its target as null-terminated string. >> > (likely typo, sounds like this last bit has been gotten backwards) Yes, thank you. strncat() treats its target (but not its source) as a null-terminated string, both before and after copying. strncpy() does not. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips void Void(void) { Void(); } /* The recursive call of the void */