Received: by 2002:a05:7412:2a91:b0:fc:a2b0:25d7 with SMTP id u17csp659475rdh; Wed, 14 Feb 2024 07:51:57 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVUThseB2YA+7rsOROOWKU2OnuxO9o5RHdjIRDJ/v6JXzCvpkbBy+zfu7zzCPxLi1c2gmfQjnDlXcitr3KLP+dHeQ9qP+jj34xbKKLq4A== X-Google-Smtp-Source: AGHT+IHmPDjST1m5KZZMeiPRnM9JYpEvwxwt5ZFAjtuGYG69jaHqav9oSiUw46JtFvQSIKQZtGGg X-Received: by 2002:a05:622a:10c:b0:42c:71ea:9b14 with SMTP id u12-20020a05622a010c00b0042c71ea9b14mr3167588qtw.67.1707925917391; Wed, 14 Feb 2024 07:51:57 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707925917; cv=pass; d=google.com; s=arc-20160816; b=TMJg/rx/wQttkuXcu2K9wL2cUCaskYksk/BdLxEQoETj2KlVAq6r9X2dilRSVYt2VM 0iLCNN2CcRHqF1H5O407zrDgC/RiGC5TM4gU6KrmOQ4CcxKUZsJWLhY2lF8OPUhHV4S0 OGVL5Awqc8PWoggt3++UCX31C4CbcU5gC4UlHXXL9HXRuvUt8WkF4OV2//NFcRax1HcK Jnp23venrguVIyv3Efp20iFhqa+h/dFIWoh3AfrVwu9gUJ9aXd5Hv3HyJVw1xww7uV4w rJjuCjj/NZkkyh6sydo27JfUTQS5cUlTHDVCuEVGofCajw3+cY5uFtazKqS6iEFVOpyb PUNw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=HL093YFp52+Cx5NqH2C8baTYxU+tILgYmPeQU6mFh8c=; fh=UaL0yjBKdhTQqu/nkq3pG/1sLcDN16UCZ922/sIyLF0=; b=vjAD/sJ87UUjuy3skXg/b1nHgaycRK1VPO6zyOtTBBp2ZRd0WurFPTABc1ZceyS4xb AWETQy56vpHOdkedrqN1mbUZ0b9Rh/pUkCPv2DNmD4UySZZXDvTs7zNLm74ZJgfkBpEM cmpSBP6xwz9eelyKw24V6ZdOHwlOn+7WdzZMtpJ9p/cSUYiT2vdwOf517HtgZIE1sqZ8 /smwHH1m/6ow75l3pyjC5JvlFR845ztawdQoCPZGGDUJ9Y9Rb2hVXtXGGrzm0QKOZawH DlLQ2iXbBs+NRSZz9LZ53/1Hx9G6t+oXBdCrmsR8mBSzWrx0rmgGED3t4/S/viOzgerI aHlQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=sdfg.com.ar); spf=pass (google.com: domain of linux-kernel+bounces-65458-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65458-linux.lists.archive=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=2; AJvYcCWKUs8ixXU4/JogNKjG/fdNmeSXDzcX3LMvSJUkaZZAZhy/0+QiXt3DnMaLHi/8xlPMGfhsOh1xfRxuoXFhH64Z5VnN9MlsZDd+jKyrmQ== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id s4-20020a05622a018400b0042c56d61276si6217843qtw.351.2024.02.14.07.51.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 07:51:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65458-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=sdfg.com.ar); spf=pass (google.com: domain of linux-kernel+bounces-65458-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65458-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 823091C221CC for ; Wed, 14 Feb 2024 15:51:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1DDF15D756; Wed, 14 Feb 2024 15:51:01 +0000 (UTC) Received: from alerce.blitiri.com.ar (alerce.blitiri.com.ar [49.12.208.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 645F05C8F9 for ; Wed, 14 Feb 2024 15:50:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=49.12.208.134 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707925860; cv=none; b=jOHUvHCFAqfXmCPTftbX5lLCQRQSREWwG7o0Pd+b+9w6OcRbVBpeuE02G/jGL8PNVEoahEa0f9zKSQUTY+/OMqYUHyp0co+ZCP3zKYAsfjRmEeFuDhNt1NqV/SF/70g+FsnkCvm+fhgeB14Ved5PTVy/qtRQw5EoXAPjgcePQXw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707925860; c=relaxed/simple; bh=o17Yaz26IjWAG2mdbxPC3P4T4qfP+MX8KI/T4rwq+ao=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=u2TlN0gfVGVY+RYx/rWA+I5QUrh8vFlVCwbAIZrlu3PvEzdUDvPtpUkXyYTGOrB3K10jxx6ogseIszNiPLm1X35DUGD1Zj1wiAhC4qM0yXdROxU6bCMzZGcHikc8oJJfzw67UYOvEFdO5HiaY5X8mRmA/xLuLo248qT+KyS3M2w= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sdfg.com.ar; spf=pass smtp.mailfrom=sdfg.com.ar; arc=none smtp.client-ip=49.12.208.134 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sdfg.com.ar Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sdfg.com.ar Received: from [192.168.0.26] by sdfg.com.ar (chasquid) with ESMTPSA tls TLS_AES_128_GCM_SHA256 (over submission+TLS, TLS-1.3, envelope from "rodrigo@sdfg.com.ar") ; Wed, 14 Feb 2024 15:50:55 +0000 Message-ID: Date: Wed, 14 Feb 2024 12:50:53 -0300 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 3/4] tools/nolibc: Fix strlcpy() return code and size usage Content-Language: en-US To: Willy Tarreau Cc: =?UTF-8?Q?Thomas_Wei=C3=9Fschuh?= , linux-kernel@vger.kernel.org References: <20240129141516.198636-1-rodrigo@sdfg.com.ar> <20240129141516.198636-4-rodrigo@sdfg.com.ar> <20240211110814.GB19364@1wt.eu> From: Rodrigo Campos In-Reply-To: <20240211110814.GB19364@1wt.eu> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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 :) 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. What do you think? I'm fine with both, of course. If I resend, shall I add a suggested-by or directly you as the author? Best, Rodrigo