Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1030785AbbD1RkB (ORCPT ); Tue, 28 Apr 2015 13:40:01 -0400 Received: from zeniv.linux.org.uk ([195.92.253.2]:43518 "EHLO ZenIV.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1030304AbbD1RkA (ORCPT ); Tue, 28 Apr 2015 13:40:00 -0400 Date: Tue, 28 Apr 2015 18:39:57 +0100 From: Al Viro To: Fabian Frederick Cc: linux-kernel@vger.kernel.org, Linus Torvalds Subject: Re: revert "fs/befs/linuxvfs.c: replace strncpy by strlcpy" Message-ID: <20150428173957.GK889@ZenIV.linux.org.uk> References: <20150428034859.GI889@ZenIV.linux.org.uk> <1363736428.511175.1430199310500.open-xchange@webmail.nmp.proximus.be> <20150428160523.GJ889@ZenIV.linux.org.uk> <146824702.30907.1430239330750.open-xchange@webmail.nmp.proximus.be> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <146824702.30907.1430239330750.open-xchange@webmail.nmp.proximus.be> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2299 Lines: 74 On Tue, Apr 28, 2015 at 06:42:10PM +0200, Fabian Frederick wrote: > > > > On 28 April 2015 at 18:05 Al Viro wrote: > > > > > > On Tue, Apr 28, 2015 at 07:35:10AM +0200, Fabian Frederick wrote: > > > > > > Al, very unhappy about the prospect of looking through ~2000 calls of > > > > strlcpy() > > > > we have in the tree... > > > > > > Sorry Al, I thought it was more secure. > > > > It's not just you, unfortunately, and dumping all that annoyance on you > > as a proxy for everyone who does that kind of thing had been unfair. > > My apologies... > > No problem Al :) but why can't we harden strlcpy at first with > something like a strlen limited to max char. > (I don't know if it's already in kernel libs). > > size_t strlenl(const char *s, size_t maxlen) aka strnlen() > ? ? ? ? const char *sc = s; > ? ? ? ? size_t i = 0; > > ? ? ? ? while (*sc != '\0' && (i < maxlen)) { > ? ? ? ? ? ? ? ? i++; > ? ? ? ? ? ? ? ? sc++; > ? ? ? ? } > ? ? ? ? return sc - s; > } > > Then we could solve problems downstream ... Can't. Seriously, look what strlcpy() is supposed to return; it's pretty much a microoptimized snprintf(dst, size, "%s", src). It's certainly been patterned after snprintf(3) - "don't exceed that size, NUL-terminate unless the size is zero, return the number of characters (excluding NUL) that would've been written if the size had been large enough". The following is a legitimate use of strlcpy(): int foo(char *); /* modifies string */ int const_foo(const char *s) { int res; char buf[32], *p = buf; size_t wanted = strlcpy(buf, sizeof(buf), s); if (wanted >= sizeof(buf)) { p = malloc(wanted + 1); if (!p) return -ENOMEM; memcpy(p, s, wanted + 1); } res = foo(p); if (p != buf) free(p); return res; } None of the kernel callers are of exactly that form (and most ignore the return value completely), but if we make that sucker return something different from what strlcpy(3) would return, we'd damn better _not_ keep the name; there's enough confusion in that area as it is. -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/