mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] tools/nolibc: fix standard conformance of a few string functions
@ 2026-11-25 18:14 Arav Verma
  2026-11-25 18:14 ` [PATCH 1/3] tools/nolibc: fix strchr() and strrchr() for the NUL terminator Arav Verma
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Arav Verma @ 2026-11-25 18:14 UTC (permalink / raw)
  To: w, linux; +Cc: shuah, linux-kernel, linux-kselftest

A few nolibc functions deviate from the C standard in corner cases:
strchr()/strrchr() do not find the terminating NUL, strstr() returns
NULL for an empty needle, and atoi()/atol() do not skip leading
whitespace or accept a '+' sign.

Fix them and add test cases. The new tests fail without the fixes. The
results were also compared against glibc.

Arav Verma (3):
  tools/nolibc: fix strchr() and strrchr() for the NUL terminator
  tools/nolibc: fix strstr() with an empty needle
  tools/nolibc: make atoi() and atol() skip whitespace and accept '+'

 tools/include/nolibc/stdlib.h                |  5 +++++
 tools/include/nolibc/string.h                | 12 +++++-------
 tools/testing/selftests/nolibc/nolibc-test.c |  9 +++++++++
 3 files changed, 19 insertions(+), 7 deletions(-)

-- 
2.47.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] tools/nolibc: fix strchr() and strrchr() for the NUL terminator
  2026-11-25 18:14 [PATCH 0/3] tools/nolibc: fix standard conformance of a few string functions Arav Verma
@ 2026-11-25 18:14 ` Arav Verma
  2026-11-25 18:14 ` [PATCH 2/3] tools/nolibc: fix strstr() with an empty needle Arav Verma
  2026-11-25 18:14 ` [PATCH 3/3] tools/nolibc: make atoi() and atol() skip whitespace and accept '+' Arav Verma
  2 siblings, 0 replies; 4+ messages in thread
From: Arav Verma @ 2026-11-25 18:14 UTC (permalink / raw)
  To: w, linux; +Cc: shuah, linux-kernel, linux-kselftest

The C standard says that the terminating NUL character is considered
part of the string, so strchr(s, '\0') and strrchr(s, '\0') must
return a pointer to the terminator. Both functions stop before it and
return NULL instead.

Check the terminator too and add test cases.

Signed-off-by: Arav Verma <svav2021@gmail.com>
---
 tools/include/nolibc/string.h                | 10 ++++------
 tools/testing/selftests/nolibc/nolibc-test.c |  2 ++
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h
index 4000926f44ac4..2693b9bef43a6 100644
--- a/tools/include/nolibc/string.h
+++ b/tools/include/nolibc/string.h
@@ -111,11 +111,10 @@ void *memchr(const void *s, int c, size_t len)
 static __attribute__((unused))
 char *strchr(const char *s, int c)
 {
-	while (*s) {
+	do {
 		if (*s == (char)c)
 			return (char *)s;
-		s++;
-	}
+	} while (*s++);
 	return NULL;
 }
 
@@ -299,11 +298,10 @@ char *strrchr(const char *s, int c)
 {
 	const char *ret = NULL;
 
-	while (*s) {
+	do {
 		if (*s == (char)c)
 			ret = s;
-		s++;
-	}
+	} while (*s++);
 	return (char *)ret;
 }
 
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 1157b521e65b1..9e1d0f3afb901 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -2028,8 +2028,10 @@ int run_stdlib(int min, int max)
 		CASE_TEST(setncmp_blah_blah6); EXPECT_NE(1, strncmp("blah", "blah6", 6), 0); break;
 		CASE_TEST(strchr_foobar_o);    EXPECT_STREQ(1, strchr("foobar", 'o'), "oobar"); break;
 		CASE_TEST(strchr_foobar_z);    EXPECT_STRZR(1, strchr("foobar", 'z')); break;
+		CASE_TEST(strchr_foobar_nul);  EXPECT_STREQ(1, strchr("foobar", '\0'), ""); break;
 		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;
+		CASE_TEST(strrchr_foobar_nul); EXPECT_STREQ(1, strrchr("foobar", '\0'), ""); break;
 		CASE_TEST(strlcat_0);          EXPECT_STRBUFEQ(is_nolibc, strlcat(buf, "bar", 0), buf, 3, "test"); break;
 		CASE_TEST(strlcat_1);          EXPECT_STRBUFEQ(is_nolibc, strlcat(buf, "bar", 1), buf, 4, "test"); break;
 		CASE_TEST(strlcat_5);          EXPECT_STRBUFEQ(is_nolibc, strlcat(buf, "bar", 5), buf, 7, "test"); break;
-- 
2.47.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] tools/nolibc: fix strstr() with an empty needle
  2026-11-25 18:14 [PATCH 0/3] tools/nolibc: fix standard conformance of a few string functions Arav Verma
  2026-11-25 18:14 ` [PATCH 1/3] tools/nolibc: fix strchr() and strrchr() for the NUL terminator Arav Verma
@ 2026-11-25 18:14 ` Arav Verma
  2026-11-25 18:14 ` [PATCH 3/3] tools/nolibc: make atoi() and atol() skip whitespace and accept '+' Arav Verma
  2 siblings, 0 replies; 4+ messages in thread
From: Arav Verma @ 2026-11-25 18:14 UTC (permalink / raw)
  To: w, linux; +Cc: shuah, linux-kernel, linux-kselftest

The C standard says that strstr() returns the haystack if the needle is
an empty string. nolibc returns NULL instead.

Return the haystack in that case and add a test case.

Signed-off-by: Arav Verma <svav2021@gmail.com>
---
 tools/include/nolibc/string.h                | 2 +-
 tools/testing/selftests/nolibc/nolibc-test.c | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h
index 2693b9bef43a6..191a3bc8af29f 100644
--- a/tools/include/nolibc/string.h
+++ b/tools/include/nolibc/string.h
@@ -312,7 +312,7 @@ char *strstr(const char *haystack, const char *needle)
 
 	len_needle = strlen(needle);
 	if (!len_needle)
-		return NULL;
+		return (char *)haystack;
 
 	len_haystack = strlen(haystack);
 	while (len_haystack >= len_needle) {
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 9e1d0f3afb901..971a7f48e016d 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -2046,6 +2046,7 @@ int run_stdlib(int min, int max)
 		CASE_TEST(strstr_foobar_foo);  EXPECT_STREQ(1, strstr("foobar", "foo"), "foobar"); break;
 		CASE_TEST(strstr_foobar_bar);  EXPECT_STREQ(1, strstr("foobar", "bar"), "bar"); break;
 		CASE_TEST(strstr_foobar_baz);  EXPECT_PTREQ(1, strstr("foobar", "baz"), NULL); break;
+		CASE_TEST(strstr_foobar_empty); EXPECT_STREQ(1, strstr("foo", ""), "foo"); break;
 		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;
-- 
2.47.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] tools/nolibc: make atoi() and atol() skip whitespace and accept '+'
  2026-11-25 18:14 [PATCH 0/3] tools/nolibc: fix standard conformance of a few string functions Arav Verma
  2026-11-25 18:14 ` [PATCH 1/3] tools/nolibc: fix strchr() and strrchr() for the NUL terminator Arav Verma
  2026-11-25 18:14 ` [PATCH 2/3] tools/nolibc: fix strstr() with an empty needle Arav Verma
@ 2026-11-25 18:14 ` Arav Verma
  2 siblings, 0 replies; 4+ messages in thread
From: Arav Verma @ 2026-11-25 18:14 UTC (permalink / raw)
  To: w, linux; +Cc: shuah, linux-kernel, linux-kselftest

The C standard defines atoi() and atol() as strtol() with a fixed base
of 10, which skips leading whitespace and accepts an optional '+' or
'-' sign. nolibc only handles '-', so atoi(" 12") and atoi("+5") both
return 0.

Skip leading whitespace, accept '+' and add test cases.

Signed-off-by: Arav Verma <svav2021@gmail.com>
---
 tools/include/nolibc/stdlib.h                | 5 +++++
 tools/testing/selftests/nolibc/nolibc-test.c | 6 ++++++
 2 files changed, 11 insertions(+)

diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h
index 1c5a914c0f231..cccb8922bde7c 100644
--- a/tools/include/nolibc/stdlib.h
+++ b/tools/include/nolibc/stdlib.h
@@ -66,9 +66,14 @@ long atol(const char *s)
 	unsigned long d;
 	int neg = 0;
 
+	while (isspace(*s))
+		s++;
+
 	if (*s == '-') {
 		neg = 1;
 		s++;
+	} else if (*s == '+') {
+		s++;
 	}
 
 	while (1) {
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 971a7f48e016d..3062af07304a6 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -2097,6 +2097,12 @@ int run_stdlib(int min, int max)
 		CASE_TEST(limit_ptrdiff_min);       EXPECT_EQ(1, PTRDIFF_MIN, sizeof(long) == 8 ? (ptrdiff_t) 0x8000000000000000LL  : (ptrdiff_t) 0x80000000); break;
 		CASE_TEST(limit_ptrdiff_max);       EXPECT_EQ(1, PTRDIFF_MAX, sizeof(long) == 8 ? (ptrdiff_t) 0x7fffffffffffffffLL  : (ptrdiff_t) 0x7fffffff); break;
 		CASE_TEST(limit_size_max);          EXPECT_EQ(1, SIZE_MAX,    sizeof(long) == 8 ? (size_t)    0xffffffffffffffffULL : (size_t)    0xffffffffU); break;
+		CASE_TEST(atoi_simple);             EXPECT_EQ(1, atoi("35"), 35); break;
+		CASE_TEST(atoi_negative);           EXPECT_EQ(1, atoi("-35"), -35); break;
+		CASE_TEST(atoi_positive);           EXPECT_EQ(1, atoi("+35"), 35); break;
+		CASE_TEST(atoi_space);              EXPECT_EQ(1, atoi(" \t\n35"), 35); break;
+		CASE_TEST(atoi_space_negative);     EXPECT_EQ(1, atoi(" -35"), -35); break;
+		CASE_TEST(atol_space_positive);     EXPECT_EQ(1, atol(" +35"), 35L); break;
 		CASE_TEST(strtol_simple);           EXPECT_STRTOX(1, strtol, "35", 10, 35, -1, 0); break;
 		CASE_TEST(strtol_positive);         EXPECT_STRTOX(1, strtol, "+35", 10, 35, -1, 0); break;
 		CASE_TEST(strtol_negative);         EXPECT_STRTOX(1, strtol, "-35", 10, -35, -1, 0); break;
-- 
2.47.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-25 18:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 18:14 [PATCH 0/3] tools/nolibc: fix standard conformance of a few string functions Arav Verma
2026-11-25 18:14 ` [PATCH 1/3] tools/nolibc: fix strchr() and strrchr() for the NUL terminator Arav Verma
2026-11-25 18:14 ` [PATCH 2/3] tools/nolibc: fix strstr() with an empty needle Arav Verma
2026-11-25 18:14 ` [PATCH 3/3] tools/nolibc: make atoi() and atol() skip whitespace and accept '+' Arav Verma

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®