Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: How to copy a string safely and efficiently in C? Date: Sun, 04 Sep 2016 23:20:18 -0700 Organization: None to speak of Lines: 22 Message-ID: References: <0b30b0cc-f7c5-4928-8acb-56bf2364797b@googlegroups.com> <87fupitz86.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="12285"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ngIYD4LSqcl5B3PNIU8jq" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:QLgupiRyBd9AJLKUAk5Zp6/7bCQ= sha1:SeV9F1jm+tLG29XKHRQ86MKCOHE= Xref: csiph.com comp.lang.c:93204 Tim Rentsch writes: > Ben Bacarisse writes: [...] >> char copy[STR_SIZE]; >> *copy = 0; >> strncat(copy, src, STR_SIZE - 1); > > Or maybe this (with the same declaration of 'copy') > > strncat( strcpy( copy, "" ), src, STR_SIZE-1 ); Interesting. It's easy to forget that strcpy() returns a result, since that result is rarely useful and usually ignored. (I'm resisting the temptation to repeat the warning that simple truncation isn't always the correct response to an overflow.) -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"