[PATCH v2 06/14] lib/string_kunit: add correctness test for strnlen

Andy Shevchenko andriy.shevchenko at intel.com
Tue Jan 13 00:41:51 PST 2026


On Tue, Jan 13, 2026 at 04:27:40PM +0800, Feng Jiang wrote:
> Add a KUnit test for strnlen() to verify correctness across
> different string lengths and memory alignments.

...

> +	for (offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) {
> +		for (len = 0; len <= STRING_TEST_MAX_LEN; len++) {
> +			s[offset + len] = '\0';

This should be one or two characters further so you check should also check for
non-NUL terminated strings...

> +			if (len > 0)
> +				KUNIT_EXPECT_EQ(test, strnlen(s + offset, len - 1), len - 1);

...or make more tests, with len - 2 at least.

> +			KUNIT_EXPECT_EQ(test, strnlen(s + offset, len), len);

> +			KUNIT_EXPECT_EQ(test, strnlen(s + offset, len + 1), len);

I would also add + 2 test case.

> +			KUNIT_EXPECT_EQ(test, strnlen(s + offset, len + 10), len);

> +			s[offset + len] = 'A';
> +		}
> +	}
> +}

-- 
With Best Regards,
Andy Shevchenko





More information about the linux-riscv mailing list