From: Christophe LEROY Subject: Re: [PATCH v2 2/2] kobject: Fix -Wstringop-truncation warning Date: Mon, 25 Jun 2018 15:32:35 +0200 Message-ID: References: <20180625124538.21051-1-shorne@gmail.com> <20180625124538.21051-3-shorne@gmail.com> <20180625132445.GI24595@lianli.shorne-pla.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Cc: LKML , Greg KH , arnd@arndb.de, Eric Biggers , linux-crypto@vger.kernel.org To: Stafford Horne Return-path: In-Reply-To: <20180625132445.GI24595@lianli.shorne-pla.net> Content-Language: fr Sender: linux-kernel-owner@vger.kernel.org List-Id: linux-crypto.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) = '/'; >>> } >>>