From patchwork Sun Feb 18 19:51:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rodrigo Campos X-Patchwork-Id: 202876 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp929069dyc; Sun, 18 Feb 2024 11:52:55 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUiNak7KHI7RrYawmfF+aJtvwGBWjy//FO/faSTw7DqiG4/T+c9bVel53ObNMOB6ZpIK2tpYeDiU1GQYwq44O1IxdeOOA== X-Google-Smtp-Source: AGHT+IHXJDD6eo6qU+0PWrDAJqb8wRxagxwMSCCwbnpHwa2WyrCOSkLT3rG9E+qW40T4zrrXPA1o X-Received: by 2002:a17:903:8c3:b0:1db:895d:6bfb with SMTP id lk3-20020a17090308c300b001db895d6bfbmr13678980plb.8.1708285975583; Sun, 18 Feb 2024 11:52:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708285975; cv=pass; d=google.com; s=arc-20160816; b=aytUh5gf6Q3jwtBkCCpMc870VsIqPV2SMv7ywhTkXElBKis0vCJUiCl0hAgU/Kz82A he/cxOQLq62K6Xk6zWUHJfeQc5W8DDCEkJ52jsZs1K41s0XDzjrRvN+sfTXSTkWF3Dxx j001E+BtcbaA1btuvSgfzuE8aeV1G1IwFtIBMEv/YN7/bAtrstQ0xnjonKjhNkydnAZe ATIn5DECxZFUL4rG7My7F14wtKDMY+9UiQPSNWahJBXM7octJJUD2ZTp/AmzMaom1c/J alhN/kscrRDVftHjw1Vj3cZPHFNg9svVGL7shlP6yNP644DEEFyzqZWuZ+vm0lgcjOOf WKtQ== 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=TFi0Tkjn4ecYIlq7T2/4l5bOXLFPCOOo7gC0cg+Tcp0=; fh=1pO/fnqlbA1DlSshLp2Kp23nBXobkeZ558bQEuNU3Z8=; b=YM11AyFtjTxp3Xunpew95+bLcOXae8jJO1FqXMirW9/alQUjnq4pJ5nqJ7EYhLUyrZ qZHCiq5TwiHwa12Ui0DJqos5nfoBsIVP8rWIMF9Y4xfb83mQK+MBe3pMVX5lso/BKJke hP5hjR3V9OD3G0uoyfkMaAAj7gpCrfH5RInZXkRp4AQfdJa6XtpjnXbYJm3PFdaKbE9c YcFQBtgKaglLqp9/oXhImWnX0Pmvti9HT5APg5wSxsrfcIrHsnsKzYR0eZifbnitLa24 kMptK99nvwNGsg7Ixo4hGdygPSt3QoS0FR+O8VRDOfYRlMrk6Bj6lFTRePDyL5VGO57c uLQQ==; 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-70560-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70560-ouuuleilei=gmail.com@vger.kernel.org" Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id h10-20020a170902f7ca00b001dbe26ae64asi1469517plw.38.2024.02.18.11.52.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 11:52:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70560-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=sdfg.com.ar); spf=pass (google.com: domain of linux-kernel+bounces-70560-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70560-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id C40B0B212F7 for ; Sun, 18 Feb 2024 19:52:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5EF3A745D0; Sun, 18 Feb 2024 19:51:40 +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 D86EB73176 for ; Sun, 18 Feb 2024 19:51:36 +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=1708285898; cv=none; b=ivEvE3lZ4UFPSWFV6lNRzfstBkdGODtr77roOmJ6KP41fujFOqPYoju23pVYlgxcJediYfRh78680FLhsIDO8If8TxZtgfQzmaUQLHP9xpr++BDvKFRrLWCqPjjQS6393zYPdBirb83pjmCC+/ic1YXIWEi23XTR3zrsqMU6/FY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708285898; c=relaxed/simple; bh=it0xpWdUNAJBa027kuEXaW7NIG4qOxDWBerpqeL8YXc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=B1yxVmrIBp9M2ESTNr5NSQbT27V2L2LqCykpybLCYHTrXRHu4dl5lSKKhkUAGUk558LLGTr3LDLTrV9V+blqp6ciO31VIkq597N5Gb+nv+KfjkgNk7yfAM9PIE9PVe5KbBsgvoG984VErkhVvGT5pZ4pN1bpDK1uwrEwWQwDFEU= 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:35 +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 3/4] tools/nolibc: Fix strlcpy() return code and size usage Date: Sun, 18 Feb 2024 16:51:05 -0300 Message-ID: <20240218195110.1386840-4-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: 1791267675627135804 X-GMAIL-MSGID: 1791267675627135804 The return code should always be strlen(src), and we should copy at most size-1 bytes. While we are there, make sure to null-terminate the dst buffer if we copied something. Signed-off-by: Rodrigo Campos --- tools/include/nolibc/string.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index cc51fd6b63d0..565230a4ad47 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -219,16 +219,18 @@ static __attribute__((unused)) size_t strlcpy(char *dst, const char *src, size_t size) { size_t len; - char c; - for (len = 0;;) { - c = src[len]; - if (len < size) - dst[len] = c; - if (!c) - break; - len++; + for (len = 0; len < size; len++) { + dst[len] = src[len]; + if (!dst[len]) + return len; } + if (size) + dst[size-1] = '\0'; + + while (src[len]) + len++; + return len; }