Received: by 2002:a05:7412:2a91:b0:fc:a2b0:25d7 with SMTP id u17csp662609rdh; Wed, 14 Feb 2024 07:57:41 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVXIn1lRIoq0WdN+wMfSVkwVIzFboAQLlnj0cEy5b7woorg1cBJUTmrMonU5OUsgxiI6A73wOCuGRnJtiEykegdnbZjuoBcjTDeH/1/Aw== X-Google-Smtp-Source: AGHT+IH3RYkYoRlvaA+BfYWcv3xfNxJCQcijUy9HdN6U9gbLId91ELQciS9++6BBySPcQbMmFB58 X-Received: by 2002:ad4:5bc9:0:b0:68e:e76a:74e with SMTP id t9-20020ad45bc9000000b0068ee76a074emr3538883qvt.40.1707926261570; Wed, 14 Feb 2024 07:57:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707926261; cv=pass; d=google.com; s=arc-20160816; b=uyHLVbR638jFL4KWt1IDzJFkNYEudYZSlktT3gG3DvHFhpR1j+aP3bh4OXKrqWuenL AELWuRM3h3FtZA+CzCCucMOZotK1CpXOWE17iM/BN9x7JnyTtxmiaQhTMpsrbz2hDEht nQVAGEGBIzJL0hRaE9PYrSFscHV2NXLvxoV5NU57xoN2ak1vTcoN7lWZpEj655dPEz3A HDj9NqlA6Jfr+SD61Rlb+aqvEsfmgx+asBL//tHmptwbhNPBlDc3lZSOEMK/dk9L/s0d l1BSmxR48egrtlB5Lix2uW6kOCYrkVDyR7lyUwbUdW+/5AS3uOsJj0R5m0bkaalvPY6v HJpA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date; bh=wJgFyyagFrRm6DYiPFvVY2lyZJDgxUgvmzR1LGa9B4E=; fh=AX4eAD8Qcus8d/ka5aOwLTKWNf6JFG9YlEyj5OvSkLU=; b=m7ff3O/pth2VeJNXfNyhHmVLdgFCcFA6xI/dZ24DEIzBStfkI0dJkJtj6vyc21MTIr axwg3oI7XS4ySnvBina/in+kinEiDFtj0Zx00zb64cR5jm9QrNBvytVlNokh7ddfA4cM BG+2ghJYjgnM2zrk/ITrq5yrrLq/PViWi/l55nbTnHcSjpbvcWB3kphtXwcXkVzCgfwl hySjCgBJuYg9laGXN68e1tkQ3Yax77sO7L+ed8Xm4l6dMp3bgjCxIIiJ5dUyY0qmioQV hOcSXkx82ZyTNFMJoCD47TEFYsWRbYT1wTf46leBsozgAcWBjYa+KV9p364DQd2GGPv9 IWTg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=1wt.eu); spf=pass (google.com: domain of linux-kernel+bounces-65468-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65468-linux.lists.archive=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=2; AJvYcCXfGHNdPXVhYTuh/HYe8mo5m/IKVOL3AatdvTXyLKBXaovJ1vcu8KR4xvnoe3Bkz9f3VsgabNuRuwVZL9yDrx6BPyrRaw5Lgx5wt39dxQ== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id kk19-20020a056214509300b0068eda269640si4661705qvb.448.2024.02.14.07.57.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 07:57:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65468-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=1wt.eu); spf=pass (google.com: domain of linux-kernel+bounces-65468-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65468-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 62E071C27ECE for ; Wed, 14 Feb 2024 15:56:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BC7F35D91A; Wed, 14 Feb 2024 15:56:00 +0000 (UTC) Received: from 1wt.eu (ded1.1wt.eu [163.172.96.212]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7B0DF5B5DB for ; Wed, 14 Feb 2024 15:55:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=163.172.96.212 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707926160; cv=none; b=sToOmL6Rg8zVtS9Sz0etO0k5Y5BYHqSKRW/nPZ+b1kP/MREG+AbuUK07qqrH6nAWXLldiDz8CxSS9rJ1Xl1tj+7A4uRttxO3XNQf1U7R/9XKX3dWvKBhwvhCP+8JViuVGLrZ3yWMKF4MhaCEgwojGZ8BVA/43hpcJgkejaoymqI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707926160; c=relaxed/simple; bh=GYlOf0ZLZBjDUzNZHnjjcXz77os/gke35De2B3RvlBc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=UbSEZjQeFbsnpecZ7WrhdsMpDYh9TjVbvTKAUktIrMOxZzBAALrZ5Nk57ziAwJvn+YHT5BBxjQ9SOkh+MBjbw2L0YYLy4CMDBUHGD/AFbPOs6ka3jHM1H5FUSA9QFkBRaWp/za9ZCZqikpY1Dz14TQmH3vy7pOozoiXHpMkzKVg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=1wt.eu; spf=pass smtp.mailfrom=1wt.eu; arc=none smtp.client-ip=163.172.96.212 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=1wt.eu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=1wt.eu Received: (from willy@localhost) by mail.home.local (8.17.1/8.17.1/Submit) id 41EFtj2x006798; Wed, 14 Feb 2024 16:55:45 +0100 Date: Wed, 14 Feb 2024 16:55:45 +0100 From: Willy Tarreau To: Rodrigo Campos Cc: Thomas =?iso-8859-1?Q?Wei=DFschuh?= , linux-kernel@vger.kernel.org Subject: Re: [PATCH 3/4] tools/nolibc: Fix strlcpy() return code and size usage Message-ID: References: <20240129141516.198636-1-rodrigo@sdfg.com.ar> <20240129141516.198636-4-rodrigo@sdfg.com.ar> <20240211110814.GB19364@1wt.eu> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Wed, Feb 14, 2024 at 12:50:53PM -0300, Rodrigo Campos wrote: > On 2/11/24 12:08, Willy Tarreau wrote: > > Hi Rodrigo, > > > > It's good, but for the same reason as the previous one, I'm getting > > smaller code by doing less in the loop. Also calling strlen() here > > looks expensive, I'm seeing that the compiler inlined it nevertheless > > and did it in a dep-optimized way due to the asm statement. That > > results in 67 bytes total while a simpler version gives 47. > > > > If I explicitly mark strlen() __attribute__((noinline)) that prevents > > it from doing so starting with gcc-10, where it correctly places a jump > > from strlcpy() to strlen() and ends up with 50 bytes (vs 44 for the alt > > one). The other one I can propose is directly derived from the other > > strlcat() variant, which first performs the copy and starts to count: > > > > size_t strlcpy(char *dst, const char *src, size_t size) > > { > > size_t len; > > > > for (len = 0; len < size; len++) { > > if (!(dst[len] = src[len])) > > return len; > > } > > > > /* end of src not found before size */ > > if (size) > > dst[size - 1] = '\0'; > > > > while (src[len]) > > len++; > > > > return len; > > } > > > > Just let me know what you think. > > This is one is very nice, thanks! > > Sorry I didn't think about the size at all when writing the functions :) Never be sorry, low-level user code like this is never trivial and that's the goal of the nolibc-test in the first place ;-) > We can change the loop to be: > > for (len = 0; len < size; len++) { > dst[len] = src[len]; > if (!dst[len]) > break; > } > > That IMHO it is slightly more readable and makes it only 2 bytes longer > here. It's not exactly the same, it will always write a zero at dst[size-1] due to the break statement. As much as I hate returns in the middle, this one made sense for this case. A goto to the final return statement is fine as well. > What do you think? I'm fine with both, of course. I'm fine with the more readable part (I also prefer it) but not the use of break here. > If I resend, shall I add a suggested-by or directly you as the author? No need for either, it's your work, my part was just a review and an addictive temptation to look at asm code ;-) Cheers, Willy