From: Zongmin Zhou <[email protected]> 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 <[email protected]> Signed-off-by: Feng Jiang <[email protected]> Signed-off-by: Zongmin Zhou <[email protected]> --- 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

