Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp1904616rdb; Sun, 11 Feb 2024 02:52:46 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVey8IzK8aZ+Wg8lXpQugZ9bBRRw2OKSP5m1oa9nUM8ITDlzrdwi/ds4dxwsvV7aHB+k5kWL6CXbxPq2TLvFPcvlgqOGtxmYJfoLjEUQQ== X-Google-Smtp-Source: AGHT+IHXTkCqohyM5Zz2uhv4CVxtLlFn0QIfbQglgedg+NgMKYKexn9uiLCsEyz4VQFfJnJyraG1 X-Received: by 2002:a17:906:b286:b0:a38:2e6d:5ec6 with SMTP id q6-20020a170906b28600b00a382e6d5ec6mr3033801ejz.32.1707648765929; Sun, 11 Feb 2024 02:52:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707648765; cv=pass; d=google.com; s=arc-20160816; b=ubBdJC9H0ewR8QhVQoMsaovyJL4Zg1Ybzpw8+Px4E3RFlSx937KpYWWxyd+lWFOQSD zLirpPQgFK3LlCtFbYwLN99/0f7Q9A+6H5pR8zL54Y/d60gyGtVW02TpFljiA2GZvjMt bMwWHRpM2K2FRWLqXNs6pAfdcjEGM0v38zFLTp8Llcv5FY5fih4mVn1lBSu2F8lQqeFc BASsYzG60Ca/1Clrr+a9nOS0dcs/KK81WqEueMzcRv/XxlcIOtd5uHeR3f8kG9Y6bC7f AtlmTWQhXfGL1GBvXKyYGpMUjo8Yp0f/cR7mpx2QY1tMxI8kf08tZUP9qZa7HfmpS+rx QghQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:content-disposition:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:references :message-id:subject:cc:to:from:date; bh=MdHgqAttiNw+ABf5+gJbS5PEct0WJBdzxasc0g+r7Tw=; fh=H/TfVALQa0bbmE7RbgWuEaxfWBkVk6B6OrpObKV4Gy4=; b=yHWEbVtGtM5CGQX1ra3UxjNHIko1jUtYT7RUmINWUnrc3fESsWHLDL9Rtvn1MN02z6 DlcQGp+KNbMH5J4tAmbKBpf+ZYkGCawFjTgMtzhzfZrKLxnNMFix3W0RPZHk3YwmXI+T ae4MkBXGJ+h6De5Z5UtAbuZopsBeb5RkEpXxO6+jsieXNfcfeY15s9woVz7bM33O1RiW swXr0z1dxSieT7I7H3lqCePII80uzCRanm5lVyC3jGrwxHlMGTcLq5snXKGhGSCl+2vm X49o47emkUK2Dqda871JaKJh0LCyaeIE2w3Rr/cpmBP2rGN+h4mLgELN2R5NN7ogqRAb e+4Q==; 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-60701-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60701-linux.lists.archive=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=2; AJvYcCVpHnkMegS5wMNrcscFaViUO99OCMLPqTzS+TuwbicQf8ym5UAexu2cxAAUBEfAhLIIA5ciypuRRv0eaJSr/YzLJYGJDwvVziXpLKy30g== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id w18-20020a17090652d200b00a3bb3b38fdasi2757026ejn.942.2024.02.11.02.52.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Feb 2024 02:52:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60701-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=1wt.eu); spf=pass (google.com: domain of linux-kernel+bounces-60701-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60701-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 am.mirrors.kernel.org (Postfix) with ESMTPS id ACC4C1F2272A for ; Sun, 11 Feb 2024 10:52:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F07DE5A11F; Sun, 11 Feb 2024 10:52:38 +0000 (UTC) Received: from 1wt.eu (ded1.1wt.eu [163.172.96.212]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 23E5159169 for ; Sun, 11 Feb 2024 10:52:34 +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=1707648758; cv=none; b=ndxRF90PllthBfkNBWFln8YgNm/l/iH2w16WTGvlTMZvRtS8VMnS1KzdUn9haAyo6d1Y5IH3RzBj3OcV9HYPiOwBqGw33gpD58egC5ddKt+C9IhbX6seYcpXCX3ReiEz0MWd3o6hVfP1dhCkdx0Js4TmBkl4Ei21ZPIVZnDhKGM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707648758; c=relaxed/simple; bh=cNxbcgqqMclAIyBM5OsBn20kZKGMNf6fjjyrqyOYt2I=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=GGk35YcGU1OcVLNTtTsNAzJqGDfCMT5VHpS5K9OupW1o1FdXZr7U6AQjJtHr9xfMfYwSkRaNEI2vS2/8fuokWFvaWbAJJQEYdEeJVSG/rHTZAfVKZ53Rbs1JRmNX3aDCAkI+jos5PacBJ6OCq7u9zHIvxLesgKOb5oPxuKnTl64= 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 pcw.home.local (8.15.2/8.15.2/Submit) id 41BAmHBB021945; Sun, 11 Feb 2024 11:48:17 +0100 Date: Sun, 11 Feb 2024 11:48:17 +0100 From: Willy Tarreau To: Rodrigo Campos Cc: Thomas =?iso-8859-1?Q?Wei=DFschuh?= , linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/4] tools/nolibc: Fix strlcat() return code and size usage Message-ID: <20240211104817.GA19364@1wt.eu> References: <20240129141516.198636-1-rodrigo@sdfg.com.ar> <20240129141516.198636-3-rodrigo@sdfg.com.ar> 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: <20240129141516.198636-3-rodrigo@sdfg.com.ar> User-Agent: Mutt/1.10.1 (2018-07-13) Hi Rodrigo, first, thanks for the series! On Mon, Jan 29, 2024 at 03:15:14PM +0100, Rodrigo Campos wrote: > The return code should always be strlen(src) + strlen(dst), but dst is > considered shorter if size is less than strlen(dst). > > While we are there, make sure to copy at most size-1 bytes and > null-terminate the dst buffer. > > Signed-off-by: Rodrigo Campos > --- > tools/include/nolibc/string.h | 14 +++++++------- > 1 file changed, 7 insertions(+), 7 deletions(-) > > diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h > index ed15c22b1b2a..b2149e1342a8 100644 > --- a/tools/include/nolibc/string.h > +++ b/tools/include/nolibc/string.h > @@ -187,23 +187,23 @@ char *strndup(const char *str, size_t maxlen) > static __attribute__((unused)) > size_t strlcat(char *dst, const char *src, size_t size) > { > - size_t len; > char c; > + size_t len = strlen(dst); > + size_t ret = strlen(src) + (size < len? size: len); From what I'm reading in the man page, ret should *always* be the sum of the two string lengths. I guess it helps for reallocation. It's even explicitly mentioned: "While this may seem somewhat confusing, it was done to make truncation detection simple." Above ret will be bound to the existing size so a realloc wouldn't work. Thus I think the correct solution is: size_t ret = strlen(src) + len; > - for (len = 0; dst[len]; len++) > - ; > - > - for (;;) { > + for (;len < size;) { > c = *src; > - if (len < size) > + if (len < size - 1) > dst[len] = c; > + if (len == size - 1) > + dst[len] = '\0'; > if (!c) > break; > len++; > src++; > } > > - return len; > + return ret; > } The test inside the loop is going to make this not very efficient. Same for the fact that we're measuring the length of src twice (once via strlen, a second time through the loop). I've just had a look and it compiles to 77 bytes at -Os. A simpler variant would consist in trying to copy what fits in and once reached, go on just for trailing zero and the size measurement: size_t strlcat(char *dst, const char *src, size_t size) { size_t len = strlen(dst); while (len < size) { if (!(dst[len] = *src)) return len; len++; src++; } /* end of src not found before size */ if (size) dst[size - 1] = '\0'; while (*src++) len++; return len; } For me it's 58 bytes, or 19 less / 25% smaller, and at first glance it should do the right thing as well. What do you think ? Thanks! Willy