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 2/3] tools/nolibc: fix strstr() with an empty needle
Date: Wed, 25 Nov 2026 12:14:52 -0600 [thread overview]
Message-ID: <20261125181453.814976-3-svav2021@gmail.com> (raw)
In-Reply-To: <20261125181453.814976-1-svav2021@gmail.com>
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
next prev parent 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 ` [PATCH 1/3] tools/nolibc: fix strchr() and strrchr() for the NUL terminator Arav Verma
2026-11-25 18:14 ` Arav Verma [this message]
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-3-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®