mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] lib/string_kunit: add strnlen() tests for strings without NUL terminator
@ 2026-05-26  8:59 Zongmin Zhou
  2026-07-16  9:16 ` [RESEND PATCH] " Zongmin Zhou
  0 siblings, 1 reply; 2+ messages in thread
From: Zongmin Zhou @ 2026-05-26  8:59 UTC (permalink / raw)
  To: kees, andy; +Cc: linux-hardening, linux-kernel, Zongmin Zhou, Feng Jiang

From: Zongmin Zhou <zhouzongmin@kylinos.cn>

The existing strnlen() tests only cover strings with a NUL terminator.
Add test cases for strings without a NUL terminator to verify that
strnlen() correctly returns the maximum length when no NUL is found
within the given bound.

Co-developed-by: Feng Jiang <jiangfeng@kylinos.cn>
Signed-off-by: Feng Jiang <jiangfeng@kylinos.cn>
Signed-off-by: Zongmin Zhou <zhouzongmin@kylinos.cn>
---
 lib/tests/string_kunit.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c
index 0819ace5b027..b0d5626a79c8 100644
--- a/lib/tests/string_kunit.c
+++ b/lib/tests/string_kunit.c
@@ -155,6 +155,16 @@ static void string_test_strnlen(struct kunit *test)
 
 	for (size_t offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) {
 		for (size_t len = 0; len <= STRING_TEST_MAX_LEN; len++) {
+			/* Test strings without NUL terminator */
+			s = buf + buf_size - offset - len;
+			if (len > 0)
+				KUNIT_EXPECT_EQ(test, strnlen(s, len - 1), len - 1);
+			if (len > 1)
+				KUNIT_EXPECT_EQ(test, strnlen(s, len - 2), len - 2);
+
+			KUNIT_EXPECT_EQ(test, strnlen(s, len), len);
+
+			/* Test strings with NUL terminator */
 			s = buf + buf_size - 1 - offset - len;
 			s[len] = '\0';
 
@@ -169,6 +179,7 @@ static void string_test_strnlen(struct kunit *test)
 			KUNIT_EXPECT_EQ(test, strnlen(s, len + 2), len);
 			KUNIT_EXPECT_EQ(test, strnlen(s, len + 10), len);
 
+			/* Restore buffer */
 			s[len] = 'A';
 		}
 	}
-- 
2.34.1


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

* [RESEND PATCH] lib/string_kunit: add strnlen() tests for strings without NUL terminator
  2026-05-26  8:59 [PATCH] lib/string_kunit: add strnlen() tests for strings without NUL terminator Zongmin Zhou
@ 2026-07-16  9:16 ` Zongmin Zhou
  0 siblings, 0 replies; 2+ messages in thread
From: Zongmin Zhou @ 2026-07-16  9:16 UTC (permalink / raw)
  To: andy, kees; +Cc: linux-hardening, linux-kernel, Zongmin Zhou, Feng Jiang

From: Zongmin Zhou <zhouzongmin@kylinos.cn>

The existing strnlen() tests only cover strings with a NUL terminator.
Add test cases for strings without a NUL terminator to verify that
strnlen() correctly returns the maximum length when no NUL is found
within the given bound.

Co-developed-by: Feng Jiang <jiangfeng@kylinos.cn>
Signed-off-by: Feng Jiang <jiangfeng@kylinos.cn>
Signed-off-by: Zongmin Zhou <zhouzongmin@kylinos.cn>
---
 lib/tests/string_kunit.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c
index 0819ace5b027..b0d5626a79c8 100644
--- a/lib/tests/string_kunit.c
+++ b/lib/tests/string_kunit.c
@@ -155,6 +155,16 @@ static void string_test_strnlen(struct kunit *test)
 
 	for (size_t offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) {
 		for (size_t len = 0; len <= STRING_TEST_MAX_LEN; len++) {
+			/* Test strings without NUL terminator */
+			s = buf + buf_size - offset - len;
+			if (len > 0)
+				KUNIT_EXPECT_EQ(test, strnlen(s, len - 1), len - 1);
+			if (len > 1)
+				KUNIT_EXPECT_EQ(test, strnlen(s, len - 2), len - 2);
+
+			KUNIT_EXPECT_EQ(test, strnlen(s, len), len);
+
+			/* Test strings with NUL terminator */
 			s = buf + buf_size - 1 - offset - len;
 			s[len] = '\0';
 
@@ -169,6 +179,7 @@ static void string_test_strnlen(struct kunit *test)
 			KUNIT_EXPECT_EQ(test, strnlen(s, len + 2), len);
 			KUNIT_EXPECT_EQ(test, strnlen(s, len + 10), len);
 
+			/* Restore buffer */
 			s[len] = 'A';
 		}
 	}
-- 
2.34.1


No virus found
		Checked by Hillstone Network AntiVirus


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

end of thread, other threads:[~2026-07-16  9:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-26  8:59 [PATCH] lib/string_kunit: add strnlen() tests for strings without NUL terminator Zongmin Zhou
2026-07-16  9:16 ` [RESEND PATCH] " Zongmin Zhou

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