The patch titled Subject: lib/test_string.c: add some testcases for strchr and strnchr has been added to the -mm tree. Its filename is lib-test_string-add-some-testcases-for-strchr-and-strnchr.patch This patch should soon appear at http://ozlabs.org/~akpm/mmots/broken-out/lib-test_string-add-some-testcases-for-strchr-and-strnchr.patch and later at http://ozlabs.org/~akpm/mmotm/broken-out/lib-test_string-add-some-testcases-for-strchr-and-strnchr.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next and is updated there every 3-4 working days ------------------------------------------------------ From: Peter Rosin <peda@xxxxxxxxxx> Subject: lib/test_string.c: add some testcases for strchr and strnchr Make sure that the trailing NUL is considered part of the string and can be found. Link: http://lkml.kernel.org/r/20190506124634.6807-4-peda@xxxxxxxxxx Signed-off-by: Peter Rosin <peda@xxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- lib/test_string.c | 77 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) --- a/lib/test_string.c~lib-test_string-add-some-testcases-for-strchr-and-strnchr +++ a/lib/test_string.c @@ -111,6 +111,73 @@ fail: return 0; } +static __init int strchr_selftest(void) +{ + const char *test_string = "abcdefghijkl"; + const char *empty_string = ""; + char *result; + int i; + + for (i = 0; i < strlen(test_string) + 1; i++) { + result = strchr(test_string, test_string[i]); + if (result - test_string != i) + return i + 'a'; + } + + result = strchr(empty_string, '\0'); + if (result != empty_string) + return 0x101; + + result = strchr(empty_string, 'a'); + if (result) + return 0x102; + + result = strchr(test_string, 'z'); + if (result) + return 0x103; + + return 0; +} + +static __init int strnchr_selftest(void) +{ + const char *test_string = "abcdefghijkl"; + const char *empty_string = ""; + char *result; + int i, j; + + for (i = 0; i < strlen(test_string) + 1; i++) { + for (j = 0; j < strlen(test_string) + 2; j++) { + result = strnchr(test_string, j, test_string[i]); + if (j <= i) { + if (!result) + continue; + return ((i + 'a') << 8) | j; + } + if (result - test_string != i) + return ((i + 'a') << 8) | j; + } + } + + result = strnchr(empty_string, 0, '\0'); + if (result) + return 0x10001; + + result = strnchr(empty_string, 1, '\0'); + if (result != empty_string) + return 0x10002; + + result = strnchr(empty_string, 1, 'a'); + if (result) + return 0x10003; + + result = strnchr(NULL, 0, '\0'); + if (result) + return 0x10004; + + return 0; +} + static __init int string_selftest_init(void) { int test, subtest; @@ -130,6 +197,16 @@ static __init int string_selftest_init(v if (subtest) goto fail; + test = 4; + subtest = strchr_selftest(); + if (subtest) + goto fail; + + test = 5; + subtest = strnchr_selftest(); + if (subtest) + goto fail; + pr_info("String selftests succeeded\n"); return 0; fail: _ Patches currently in -mm which might be from peda@xxxxxxxxxx are lib-string-allow-searching-for-nul-with-strnchr.patch lib-test_string-avoid-masking-memset16-32-64-failures.patch lib-test_string-add-some-testcases-for-strchr-and-strnchr.patch