diff mbox series

[v3] lib/test_string.c: Add strncmp() tests

Message ID 20230302071934.254111-1-bjorn@kernel.org (mailing list archive)
State Superseded
Headers show
Series [v3] lib/test_string.c: Add strncmp() tests | expand

Checks

Context Check Description
conchuod/cover_letter success Single patches do not need cover letters
conchuod/tree_selection success Guessed tree name to be for-next
conchuod/fixes_present success Fixes tag not required for -next series
conchuod/maintainers_pattern success MAINTAINERS pattern errors before the patch: 1 and now 1
conchuod/verify_signedoff success Signed-off-by tag matches author and committer
conchuod/kdoc success Errors and warnings before: 0 this patch: 0
conchuod/build_rv64_clang_allmodconfig success Errors and warnings before: 0 this patch: 0
conchuod/module_param success Was 0 now: 0
conchuod/build_rv64_gcc_allmodconfig success Errors and warnings before: 0 this patch: 0
conchuod/alphanumeric_selects success Out of order selects before the patch: 728 and now 728
conchuod/build_rv32_defconfig success Build OK
conchuod/dtb_warn_rv64 success Errors and warnings before: 11 this patch: 11
conchuod/header_inline success No static functions without inline keyword in header files
conchuod/checkpatch fail CHECK: spaces preferred around that '*' (ctx:VxV) ERROR: Use of __initconst requires a separate use of const
conchuod/source_inline success Was 0 now: 0
conchuod/build_rv64_nommu_k210_defconfig success Build OK
conchuod/verify_fixes success No Fixes tag
conchuod/build_rv64_nommu_virt_defconfig success Build OK

Commit Message

Björn Töpel March 2, 2023, 7:19 a.m. UTC
From: Palmer Dabbelt <palmer@rivosinc.com>

The RISC-V strncmp() fails on some inputs, see the linked thread for
more details. It turns out there were no strncmp() calls in the self
tests, this adds one.

Reported-by: Heiko Stübner <heiko@sntech.de>
Link: https://lore.kernel.org/all/2801162.88bMQJbFj6@diego/
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
Signed-off-by: Björn Töpel <bjorn@rivosinc.com>
---
Change back to it's original form. Checkpatch still complains about
__initconst, but there's a patch addressing that [1].

[1] https://lore.kernel.org/all/20230301094320.15954-1-bjorn@kernel.org/

v2->v3: Removed the "checkpatch fix" patch
        Added one more test

v1->v2: Added two more tests (pos/neg). (Andy)
        Minor code style issues. (Andy)
        Fixed checkpatch errors.
---
lib/test_string.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)


base-commit: c0927a7a5391f7d8e593e5e50ead7505a23cadf9

Comments

Andy Shevchenko March 2, 2023, 10:26 a.m. UTC | #1
On Thu, Mar 2, 2023 at 9:19 AM Björn Töpel <bjorn@kernel.org> wrote:
>
> From: Palmer Dabbelt <palmer@rivosinc.com>
>
> The RISC-V strncmp() fails on some inputs, see the linked thread for
> more details. It turns out there were no strncmp() calls in the self
> tests, this adds one.

Thank you, it's good enough.

Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>

> Reported-by: Heiko Stübner <heiko@sntech.de>
> Link: https://lore.kernel.org/all/2801162.88bMQJbFj6@diego/
> Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
> Signed-off-by: Björn Töpel <bjorn@rivosinc.com>
> ---
> Change back to it's original form. Checkpatch still complains about
> __initconst, but there's a patch addressing that [1].
>
> [1] https://lore.kernel.org/all/20230301094320.15954-1-bjorn@kernel.org/
>
> v2->v3: Removed the "checkpatch fix" patch
>         Added one more test
>
> v1->v2: Added two more tests (pos/neg). (Andy)
>         Minor code style issues. (Andy)
>         Fixed checkpatch errors.
> ---
> lib/test_string.c | 42 ++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 42 insertions(+)
>
> diff --git a/lib/test_string.c b/lib/test_string.c
> index c5cb92fb710e..f18a34578f1f 100644
> --- a/lib/test_string.c
> +++ b/lib/test_string.c
> @@ -207,6 +207,43 @@ static __init int strspn_selftest(void)
>         return 0;
>  }
>
> +static __init int strncmp_selftest(void)
> +{
> +       size_t i;
> +       static const struct strncmp_test {
> +               const char *str_a;
> +               const char *str_b;
> +               unsigned long count;
> +               unsigned long max_off;
> +               int retval;
> +       } tests[] __initconst = {
> +               { "/dev/vda", "/dev/", 5, 4, 0 },
> +               { "/dev/vda", "/dev/vdb", 5, 4, 0 },
> +               { "00000000---11111", "00000000---11112", 12, 4, 0 },
> +               { "/dev/vda", "/dev/vd\0", 8, 0, 97 },
> +               { "ABC", "AB", 3, 0, 67 },
> +               { "ABA", "ABZ", 3, 0, -25 },
> +               { "ABC", "ABC", 3, 0, 0 },
> +       };
> +
> +       for (i = 0; i < ARRAY_SIZE(tests); ++i) {
> +               const struct strncmp_test *s = tests + i;
> +               size_t off;
> +
> +               for (off = 0; off <= s->max_off; off++) {
> +                       int res = strncmp(s->str_a + off, s->str_b + off, s->count - off);
> +
> +                       if (res == 0 && s->retval != 0)
> +                               return 0x1000 + 0x100*off + 0x10*i + 0x0;
> +                       if (res > 0 && s->retval <= 0)
> +                               return 0x1000 + 0x100*off + 0x10*i + 0x1;
> +                       if (res < 0 && s->retval >= 0)
> +                               return 0x1000 + 0x100*off + 0x10*i + 0x2;
> +               }
> +       }
> +       return 0;
> +}
> +
>  static __exit void string_selftest_remove(void)
>  {
>  }
> @@ -245,6 +282,11 @@ static __init int string_selftest_init(void)
>         if (subtest)
>                 goto fail;
>
> +       test = 7;
> +       subtest = strncmp_selftest();
> +       if (subtest)
> +               goto fail;
> +
>         pr_info("String selftests succeeded\n");
>         return 0;
>  fail:
>
> base-commit: c0927a7a5391f7d8e593e5e50ead7505a23cadf9
> --
> 2.37.2
>
diff mbox series

Patch

diff --git a/lib/test_string.c b/lib/test_string.c
index c5cb92fb710e..f18a34578f1f 100644
--- a/lib/test_string.c
+++ b/lib/test_string.c
@@ -207,6 +207,43 @@  static __init int strspn_selftest(void)
 	return 0;
 }
 
+static __init int strncmp_selftest(void)
+{
+	size_t i;
+	static const struct strncmp_test {
+		const char *str_a;
+		const char *str_b;
+		unsigned long count;
+		unsigned long max_off;
+		int retval;
+	} tests[] __initconst = {
+		{ "/dev/vda", "/dev/", 5, 4, 0 },
+		{ "/dev/vda", "/dev/vdb", 5, 4, 0 },
+		{ "00000000---11111", "00000000---11112", 12, 4, 0 },
+		{ "/dev/vda", "/dev/vd\0", 8, 0, 97 },
+		{ "ABC", "AB", 3, 0, 67 },
+		{ "ABA", "ABZ", 3, 0, -25 },
+		{ "ABC", "ABC", 3, 0, 0 },
+	};
+
+	for (i = 0; i < ARRAY_SIZE(tests); ++i) {
+		const struct strncmp_test *s = tests + i;
+		size_t off;
+
+		for (off = 0; off <= s->max_off; off++) {
+			int res = strncmp(s->str_a + off, s->str_b + off, s->count - off);
+
+			if (res == 0 && s->retval != 0)
+				return 0x1000 + 0x100*off + 0x10*i + 0x0;
+			if (res > 0 && s->retval <= 0)
+				return 0x1000 + 0x100*off + 0x10*i + 0x1;
+			if (res < 0 && s->retval >= 0)
+				return 0x1000 + 0x100*off + 0x10*i + 0x2;
+		}
+	}
+	return 0;
+}
+
 static __exit void string_selftest_remove(void)
 {
 }
@@ -245,6 +282,11 @@  static __init int string_selftest_init(void)
 	if (subtest)
 		goto fail;
 
+	test = 7;
+	subtest = strncmp_selftest();
+	if (subtest)
+		goto fail;
+
 	pr_info("String selftests succeeded\n");
 	return 0;
 fail: