From patchwork Sun Feb 18 19:51:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rodrigo Campos X-Patchwork-Id: 202874 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp928921dyc; Sun, 18 Feb 2024 11:52:16 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU22vDPHJymzmF1SKj407SEQFxOFgnYR+/98bz5Hvm5jKIlpgI/oyroVMMPCMP9CXNBxQmJHpAIbfKRln6KFEw3Ny0Egg== X-Google-Smtp-Source: AGHT+IEcK/ZgH3lkwAVykITUAw3qEltYrL34/k1Ti4H3uudypJ/P3/R5UaSl85nwEr0pQHoKNsBU X-Received: by 2002:a17:906:3888:b0:a3e:abb4:d452 with SMTP id q8-20020a170906388800b00a3eabb4d452mr9406ejd.34.1708285936392; Sun, 18 Feb 2024 11:52:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708285936; cv=pass; d=google.com; s=arc-20160816; b=b8wfjWKsl822rbchr1d4kXXAw6/LCsNiUPR6bqJAoq7zj05SRlo7D+cwacK77N5SbW D6vfZHbKDtJQ7BwjBwjJmJz0DazpnqF0TPzluEna+slC6pHjoMWupoc+Tan/ACHgpUiY 22pj5Ifvt4HMKXxJ/To3m7WVKfqWCCTgq/VDmjeecxEPhWN1ICo0XsYn5WGEsS4OJonh 81jxLGRkP/elCLod7j9kmO6Qa/Fht7vA30+virqm1GYkLpyY+Npz1VRWNV0EpJH1nf/F Gw2XjNfuCM6PLJvR7pmFbvUAZHSnloWBU36MF8f1sGL5k1eC5z24DIAxAUQezLCw4y8g ZxiA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=/Jj/dTfBMxiOSprWgnxYhcQdbzES3oh6wRnpL+NDXaU=; fh=1pO/fnqlbA1DlSshLp2Kp23nBXobkeZ558bQEuNU3Z8=; b=E6OdLQHL7NDrubTh1w2rtkMEZnvgCLNpvZ3iybqEWLFE7X0+u5LAmjvM9Ey13ClQIm U3radwFbBil4f0PfNJN+HU6R/yhqsSo7DqFfcp01zY85L+cHawIm9QRAAxBU3nQFhzJB hYA11Pw55NLUT9zzT/fvi1Kh2L3m9B5bkZoF4BtSeHlefK/Sa7hsy8NTXOMSNNWxHtIV ZLVBs5WfFZ/CGko3IvALizof0LOHzFNGv14+VhGQRHbj6RHtsKLv3S0S2/wErlECrLoD UET2s6C8goinseFrrZ7po0M2StwDJ6bHENEDNeTtI02WSysgbWzK4d02763t2cn/0Nrh DeEQ==; 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-70559-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70559-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id r25-20020a170906351900b00a3ea73794f6si47490eja.412.2024.02.18.11.52.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 11:52:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70559-ouuuleilei=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=sdfg.com.ar); spf=pass (google.com: domain of linux-kernel+bounces-70559-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70559-ouuuleilei=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 0232F1F21373 for ; Sun, 18 Feb 2024 19:52:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E35B074262; Sun, 18 Feb 2024 19:51:38 +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 4D7DD71B57 for ; Sun, 18 Feb 2024 19:51:34 +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=1708285897; cv=none; b=GlwdBe3qukoy7h3uSTauG8J4ujiECaP2B9JMnh94WZFg/HwLX6eJhsU50Sv2O8Zj2K3L0IUBQgf+kiBE5ZaDsLKzSmklikfmL3ufVx9Q1eVaThyRAbxe34jkCaLPjF6w60wRtIvVbiZkJi8Qq/HcESUcEyouPuzNbTYRkyulxuA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708285897; c=relaxed/simple; bh=M6eSb0oeupi57UE5O2+bBR18+q2i2okEZ18XgE2QKuM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qW0UwJMoQFow3JLlC0O4jHZOjXFxngqO/docVF3hjhODOCEYBjFkZbH3DT8gtzMtXXCC/stIEYPZ168FA3K9Z77/iKWJRCgU05DGTjvOIwVU5+1xMvgcB5qU58I1aNuAlCU9YQNa83SV5PGLfEDOCgI6rNmCkHAnSJaOybveUb8= 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 lindsay.fibertel.com.ar by sdfg.com.ar (chasquid) with ESMTPSA tls TLS_AES_128_GCM_SHA256 (over submission, TLS-1.3, envelope from "rodrigo@sdfg.com.ar") ; Sun, 18 Feb 2024 19:51:33 +0000 From: Rodrigo Campos To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Cc: linux-kernel@vger.kernel.org, Rodrigo Campos Subject: [PATCH v3 2/4] tools/nolibc: Fix strlcat() return code and size usage Date: Sun, 18 Feb 2024 16:51:04 -0300 Message-ID: <20240218195110.1386840-3-rodrigo@sdfg.com.ar> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240218195110.1386840-1-rodrigo@sdfg.com.ar> References: <20240218195110.1386840-1-rodrigo@sdfg.com.ar> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791267634281150520 X-GMAIL-MSGID: 1791267634281150520 The return code should always be strlen(src) + strnlen(dst, size). Let's make sure to copy at most size-1 bytes from src and null-terminate the dst buffer if we did copied something. While we can use strnlen() and strncpy() to implement strlcat(), this is simple enough and results in shorter code when compiled. Signed-off-by: Rodrigo Campos --- tools/include/nolibc/string.h | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index ed15c22b1b2a..cc51fd6b63d0 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -187,22 +187,31 @@ 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 = 0; - for (len = 0; dst[len]; len++) - ; + for (; len < size; len++) { + if (dst[len] == '\0') + break; + } - for (;;) { - c = *src; - if (len < size) - dst[len] = c; - if (!c) + /* + * We want len < size-1. But as size is unsigned and can wrap + * around, we use len + 1 instead. + */ + while (len + 1 < size) { + dst[len] = *src; + if (*src == '\0') break; len++; src++; } + if (len < size) + dst[len] = '\0'; + + while (*src++) + len++; + return len; }