From nobody Sun Feb 8 14:07:11 2026 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 8366A6F53E for ; Sun, 18 Feb 2024 19:51:33 +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=1708285895; cv=none; b=UVoghnq1iGkqkB884zZRC3xMY+PrcHV4ZKOyWvAGk/CGlMmzJlMLSuoZSVC4yFWMMKG2bJbmd8FJrNsMbLh2qn6xMV0xNfjVBIgeItFPp7027ST0F5B2mR9yyv/zca7f0OoKglqFSrVZHl0fL2Ky3lfFRWpImkC6iKrn+E+sBnk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708285895; c=relaxed/simple; bh=yW/KTPvl1LdQSw2p4A5TjI0BdCEzeZVKTkHAyd72DpQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fy7oajpEZ+ggijQDl4MUE9/+W8Ir4XcYPGwkCybtYpivfD2quvroAihH7dBHxJjyzctajASarJozLUVQuozLrIR+F9y6iLSOHrtfBa6FmF1fG3TXbEmPAYNB2RjUVE5GO/dHtsrAXKw2zkQGvel4RZ+RVQQyd5AI7IsNoXV0/hg= 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:31 +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 1/4] tools/nolibc/string: export strlen() Date: Sun, 18 Feb 2024 16:51:03 -0300 Message-ID: <20240218195110.1386840-2-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: quoted-printable Content-Type: text/plain; charset="utf-8" As with commit 8d304a374023, "tools/nolibc/string: export memset() and memmove()", gcc -Os without -ffreestanding may fail to compile with: cc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib -lgcc -sta= tic -o test test.c /usr/bin/ld: /tmp/cccIasKL.o: in function `main': test.c:(.text.startup+0x1e): undefined reference to `strlen' collect2: error: ld returned 1 exit status As on the aforementioned commit, this patch adds a section to export this function so compilation works on those cases too. Signed-off-by: Rodrigo Campos --- tools/include/nolibc/string.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index a01c69dd495f..ed15c22b1b2a 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -123,7 +123,7 @@ char *strcpy(char *dst, const char *src) * thus itself, hence the asm() statement below that's meant to disable th= is * confusing practice. */ -static __attribute__((unused)) +__attribute__((weak,unused,section(".text.nolibc_strlen"))) size_t strlen(const char *str) { size_t len; --=20 2.43.0 From nobody Sun Feb 8 14:07:11 2026 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=20Wei=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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 =3D 0; =20 - for (len =3D 0; dst[len]; len++) - ; + for (; len < size; len++) { + if (dst[len] =3D=3D '\0') + break; + } =20 - for (;;) { - c =3D *src; - if (len < size) - dst[len] =3D 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] =3D *src; + if (*src =3D=3D '\0') break; len++; src++; } =20 + if (len < size) + dst[len] =3D '\0'; + + while (*src++) + len++; + return len; } =20 --=20 2.43.0 From nobody Sun Feb 8 14:07:11 2026 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: quoted-printable Content-Type: text/plain; charset="utf-8" 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; =20 - for (len =3D 0;;) { - c =3D src[len]; - if (len < size) - dst[len] =3D c; - if (!c) - break; - len++; + for (len =3D 0; len < size; len++) { + dst[len] =3D src[len]; + if (!dst[len]) + return len; } + if (size) + dst[size-1] =3D '\0'; + + while (src[len]) + len++; + return len; } =20 --=20 2.43.0 From nobody Sun Feb 8 14:07:11 2026 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 B1E9D7319A for ; Sun, 18 Feb 2024 19:51:38 +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=1708285900; cv=none; b=dLvai4KIONbkN59Nk3zgFL2bQEuzeIAJr94bxDDZf7ZXnfFo+EQ3Noi62PKAiN/DNoJ1SGDU2v6vs92szg+omCJ/EqTH7PhkZ9mbYooD0kzofuBwtomvE3vRw3BFzHPr98jf7INK+YmPVV0hx3AO4wi5itouSDdpnEh32UgHJxM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708285900; c=relaxed/simple; bh=9RVmOlD4OgrvSuTTroVq8lj5KRTmT9fr3g11wmsxIZQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BdUqKgOBMwh+cHWGnGIIwF/WUwcXtSdTSqjGK/t/tLzQakldjxD4ysSXdqQ4hzEo7IVvSGh6g02c/8dM9KP0uwjPm1BctI/VHn26vEpIlw4ZVWEMtOpXCpXZ4e4DcgKUZ1re/fDE0GHEyuFySLGZE5rN/rz/+dTmrbJD78p+aKE= 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:36 +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 4/4] selftests/nolibc: Add tests for strlcat() and strlcpy() Date: Sun, 18 Feb 2024 16:51:06 -0300 Message-ID: <20240218195110.1386840-5-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: quoted-printable Content-Type: text/plain; charset="utf-8" I've verified that the tests matches libbsd's strlcat()/strlcpy() implementation. Please note that as strlcat()/strlcpy() are not part of the libc, the tests are only compiled when using nolibc. Signed-off-by: Rodrigo Campos --- tools/testing/selftests/nolibc/nolibc-test.c | 40 ++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index 6ba4f8275ac4..d373fc14706c 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -600,6 +600,25 @@ int expect_strne(const char *expr, int llen, const cha= r *cmp) return ret; } =20 +#define EXPECT_STRBUFEQ(cond, expr, buf, val, cmp) \ + do { if (!(cond)) result(llen, SKIPPED); else ret +=3D expect_str_buf_eq(= expr, buf, val, llen, cmp); } while (0) + +static __attribute__((unused)) +int expect_str_buf_eq(size_t expr, const char *buf, size_t val, int llen, = const char *cmp) +{ + llen +=3D printf(" =3D %lu <%s> ", expr, buf); + if (strcmp(buf, cmp) !=3D 0) { + result(llen, FAIL); + return 1; + } + if (expr !=3D val) { + result(llen, FAIL); + return 1; + } + + result(llen, OK); + return 0; +} =20 /* declare tests based on line numbers. There must be exactly one test per= line. */ #define CASE_TEST(name) \ @@ -991,6 +1010,14 @@ int run_stdlib(int min, int max) for (test =3D min; test >=3D 0 && test <=3D max; test++) { int llen =3D 0; /* line length */ =20 + /* For functions that take a long buffer, like strlcat() + * Add some more chars after the \0, to test functions that overwrite th= e buffer set + * the \0 at the exact right position. + */ + char buf[10] =3D "test123456"; + buf[4] =3D '\0'; + + /* avoid leaving empty lines below, this will insert holes into * test numbers. */ @@ -1007,6 +1034,19 @@ int run_stdlib(int min, int max) CASE_TEST(strchr_foobar_z); EXPECT_STRZR(1, strchr("foobar", 'z')); b= reak; CASE_TEST(strrchr_foobar_o); EXPECT_STREQ(1, strrchr("foobar", 'o'), "= obar"); break; CASE_TEST(strrchr_foobar_z); EXPECT_STRZR(1, strrchr("foobar", 'z')); = break; +#ifdef NOLIBC + CASE_TEST(strlcat_0); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 0)= , buf, 3, "test"); break; + CASE_TEST(strlcat_1); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 1)= , buf, 4, "test"); break; + CASE_TEST(strlcat_5); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 5)= , buf, 7, "test"); break; + CASE_TEST(strlcat_6); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 6)= , buf, 7, "testb"); break; + CASE_TEST(strlcat_7); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 7)= , buf, 7, "testba"); break; + CASE_TEST(strlcat_8); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 8)= , buf, 7, "testbar"); break; + CASE_TEST(strlcpy_0); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 0)= , buf, 3, "test"); break; + CASE_TEST(strlcpy_1); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 1)= , buf, 3, ""); break; + CASE_TEST(strlcpy_2); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 2)= , buf, 3, "b"); break; + CASE_TEST(strlcpy_3); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 3)= , buf, 3, "ba"); break; + CASE_TEST(strlcpy_4); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 4)= , buf, 3, "bar"); break; +#endif CASE_TEST(memcmp_20_20); EXPECT_EQ(1, memcmp("aaa\x20", "aaa\x20",= 4), 0); break; CASE_TEST(memcmp_20_60); EXPECT_LT(1, memcmp("aaa\x20", "aaa\x60",= 4), 0); break; CASE_TEST(memcmp_60_20); EXPECT_GT(1, memcmp("aaa\x60", "aaa\x20",= 4), 0); break; --=20 2.43.0