From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753058AbbJSMmj (ORCPT ); Mon, 19 Oct 2015 08:42:39 -0400 Received: from mail-lb0-f175.google.com ([209.85.217.175]:33040 "EHLO mail-lb0-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751741AbbJSMmh (ORCPT ); Mon, 19 Oct 2015 08:42:37 -0400 From: Rasmus Villemoes To: Ingo Molnar Cc: Linus Torvalds , Chris Metcalf , open list , Peter Zijlstra , Thomas Gleixner , "H. Peter Anvin" , Borislav Petkov Subject: Re: [PATCH] string: Improve the generic strlcpy() implementation Organization: D03 References: <55F1DD53.1070102@ezchip.com> <20151005112700.GA1096@gmail.com> X-Hashcash: 1:20:151019:bp@alien8.de::Zh5DORKcUrjB5+Xr:0000007+l X-Hashcash: 1:20:151019:linux-kernel@vger.kernel.org::SiFhUdWHmmJ8ALY1:0000000000000000000000000000000000e80 X-Hashcash: 1:20:151019:cmetcalf@ezchip.com::tF10S8T8SkbN0thb:0000000000000000000000000000000000000000001CsG X-Hashcash: 1:20:151019:mingo@kernel.org::M9sd596WswLATJ/s:01z7R X-Hashcash: 1:20:151019:tglx@linutronix.de::lqRHw8tY7vENa7N4:00000000000000000000000000000000000000000003HYF X-Hashcash: 1:20:151019:hpa@zytor.com::vcPeQrwhHuJ+8rEP:00003Faw X-Hashcash: 1:20:151019:torvalds@linux-foundation.org::4TKwRgnPijkotkuq:000000000000000000000000000000003yXG X-Hashcash: 1:20:151019:a.p.zijlstra@chello.nl::FshfGQT6rCi5riKU:0000000000000000000000000000000000000005n3l Date: Mon, 19 Oct 2015 14:42:34 +0200 In-Reply-To: <20151005112700.GA1096@gmail.com> (Ingo Molnar's message of "Mon, 5 Oct 2015 13:27:00 +0200") Message-ID: <87r3krowqt.fsf@rasmusvillemoes.dk> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Oct 05 2015, Ingo Molnar wrote: > * Linus Torvalds wrote: > >> On Thu, Sep 10, 2015 at 8:43 PM, Chris Metcalf wrote: >> > >> > Please pull the following changes for 4.3 from: >> > >> > git://git.kernel.org/pub/scm/linux/kernel/git/cmetcalf/linux-tile.git strscpy >> >> So I finally pulled it. I like the patch, I like the new interface, >> but despite that I wasn't really sure if I wanted to pull it in - thus >> the long delay of me just seeing this in my list of pending pulls for >> almost a month, but never really getting to the point where I decided >> I want to commit to it. > > Interesting. I noticed that strscpy() says this in its comments: > > * In addition, the implementation is robust to the string changing out > * from underneath it, unlike the current strlcpy() implementation. Apologies for beating a dead horse, but: c = *(unsigned long *)(src+res); if (has_zero(c, &data, &constants)) { data = prep_zero_mask(c, data, &constants); data = create_zero_mask(data); *(unsigned long *)(dest+res) = c & zero_bytemask(data); return res + find_zero(data); } *(unsigned long *)(dest+res) = c; I wonder whether an insane compiler might actually reload c before storing to dest+res, so that we'd have exactly the same problem of embedded nul characters? And similarly in the byte-by-byte case: char c; c = src[res]; dest[res] = c; if (!c) return res; (yes, there's a store between the load and the test, and the compiler probably can't know or prove that src and dest don't alias - but we're storing c, so a sufficiently clever _and_ insane compiler could test src[res]). Would READ_ONCE be justified? Rasmus