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 8E632C32772 for ; Tue, 23 Aug 2022 13:38:47 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 7FB0184812; Tue, 23 Aug 2022 15:38:42 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="VXVTxn9O"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4D3CD84814; Tue, 23 Aug 2022 15:38:38 +0200 (CEST) Received: from mail-yw1-x112a.google.com (mail-yw1-x112a.google.com [IPv6:2607:f8b0:4864:20::112a]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 0483F84804 for ; Tue, 23 Aug 2022 15:38:34 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@google.com Received: by mail-yw1-x112a.google.com with SMTP id 00721157ae682-32a09b909f6so380287077b3.0 for ; Tue, 23 Aug 2022 06:38:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc; bh=TyeSJSWIzX+5YH/kdIKTwsYLrUL1nSa8vAOD5raTiZk=; b=VXVTxn9OoHwDKQ209tGPKkY82a9Kor1QOy9Ek+QOoRfoZrlT31Qq7yO+zFdjXjczvj yGY1BQK6QE9JAmlBJrhiQk4UweALJbPNtoYJnpfPeayKKnxjS/pqqVdTWodq4KxqeIBP jfA45KU4J7kDgJN0y9z26q9NLwYbJFrs76tig= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc; bh=TyeSJSWIzX+5YH/kdIKTwsYLrUL1nSa8vAOD5raTiZk=; b=7xk74C1W8XZRvre40OCc8BA2U2vzKFhU4SwrZzQTWoTejALR1eaYIGzbBztPsoh8Qx k743nqZ/qA1ZOXsijirr4xgn4qzuaAvCY4q+q3oTRqUFw0iVac4htHN5nFvLeLp0++it tholMAisfbUy3lKpEjtTPiJ2nBTkn6qcCi1RKpLy2DxLqkB7Yxs6av3RfqLw4zEF+7Rf 5RKvnfhUgMxsAH99dW68PEa6zo3JemO+5sOojap+NNWUOSAnGPp9nAYZFkBgUXTdddaA LEOD4+yzm20+BTRICjWwcVbJgsKV4GuLNfvDQB1pUAWkZMByoZBfMG4LivxVjpHmPcIO CxGg== X-Gm-Message-State: ACgBeo1/5APtzLIt3n+8sGjhrL3qvCGZ5UJrFCzHauUi2oNRlaezde8N e3k7t01gBFvUKv+hpqRTnnr8YMzkUKK7d0kRY8U7jw== X-Google-Smtp-Source: AA6agR69IfSiNeMYmmiI3xTYkivYqa/Va7wS114U6j0wUtBZv0Gl2JbDraxgOeGY7nno6JGfW/BOT9sQH+oR5OHlK+k= X-Received: by 2002:a81:830c:0:b0:334:1e55:6e40 with SMTP id t12-20020a81830c000000b003341e556e40mr25916065ywf.423.1661261912400; Tue, 23 Aug 2022 06:38:32 -0700 (PDT) MIME-Version: 1.0 References: <20220823055907.416060-1-joel@jms.id.au> In-Reply-To: From: Simon Glass Date: Tue, 23 Aug 2022 06:38:20 -0700 Message-ID: Subject: Re: [PATCH] image: Ensure image header name is null terminated To: John Keeping Cc: Joel Stanley , U-Boot Mailing List Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 Hi John, On Tue, 23 Aug 2022 at 03:46, John Keeping wrote: > > On Tue, Aug 23, 2022 at 03:59:07PM +1000, Joel Stanley wrote: > > When building with GCC 12: > > > > ../include/image.h:779:9: warning: =E2=80=98strncpy=E2=80=99 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 overwrit= e > > the last byte. > > > > We can't use strlcpy as this is code is built on the host as well as th= e > > 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 That's my understanding too. We are careful to avoid expecting a terminator. I am not sure what to do with the warning though Regards, Simon > > > Fixes: b97a2a0a21f2 ("[new uImage] Define a API for image handling oper= ations") > > 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 *nam= e) > > { > > - strncpy(image_get_name(hdr), name, IH_NMLEN); > > + char *hdr_name =3D image_get_name(hdr); > > + > > + strncpy(hdr_name, name, IH_NMLEN - 1); > > + hdr_name[IH_NMLEN - 1] =3D '\0'; > > } > > > > int image_check_hcrc(const image_header_t *hdr); > > -- > > 2.35.1 > >