From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752793AbbJENPa (ORCPT ); Mon, 5 Oct 2015 09:15:30 -0400 Received: from mail-wi0-f181.google.com ([209.85.212.181]:33477 "EHLO mail-wi0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751965AbbJENP2 (ORCPT ); Mon, 5 Oct 2015 09:15:28 -0400 Date: Mon, 5 Oct 2015 15:15:24 +0200 From: Ingo Molnar To: Linus Torvalds Cc: Chris Metcalf , open list , Peter Zijlstra , Thomas Gleixner , "H. Peter Anvin" , Borislav Petkov Subject: Re: [PATCH] string: Improve the generic strlcpy() implementation Message-ID: <20151005131524.GA807@gmail.com> References: <55F1DD53.1070102@ezchip.com> <20151005112700.GA1096@gmail.com> <20151005115355.GA27073@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20151005115355.GA27073@gmail.com> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org * Ingo Molnar wrote: > > * Ingo Molnar wrote: > > > 2) > > > > Another problem is that strlcpy() will also happily do bad stuff if we pass > > it a negative size. Instead of that we will from now on print a (one time) > > warning and return safely. > > Hm, so this check is buggy, as 'size_t' is unsigned - and for some reason GCC > didn't warn about the never-met comparison and the resulting unreachable dead > code here: > > > + /* Overflow check: */ > > + if (unlikely(dest_size < 0)) { > > + WARN_ONCE(1, "strlcpy(): dest_size < 0 underflow!"); > > + return strlen(src); > > + } > > which is annoying. > > Would people object to something like: > > > + /* Overflow check: */ > > + if (unlikely((ssize_t)dest_size < 0)) { > > + WARN_ONCE(1, "strlcpy(): dest_size < 0 underflow!"); > > + return strlen(src); > > + } > > ? > > As I doubt it's legit to have larger than 2GB strings. > > Also, I'm wondering why GCC didn't warn. Hm, so GCC (v4.9.2) will only warn about this bug if -Wtype-limits is enabled explicitly: lib/string.c: In function ‘strlcpy’: lib/string.c:228:32: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits] if (unlikely((size_t)dst_size < 0)) { ^ ... which we don't do in the kernel. Has anyone considered enabling -Wtype-limits? It seems to catch real bugs. I can see there are patches that enable -Wextra (which enables -Wtype-limits and many other warnings), but it would be more manageable to just enable one such warning at a time. Thanks, Ingo