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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E15E7C433F5 for ; Tue, 12 Oct 2021 12:00:31 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 1694561074 for ; Tue, 12 Oct 2021 12:00:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 1694561074 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id BD0DC83728; Tue, 12 Oct 2021 14:00:28 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org 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=kernel.org header.i=@kernel.org header.b="dLYS/VTd"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id AA45483735; Tue, 12 Oct 2021 14:00:26 +0200 (CEST) Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 328AD834D1 for ; Tue, 12 Oct 2021 14:00:23 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: by mail.kernel.org (Postfix) with ESMTPSA id B29CD61074; Tue, 12 Oct 2021 12:00:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634040021; bh=qquhSySyJdex/tRy8mN/06dRWZOYXyXLV8pjuAvbTtI=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=dLYS/VTdjde1IZA/naDLDNP8TaD7UCW+EttCxoch28B8IK5toslFfSNVzBnMRG/mv Puen/2OAB80YL7cwNjtxY7WhJbc+9VyS13L26P686OGwPEGv6OCEtKEM2Je5RQS8mu 6/o3Laiq27NVpFecrRnU0vivsxVjFlBlTgF9txGzG4DHyFPK5KXKWOQ7Z1eRMYLg5S 2nCcK6U0IZuoB4VrnkD1iTxrO2Sc9L45/FxmvehJcOZeUOKS9C1lhx7/cXWU/QXApb 80yvN09HNUFSllcpIoHUDyQKLYq3ii7Vcib9hFyNrdBtsBLcj7fVMzwDr+3I8sWMb0 fLWTzQ5FS9QBg== Date: Tue, 12 Oct 2021 14:00:18 +0200 From: Marek =?UTF-8?B?QmVow7pu?= To: Rasmus Villemoes Cc: Simon Glass , Tom Rini , U-Boot Mailing List , Marek =?UTF-8?B?QmVow7pu?= Subject: Re: [PATCH 08/10] env: Use strncpy() instead of ad-hoc code to copy variable value Message-ID: <20211012140018.3a4502e6@dellmb> In-Reply-To: References: <20211012110501.6118-1-kabel@kernel.org> <20211012110501.6118-9-kabel@kernel.org> X-Mailer: Claws Mail 3.18.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.2 at phobos.denx.de X-Virus-Status: Clean On Tue, 12 Oct 2021 13:41:43 +0200 Rasmus Villemoes wrote: > On 12/10/2021 13.04, Marek Beh=C3=BAn wrote: > > From: Marek Beh=C3=BAn > >=20 > > Copy the value of the found variable into given buffer with > > strncpy(). > >=20 > > Signed-off-by: Marek Beh=C3=BAn > > --- > > cmd/nvedit.c | 15 +++++---------- > > 1 file changed, 5 insertions(+), 10 deletions(-) > >=20 > > diff --git a/cmd/nvedit.c b/cmd/nvedit.c > > index 412918a000..51b9e4ffa4 100644 > > --- a/cmd/nvedit.c > > +++ b/cmd/nvedit.c > > @@ -746,18 +746,13 @@ int env_get_f(const char *name, char *buf, > > unsigned len) continue; > > =20 > > /* found; copy out */ > > - for (n =3D 0; n < len; ++n, ++buf) { > > - *buf =3D env[val++]; > > - if (*buf =3D=3D '\0') > > - return n; > > + n =3D strncpy(buf, &env[val], len) - buf; =20 >=20 > strncpy by definition returns the dst argument, so this is, apart from > the side effects done by strncpy, a complicated way of saying "n =3D > 0;". You are right, this is a mistake. > > + if (len && n =3D=3D len) { =20 >=20 > which then makes this automatically false always. >=20 > I understand why you want to avoid an open-coded copying, but strncpy > is almost certainly the wrong tool for the job. Apart from not > revealing anything about the actual length of the source string, it > also has the well-known defect of zeroing the tail of the buffer in > the (usual) case where the source is shorter than the buffer. U-Boot's strncpy does not do that: * Note that unlike userspace strncpy, this does not %NUL-pad the buffer. * However, the result is not %NUL-terminated if the source exceeds * @count bytes. But you are right that I should use strlcpy here. Thanks. > n =3D strlcpy(buf, &env[val], len); > if (n >=3D len) ... >=20 > is probably closer to what you want. But only if you can trust > &env[val] to actually have a nul byte before going into lala land. >=20 > Rasmus