Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.c++ Subject: Re: Never use strncpy! Date: Wed, 21 Sep 2022 21:49:18 +0100 Organization: A noiseless patient Spider Lines: 24 Message-ID: <87o7v8zc4x.fsf@bsb.me.uk> References: MIME-Version: 1.0 Content-Type: text/plain Injection-Info: reader01.eternal-september.org; posting-host="5f56c02635115ece86879230cc5e5216"; logging-data="2008014"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+uczo4onB23Bjz+DiT9c8OR9s1u+1ROwk=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) Cancel-Lock: sha1:U3H4HikgdmgHSq3UpDD9otTxI/M= sha1:IdnvDyUtpz/RYtofoPmS2HfE4ws= X-BSB-Auth: 1.4a2687decb7eae5d7025.20220921214918BST.87o7v8zc4x.fsf@bsb.me.uk Xref: csiph.com comp.lang.c++:86472 Andrey Tarasevich writes: > On 9/21/2022 3:04 AM, Juha Nieminen wrote: >> I always thought that std::strncpy() works exactly like std::strcpy(), >> except that it stops early if the specified count is reached. Turns out >> that I was gravely mistaken: >> > > The matter has been explained, explained and over-explained to death > already, including here in comp.lang.* newsgroups. It is well-known > that `strncpy` has never been intended as a "safe string copying" > function. It is a niche function introduced for so called > "fixed-width" string support. > > https://stackoverflow.com/questions/2886931/difference-fixed-width-strings-and-zero-terminated-strings > > It has never been intended for use with zero-terminated strings. Except for the quibble that a null in the source string is respected -- i.e. the destination is considered to be a fixed-width field but not the source. -- Ben.