From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6A0AFC32772 for ; Tue, 23 Aug 2022 09:46:34 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 29DC7845FF; Tue, 23 Aug 2022 11:46:32 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=metanate.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=metanate.com header.i=@metanate.com header.b="qH22t08y"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 010AA845FA; Tue, 23 Aug 2022 11:46:30 +0200 (CEST) Received: from metanate.com (unknown [IPv6:2001:8b0:1628:5005::111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id C1D9884600 for ; Tue, 23 Aug 2022 11:46:26 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=metanate.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=john@metanate.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=metanate.com; s=stronger; h=In-Reply-To:Content-Transfer-Encoding: Content-Type:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-ID:Content-Description; bh=R4olrkpKosRvxjw3E5scQu9I1UYm+ETSXcUAkWMlLVU=; b=qH22t08ycsL+c0wDhgxrf3d38q DLBEi0rOeohdH0tkchyVBgnj7s2j4TQ6qV9hsmqpbi2f9OUZVnC2gTYHNp60m008Riji1x3mDf8xX zwYCzTZIUbkbkYR4As2CgH6Pi16rijG9uigqmfRlqTM1Qg1QmH26qD7MktPIi/lVZU3a0WXwZtg0k t/9Qi4zhQuDHMIx7q/39v5RnI8lUGhwAY0KFyiQIYEXBAYB8rfdrsGDRqIo/OGq8RmxPmeBuEULar s9CLULeE2vHYjx3Im8OgxdtoqtgYIT4eUihe0ysVnCI24C2HLMEfNV4SDp6WSshXUPAGuKa98ERft TdNvmFsw==; Received: from [81.174.171.191] (helo=donbot) by email.metanate.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1oQQUK-00079x-NM; Tue, 23 Aug 2022 10:46:25 +0100 Date: Tue, 23 Aug 2022 10:46:23 +0100 From: John Keeping To: Joel Stanley Cc: Simon Glass , u-boot@lists.denx.de Subject: Re: [PATCH] image: Ensure image header name is null terminated Message-ID: References: <20220823055907.416060-1-joel@jms.id.au> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20220823055907.416060-1-joel@jms.id.au> X-Authenticated: YES X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean On Tue, Aug 23, 2022 at 03:59:07PM +1000, Joel Stanley wrote: > When building with GCC 12: > > ../include/image.h:779:9: warning: ‘strncpy’ specified bound 32 equals destination size [-Wstringop-truncation] > 779 | strncpy(image_get_name(hdr), name, IH_NMLEN); > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > Ensure the copied string is null terminated by always setting the final > byte to 0. Shorten the strncpy to IH_NMLEN-1 as we will always overwrite > the last byte. > > We can't use strlcpy as this is code is built on the host as well as the > target. Since this is in the header, isn't the point that it doesn't need to be null-terminated? When printing we're careful to use: "%.*s", IH_NMLEN, ... so I think the warning is wrong here - we want both of the strncpy() behaviours that are normally considered strange: - it's okay not to null terminate as this is an explicitly sized field - we want to pad the whole field with zeroes if the string is short > Fixes: b97a2a0a21f2 ("[new uImage] Define a API for image handling operations") > Signed-off-by: Joel Stanley > --- > include/image.h | 5 ++++- > 1 file changed, 4 insertions(+), 1 deletion(-) > > diff --git a/include/image.h b/include/image.h > index e4c6a50b885f..665b2278b7fb 100644 > --- a/include/image.h > +++ b/include/image.h > @@ -776,7 +776,10 @@ image_set_hdr_b(comp) /* image_set_comp */ > > static inline void image_set_name(image_header_t *hdr, const char *name) > { > - strncpy(image_get_name(hdr), name, IH_NMLEN); > + char *hdr_name = image_get_name(hdr); > + > + strncpy(hdr_name, name, IH_NMLEN - 1); > + hdr_name[IH_NMLEN - 1] = '\0'; > } > > int image_check_hcrc(const image_header_t *hdr); > -- > 2.35.1 >