Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp789621rdb; Sun, 18 Feb 2024 11:52:21 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXrWG6R3YJ3GokyP3EsJ6FDhxSF40NaeLVvQX2J4fvNAoiL+wv266SYYQ684I2OBLOio0cUrKwhaomeDyEsu4enQFSrBGOGEDxV1IJVEg== X-Google-Smtp-Source: AGHT+IFat2Stpjaih5Oo8r28klkU8shY3Gg0KmUtkkYmnlpnSK1Dw67Lcx8JDrmRKcHk2Ys84kdR X-Received: by 2002:a17:90a:d243:b0:299:9ba4:abe6 with SMTP id o3-20020a17090ad24300b002999ba4abe6mr666280pjw.46.1708285940830; Sun, 18 Feb 2024 11:52:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708285940; cv=pass; d=google.com; s=arc-20160816; b=fEdb5Hdn15oPU/z+Kr3tMPw5Yp8BaqsnQIF2BSkVKzZ7vRmn+c46YMmLvZjTGNfz9J db2QJkRcjOCbE3QqHB681qMff+PrlA1xfKp2ALOOKyoUy/cd2ULslOzPdqH2CGzrAzVs Y7Uc9PvT7sXNFV2FYqZ/oOXik3WF1oIHKBwrCTZuWXQxKfAUIBeGoBxTrDaxlpQYxNXT 2/xDs/m5c6Jgbo2dm8O1tEQaF5rycqVP+JEXMdSBupUampil/ObLceBDDrIg+tPj8/F7 Q/TvlXGWSIuH05jTCKFJ8IhSGz8O+MEAM472v+ZUnMRizdj0wcxM7PRTeaqYHMAw56vz yB/A== 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-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70560-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id so13-20020a17090b1f8d00b0029631bd90c5si3334384pjb.156.2024.02.18.11.52.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 11:52:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70560-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70560-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 8B6A2281B81 for ; Sun, 18 Feb 2024 19:52:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 58EA071B57; Sun, 18 Feb 2024 19:51:39 +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=20Wei=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 Content-Transfer-Encoding: 8bit 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; } -- 2.43.0