Received: by 10.223.185.116 with SMTP id b49csp3248533wrg; Mon, 12 Feb 2018 23:24:34 -0800 (PST) X-Google-Smtp-Source: AH8x226h4whRGkFC8FtsmB+F40c3LmeiBDwBxpFOrAezKBPIvKFqRcctLB8w3WmlA72rq3FyzC1M X-Received: by 10.98.159.200 with SMTP id v69mr310187pfk.236.1518506674524; Mon, 12 Feb 2018 23:24:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518506674; cv=none; d=google.com; s=arc-20160816; b=Q1hSFGRk/ir3kustl5/T7CRSuSgyHIiz1Nshj60XO8/Sk1EXbueBPoJNkDwjA05DJn 0dECnL+vyMRsFb0DWdD0NsJjLLxSajJIpHJ4nwdnyGu63vdIAgoUahEld56ABl5HDzEI KdCtTn/nJXfte35rCFeJkzF7jrz/ylGQSmQT7Ju2QkKsLBNc2lmn3Bwjphv+Ohr5TS9q H7lPBvSLU/pee0XjcMJ4gLdl2ECZOIkCCyRVb1YcRMP/khZsgdk9ZGL5uQKdHoKPVIIC UEQduRX91o1gg2qvchvx+lpVeKgfbx21c2fZZ+4FKUfuYmu3t1FrJKWmlvMoQyO8nKuV q6OA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=aalgxxu2tXxLhh54pSm+/nP8rwWRkJPJiTouZSSAhyA=; b=x/O0z1dPUBMTU42Fnbq4RUokATdSEqn1VT4zDy+RHcOvQRerdyAg64+HVSapGAziNL x9BtiacO5Qlbqr1vdhenh91hF4moPLGXh2tGGbimyaMi+CPNuM3H41kW8OJv254NzVY9 RTbfsG5eBmOKnzGddmu/juRFCNW+S+ej8h8uf+W9K1p1PWV2DppcaVwjtJgrIauc+sJw 0cQefofGIubFkulLkWM4GEiJDJYhtrJjaRUqj6qTEjArh7RwLKGR7LFV9oozzI2Vzczx NrEOo6/JFZNWDnx7gZkNLdyZQuSzxzYtopbppg/cC5RWL2ewVZ1YECoyMruMSMsjYeOa PqKw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c3si4850681pfi.404.2018.02.12.23.24.19; Mon, 12 Feb 2018 23:24:34 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933524AbeBMHXk (ORCPT + 99 others); Tue, 13 Feb 2018 02:23:40 -0500 Received: from wtarreau.pck.nerim.net ([62.212.114.60]:40568 "EHLO 1wt.eu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933426AbeBMHXj (ORCPT ); Tue, 13 Feb 2018 02:23:39 -0500 Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id w1D7NSgn001645; Tue, 13 Feb 2018 08:23:28 +0100 Date: Tue, 13 Feb 2018 08:23:28 +0100 From: Willy Tarreau To: Xiongfeng Wang Cc: Miguel Ojeda , Dan , Arnd Bergmann , linux-kernel Subject: Re: [PATCH V2] auxdisplay: use correct string length Message-ID: <20180213072328.GA1636@1wt.eu> References: <1516095490-83827-1-git-send-email-wangxiongfeng2@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.6.1 (2016-04-27) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 13, 2018 at 09:19:21AM +0800, Xiongfeng Wang wrote: > >> diff --git a/drivers/auxdisplay/panel.c b/drivers/auxdisplay/panel.c > >> index ea7869c..d288900 100644 > >> --- a/drivers/auxdisplay/panel.c > >> +++ b/drivers/auxdisplay/panel.c > >> @@ -1506,10 +1506,10 @@ static struct logical_input *panel_bind_key(const char *name, const char *press, > >> key->rise_time = 1; > >> key->fall_time = 1; > >> > >> - strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str)); > >> - strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str)); > >> + strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str) - 1); > >> + strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str) - 1); > >> strncpy(key->u.kbd.release_str, release, > >> - sizeof(key->u.kbd.release_str)); > >> + sizeof(key->u.kbd.release_str) - 1); > > > > Are you sure about this patch? `kbd` says "strings can be non null-terminated". > > > > Willy, maybe those should just be memcpy()s? (unless the remaining > > bytes, if any, must be 0). > Sorry, my apologies. I think I made a mistake. I meant to use strlcpy(), but this > also decrease the destination storage size by one. > I think, if the strings can be non null-terminated, we can just use memcpy(). Well, memcpy() needs as much data in as out. strncpy() does exactly what was apparently needed there : take at most X chars from a given string, and write exactly X on the output, possibly padding with zeroes. We sure can stop using strncpy() and reimplement it, but that becomes ridiculous. One day gcc will tell us that an "if" statement misses an "else" which is probably an error and we'll have to put "else dummy();" everywhere in the code to calm it. > This may suppress the gcc warning. In fact there are two big problems with gcc warnings : - writing -Wno-something-unknown triggers an error on versions where "something-unknown" isn't known, making it difficult to permanently disable warnings (though in the kernel we handle this pretty fine) - warnings and diagnostics are conflated. Some warnings should only be provided when the developer explicitly asks for suspicious stuff (-Wsecurity, -Wsuspicious, etc) that would *not* be part of -Wall since -Wall is usually used to report real warnings. My preferred one today is the one by which gcc reads your *comments* to figure whether you forgot a break in a case statement... Regards, Willy