Hi! str_has_prefix() is based on sizes and lengths, which means it must overload the valid length 0 to mean not found. This is a typical consequence of choosing to design an API around sizes and lengths, and vanishes when moving to pointer-based similar APIs. This patch set adds strprefix(), deprecates str_has_prefix(), and uses strprefix() in one place to fix a bug reported by Bartosz. We can later work on replacing the other uses of str_has_prefix(), which might hide similar bugs. Changes in v2 ~~~~~~~~~~~~~ - Added tests, as suggested by Andy. - Remove Cc tags from the commit messages. [Andy] Testing ~~~~~~~ The new tests added for strprefix() pass. Below is the session I ran for testing. The important line is: [13:26:30] [PASSED] string_test_strprefix Full session: $ cat .kunitconfig CONFIG_KUNIT=y CONFIG_STRING_KUNIT_TEST=y $ mkdir -p ~/tmp $ export TMPDIR=$HOME/tmp $ ./tools/testing/kunit/kunit.py run "string*" [13:26:28] Configuring KUnit Kernel ... [13:26:28] Building KUnit Kernel ... Populating config with: $ make ARCH=um O=.kunit olddefconfig Building with: $ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=24 [13:26:30] Starting KUnit Kernel (1/1)... [13:26:30] ============================================================ Running tests with: $ .kunit/linux 'kunit.filter_glob=string*' kunit.enable=1 mem=1G console=tty kunit_shutdown=halt [13:26:30] ============= string-stream-test (12 subtests) ============= [13:26:30] [PASSED] string_stream_managed_init_test [13:26:30] [PASSED] string_stream_unmanaged_init_test [13:26:30] [PASSED] string_stream_managed_free_test [13:26:30] [PASSED] string_stream_resource_free_test [13:26:30] [PASSED] string_stream_line_add_test [13:26:30] [PASSED] string_stream_variable_length_line_test [13:26:30] [PASSED] string_stream_append_test [13:26:30] [PASSED] string_stream_append_auto_newline_test [13:26:30] [PASSED] string_stream_append_empty_string_test [13:26:30] [PASSED] string_stream_no_auto_newline_test [13:26:30] [PASSED] string_stream_auto_newline_test [13:26:30] [PASSED] string_stream_performance_test [13:26:30] =============== [PASSED] string-stream-test ================ [13:26:30] =================== string (29 subtests) =================== [13:26:30] [PASSED] string_test_memset16 [13:26:30] [PASSED] string_test_memset32 [13:26:30] [PASSED] string_test_memset64 [13:26:30] [PASSED] string_test_strlen [13:26:30] [PASSED] string_test_strnlen [13:26:30] [PASSED] string_test_strchr [13:26:30] [PASSED] string_test_strnchr [13:26:30] [PASSED] string_test_strrchr [13:26:30] [PASSED] string_test_strspn [13:26:30] [PASSED] string_test_strprefix [13:26:30] [PASSED] string_test_strcmp [13:26:30] [PASSED] string_test_strcmp_long_strings [13:26:30] [PASSED] string_test_strncmp [13:26:30] [PASSED] string_test_strncmp_long_strings [13:26:30] [PASSED] string_test_strcasecmp [13:26:30] [PASSED] string_test_strcasecmp_long_strings [13:26:30] [PASSED] string_test_strncasecmp [13:26:30] [PASSED] string_test_strncasecmp_long_strings [13:26:30] [PASSED] string_test_strscpy [13:26:30] [PASSED] string_test_strcat [13:26:30] [PASSED] string_test_strncat [13:26:30] [PASSED] string_test_strlcat [13:26:30] [PASSED] string_test_strtomem [13:26:30] [PASSED] string_test_memtostr [13:26:30] [PASSED] string_test_strends [13:26:30] [SKIPPED] string_bench_strlen (not enabled) [13:26:30] [SKIPPED] string_bench_strnlen (not enabled) [13:26:30] [SKIPPED] string_bench_strchr (not enabled) [13:26:30] [SKIPPED] string_bench_strrchr (not enabled) [13:26:30] ===================== [PASSED] string ====================== [13:26:30] =============== string_helpers (4 subtests) ================ [13:26:30] [PASSED] test_get_size [13:26:30] [PASSED] test_upper_lower [13:26:30] [PASSED] test_unescape [13:26:30] [PASSED] test_escape [13:26:30] ================= [PASSED] string_helpers ================== [13:26:30] ============================================================ [13:26:30] Testing complete. Ran 45 tests: passed: 41, skipped: 4 [13:26:31] Elapsed time: 2.509s total, 0.001s configuring, 2.192s building, 0.271s running Cheers, Alex Alejandro Colomar (2): string: Add strprefix(), and deprecate str_has_prefix() device property: Fix fwnode_name_eq() for empty node names drivers/base/property.c | 5 +---- include/linux/string.h | 19 ++++++++++++++++++- lib/tests/string_kunit.c | 9 +++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) Range-diff against v1: 1: 9c81f6f62fd0 ! 1: d3b325ecefd1 string: Add strprefix(), and deprecate str_has_prefix() @@ Commit message We'll slowly migrate existing code from str_has_prefix() to strprefix(). Reported-by: Bartosz Golaszewski - Cc: Andreas Schwab - Cc: Andy Shevchenko Cc: Andy Shevchenko - Cc: Bartosz Golaszewski - Cc: Daniel Scally - Cc: Danilo Krummrich - Cc: Greg Kroah-Hartman - Cc: Hans Verkuil - Cc: Heikki Krogerus - Cc: Joe Perches - Cc: Johan Hovold - Cc: Kees Cook - Cc: Laurent Pinchart - Cc: Len Brown - Cc: Linus Torvalds - Cc: Linus Walleij - Cc: Namhyung Kim - Cc: "Rafael J. Wysocki" - Cc: Rob Herring - Cc: Sakari Ailus - Cc: Saravana Kannan - Cc: Steven Rostedt - Cc: Tom Zanussi ## include/linux/string.h ## @@ include/linux/string.h: void memcpy_and_pad(void *dest, size_t dest_len, const v * * Returns: * * strlen(@prefix) if @str starts with @prefix + + ## lib/tests/string_kunit.c ## +@@ lib/tests/string_kunit.c: static void string_test_strspn(struct kunit *test) + } + } + ++static void string_test_strprefix(struct kunit *test) ++{ ++ KUNIT_ASSERT_EQ(test, 0, strcmp(strprefix("foobar", "foo"), "bar")); ++ KUNIT_ASSERT_EQ(test, 0, strcmp(strprefix("foobar", ""), "foobar")); ++ KUNIT_ASSERT_EQ(test, 0, strcmp(strprefix("foobar", "foobar"), "")); ++ KUNIT_ASSERT_NULL(test, strprefix("foobar", "bar")); ++} ++ + static char strcmp_buffer1[STRCMP_LARGE_BUF_LEN]; + static char strcmp_buffer2[STRCMP_LARGE_BUF_LEN]; + +@@ lib/tests/string_kunit.c: static struct kunit_case string_test_cases[] = { + KUNIT_CASE(string_test_strnchr), + KUNIT_CASE(string_test_strrchr), + KUNIT_CASE(string_test_strspn), ++ KUNIT_CASE(string_test_strprefix), + KUNIT_CASE(string_test_strcmp), + KUNIT_CASE(string_test_strcmp_long_strings), + KUNIT_CASE(string_test_strncmp), 2: 37eafcd682b0 ! 2: ac9728afa027 device property: Fix fwnode_name_eq() for empty node names @@ Commit message Fixes: a9c8c738066b7 ("device property: Add fwnode_name_eq()") Reported-by: Bartosz Golaszewski - Cc: Andreas Schwab - Cc: Andy Shevchenko Cc: Andy Shevchenko - Cc: Bartosz Golaszewski - Cc: Daniel Scally - Cc: Danilo Krummrich - Cc: Greg Kroah-Hartman - Cc: Hans Verkuil - Cc: Heikki Krogerus - Cc: Joe Perches - Cc: Johan Hovold - Cc: Kees Cook - Cc: Laurent Pinchart - Cc: Len Brown - Cc: Linus Torvalds - Cc: Linus Walleij - Cc: Namhyung Kim - Cc: "Rafael J. Wysocki" - Cc: Rob Herring - Cc: Sakari Ailus - Cc: Saravana Kannan - Cc: Steven Rostedt - Cc: Tom Zanussi ## drivers/base/property.c ## -- 2.55.0