On 2026/7/22 14:40, Gao Rui wrote:
> 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 <[email protected]>
> ---
>  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';
>               }
>       }

A similar patch has already been submitted:

https://lore.kernel.org/all/[email protected]/

-- 
With Best Regards,
Feng Jiang


Reply via email to