[PATCH 2/2] kunit: string: add strnlen SIZE_MAX test coverage

Gao Rui gao.rui at zte.com.cn
Tue Jul 21 23:40:27 PDT 2026


Extend string_test_strnlen to validate strnlen behavior with
SIZE_MAX input:

- Add explicit test case for strnlen(s, SIZE_MAX).
- Ensure fallback to generic path returns correct length when
  string has no NUL terminator.
- Complements existing tests for non-terminated strings and
  boundary conditions.

This improves KUnit coverage for extreme inputs and verifies
the correctness of the new fallback logic in strnlen.S.

Signed-off-by: Gao Rui <gao.rui at zte.com.cn>
---
 lib/tests/string_kunit.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c
index 0819ace5b027..8e2e8980aa75 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,9 @@ 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);
 
+			/* Test Count overflow fallback */
+			KUNIT_EXPECT_EQ(test, strnlen(s, SSIZE_MAX), len);
+
 			s[len] = 'A';
 		}
 	}
-- 
2.27.0




More information about the linux-riscv mailing list