Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp3902248imm; Mon, 25 Jun 2018 06:34:47 -0700 (PDT) X-Google-Smtp-Source: ADUXVKI4FEGK+Fhpu0OrFuh54dWUvc/UCsf+y+8AIC4M95H6OwD2AcjvLRLz8jkISC/efYYDcIH9 X-Received: by 2002:a62:da07:: with SMTP id c7-v6mr13128051pfh.106.1529933687649; Mon, 25 Jun 2018 06:34:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529933687; cv=none; d=google.com; s=arc-20160816; b=XFQVwyIAwdx36Ot+V5Kr8x59FkURYGdtEuOHX3QNypkciCs40Rgrcq/KyJlkGMFXVK tlscoemb4BZqhQQd6suzeDq1MpFVwxo/Csu1GApc+K/E3bCB8zo3J+TFgGuWUE1uozjV tsnCjeVlQVinZqurgIxODpg6HLM+wwePfRpEB7UhvldtgSdErFHhFVgNLX7AcMK+0aYL 25Mkq5eZhB60if5BnOcWhPB0FmNbKC4DYieWpE+koFwOBEgaZlsEdKKe9GesRW/1e4/d 8XnjdyQi1gKT5U7DzNmRVs3YY4pVnM0gpA5YZsr0kux3+AGLOGYDfYj5EKg1aQAdNS2q mLvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:arc-authentication-results; bh=+1FoSzuiR7/I81OvpJnaQcmP/oHvoCrOP7+OjrvIOak=; b=O2BmPdWRvh/Xygr0iyOe/MpRfaMVqNdlpxlOAA7lYnyv5C8wuT/VChTzWRkAeXO8Au GpYzJd/T6yxtgncENrOgN4OkCn/lkxA87E8pnXxq28xqAa0pov1sqoojIOMVGg7RCm9S usj7BbZq0zXKl6HdwQpOYBChOAMc/iYEel4duED+iY9b2vtlvZNb24sdo3plakHmmsb3 t5VQ9Lj279+zg5QfxD4fTe3G+NH6f0pvcjLjYGqXDoTrJwkLXcbGdEUN3Th691lfJA0k hRMDo2g2GtoNV9450o7NCOStcCkd9iSaZot0IyZJEVvqBHkZT7w2mmlK6zp5IRCg1ZA1 FtFg== 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 a10-v6si14139454pls.480.2018.06.25.06.34.33; Mon, 25 Jun 2018 06:34:47 -0700 (PDT) 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 S934118AbeFYNcv (ORCPT + 99 others); Mon, 25 Jun 2018 09:32:51 -0400 Received: from pegase1.c-s.fr ([93.17.236.30]:21928 "EHLO pegase1.c-s.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933785AbeFYNct (ORCPT ); Mon, 25 Jun 2018 09:32:49 -0400 Received: from localhost (mailhub1-int [192.168.12.234]) by localhost (Postfix) with ESMTP id 41Dqq04v1Nz9txd0; Mon, 25 Jun 2018 15:32:28 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at c-s.fr Received: from pegase1.c-s.fr ([192.168.12.234]) by localhost (pegase1.c-s.fr [192.168.12.234]) (amavisd-new, port 10024) with ESMTP id M-Ht8ZYJGn8i; Mon, 25 Jun 2018 15:32:28 +0200 (CEST) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase1.c-s.fr (Postfix) with ESMTP id 41Dqq04JRsz9txcm; Mon, 25 Jun 2018 15:32:28 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id D4DB78B7A0; Mon, 25 Jun 2018 15:32:35 +0200 (CEST) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id RWhLy7_ecLVj; Mon, 25 Jun 2018 15:32:35 +0200 (CEST) Received: from PO15451 (po15451.idsi0.si.c-s.fr [172.25.231.35]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 6DEAF8B7C2; Mon, 25 Jun 2018 15:32:35 +0200 (CEST) Subject: Re: [PATCH v2 2/2] kobject: Fix -Wstringop-truncation warning To: Stafford Horne Cc: LKML , Greg KH , arnd@arndb.de, Eric Biggers , linux-crypto@vger.kernel.org References: <20180625124538.21051-1-shorne@gmail.com> <20180625124538.21051-3-shorne@gmail.com> <20180625132445.GI24595@lianli.shorne-pla.net> From: Christophe LEROY Message-ID: Date: Mon, 25 Jun 2018 15:32:35 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: <20180625132445.GI24595@lianli.shorne-pla.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: fr Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Le 25/06/2018 à 15:24, Stafford Horne a écrit : > On Mon, Jun 25, 2018 at 02:57:13PM +0200, Christophe LEROY wrote: >> >> >> Le 25/06/2018 à 14:45, Stafford Horne a écrit : >>> When compiling with GCC 9.0.0 I am seeing the following warning: >>> >>> In function ‘fill_kobj_path’, >>> inlined from ‘kobject_get_path’ at lib/kobject.c:155:2: >>> lib/kobject.c:128:3: warning: ‘strncpy’ output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation] >>> strncpy(path + length, kobject_name(parent), cur); >>> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >>> lib/kobject.c: In function ‘kobject_get_path’: >>> lib/kobject.c:125:13: note: length computed here >>> int cur = strlen(kobject_name(parent)); >>> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ >>> >>> This is not really an issue since the buffer we are writing to is >>> pre-zero'd and we have already allocated the buffer based on the >>> calculated strlen size and accounted for the terminating '\0'. >>> Just use memcpy() instead. >> >> If we are already sure the destination is big enough, why not just do a >> strcpy() and drop the 'cur = strlen()' ? > > Hi Christophe, > > Here were are writing multiple strings into a buffer from back to front. We are > copying exactly strlen() bytes at a time to avoid the nul terminator being > copied into the buffer. > > I don't doubt we could use strcpy() but I was trying to keep the change small. Ok, fair enough. Reviewed-by: Christophe Leroy > > -Stafford > >>> >>> Cc: Greg Kroah-Hartman >>> Cc: Arnd Bergmann >>> Cc: Eric Biggers >>> Signed-off-by: Stafford Horne >>> --- >>> lib/kobject.c | 2 +- >>> 1 file changed, 1 insertion(+), 1 deletion(-) >>> >>> diff --git a/lib/kobject.c b/lib/kobject.c >>> index 18989b5b3b56..e876957743c8 100644 >>> --- a/lib/kobject.c >>> +++ b/lib/kobject.c >>> @@ -125,7 +125,7 @@ static void fill_kobj_path(struct kobject *kobj, char *path, int length) >>> int cur = strlen(kobject_name(parent)); >>> /* back up enough to print this name with '/' */ >>> length -= cur; >>> - strncpy(path + length, kobject_name(parent), cur); >>> + memcpy(path + length, kobject_name(parent), cur); >>> *(path + --length) = '/'; >>> } >>>