mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Arav Verma <svav2021@gmail.com>
To: w@1wt.eu, linux@weissschuh.net
Cc: shuah@kernel.org, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org
Subject: [PATCH 1/3] tools/nolibc: fix strchr() and strrchr() for the NUL terminator
Date: Wed, 25 Nov 2026 12:14:51 -0600	[thread overview]
Message-ID: <20261125181453.814976-2-svav2021@gmail.com> (raw)
In-Reply-To: <20261125181453.814976-1-svav2021@gmail.com>

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


  reply	other threads:[~2026-11-25 18:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125181453.814976-2-svav2021@gmail.com \
    --to=svav2021@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=shuah@kernel.org \
    --cc=w@1wt.eu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®