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: Wed, 21 Sep 2022 17:55:50 -0700 Organization: None to speak of Lines: 60 Message-ID: <87wn9wdy7d.fsf@nosuchdomain.example.com> References: <87o7v8zc4x.fsf@bsb.me.uk> <87czboz48n.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain Injection-Info: reader01.eternal-september.org; posting-host="b6dd3111f3087e08fb468b174c9193d6"; logging-data="2051493"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/5ZCQYRK7Nyx05vvsNgVL1" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:voQsakn0ftAnQZoTdr721WQ5zfU= sha1:79M1yUewreBajGPjJst8mwPagYc= Xref: csiph.com comp.lang.c++:86478 Ben Bacarisse writes: > Richard Damon writes: > >> On 9/21/22 4:49 PM, Ben Bacarisse wrote: >>> 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. >> >> Yes, it is to copy a "C String" (Null Terminated) into a fixed width >> field. > > Again, a quibble: not quite. A null will be respected, but there is no > need for the source to be a C string. You're right (and I misstated it in a recent post). The standard (I'll quote N1570 because I have it open) says: The strncpy function copies not more than n characters (characters that follow a null character are not copied) from the array pointed to by s2 to the array pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined. Which means that the source doesn't have to point to a C string. The Linux Programmer's Manual man page incorrectly suggests that the source has to be a pointer to a C string: The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer pointed to by dest. [...] The strncpy() function is similar, except that at most n bytes of src are copied. The phrase "the string pointed to by src" in the description of strcpy implies that the behavior is undefined if src doesn't point to a string. The man page incorrectly implies that same wording applies to strncpy. -- 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 */