Skip to contentSkip to conversation

Add strprefix() and fix a bug with it

PATCH Superseded Alejandro Colomar 4 participants 26 messages Nov 13

Subsystems (6)
ACPI Library code GPIO Driver core +2 more subsystems

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover Add strprefix() and fix a bug with it

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.

According to its author (Steven R.), str_has_prefix() was never meant to accept an empty prefix, but that's not documented, and there's no obvious reason why that would be acceptable for a generic string function (as opposed to a function very specific to its original use). Anyway, as suggested by Steven, I've added a WARN_ON_ONCE() to catch such uses, and also added documentation stating this limitation.

Users of strings need a function whose behavior extends naturally to the empty string, and we can do better, by returning a pointer. And indeed, users have ended up using str_has_prefix() with empty prefix, expecting it to work (as is natural of a generic string API).

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.

This patch set also adds infrastructure to make string functions return a pointer that is as qualified as the input pointer.

Changes in v5
~~~~~~~~~~~~~

- [1/7]
- Fix .rst formatting. [sashiko]
- [2/7]
- Move the function to "lib/string.h" to be able to use

      WARN_ON_ONCE().  [Andy, Bart]

- [3/7] -- no changes --
- [4/7]
- Simplify const_cast(), which makes it more robust. 'T:' didn't work, because in case const T is the same type as T, there's ambiguity in which branch should be taken, and the compiler rejects that. We could go back to 'default:' (as in v3), but that is only needed for casting 'const char **' to 'char **'; since we don't need that at the moment, let's keep it simple, and we'll be able to add it in the future if we ever want that.

-  [5/7]  -- no changes --
-  [6/7]  -- no changes --
-  [7/7]

- New patch; use strprefix() to implement strstarts().

I didn't apply some changes requrested by reviewers:

- I didn't move strprefix() to "lib/string.h" implementing it as a loop with manual byte operations. The only reason for that seems to be performance, but I expect by being inline, the compiler should be able to completely drop the strlen(3) call in many cases, making the inline function faster.

See range-diff below.

v1: <https://lore.kernel.org/linux-hardening/[email protected]/>
v2: <https://lore.kernel.org/linux-hardening/[email protected]/>
v3: <https://lore.kernel.org/linux-hardening/[email protected]/>
v4: <https://lore.kernel.org/linux-hardening/[email protected]/>

Testing
~~~~~~~

(I've indented the output, to better see the commands.)

$ git log --oneline --graph v7.4-rc1^..strprefix5
	* 382a44f7c686 (HEAD -> strprefix, tag: strprefix5) string: Implement strstarts() with strprefix()
	* 0ca0b7aabcfc string: Make strprefix() const-qualifier-generic
	* 1294b3d64440 string: Add [C]QChar_of() and QVoid_of()
	* b615b5afb05e stddef: Add const_cast()
	* 49252e78c9b6 device property: Fix fwnode_name_eq() for empty node names
	* df3a35c469c6 string: Warn if the prefix is the empty string
	* f25bf43d7f18 string: Add strprefix(), and deprecate str_has_prefix()
	* 6628e7d4c6b2 (tag: v7.4-rc1, master) Linux 7.4-rc1
$ cat .kunitconfig
	CONFIG_KUNIT=y
	CONFIG_STRING_KUNIT_TEST=y
$ mkdir -p ~/tmp/strprefix5
$ export TMPDIR=$HOME/tmp/strprefix5/
$ ./tools/testing/kunit/kunit.py run "string*"
	[13:27:47] Configuring KUnit Kernel ...
	[13:27:47] 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:28:02] Starting KUnit Kernel (1/1)...
	[13:28:02] ============================================================
	Running tests with:
	$ .kunit/linux 'kunit.filter_glob=string*' kunit.enable=1 mem=1G console=tty kunit_shutdown=halt
	[13:28:02] ============= string-stream-test (12 subtests) =============
	[13:28:02] [PASSED] string_stream_managed_init_test
	[13:28:02] [PASSED] string_stream_unmanaged_init_test
	[13:28:02] [PASSED] string_stream_managed_free_test
	[13:28:02] [PASSED] string_stream_resource_free_test
	[13:28:02] [PASSED] string_stream_line_add_test
	[13:28:02] [PASSED] string_stream_variable_length_line_test
	[13:28:02] [PASSED] string_stream_append_test
	[13:28:02] [PASSED] string_stream_append_auto_newline_test
	[13:28:02] [PASSED] string_stream_append_empty_string_test
	[13:28:02] [PASSED] string_stream_no_auto_newline_test
	[13:28:02] [PASSED] string_stream_auto_newline_test
	[13:28:02] [PASSED] string_stream_performance_test
	[13:28:02] =============== [PASSED] string-stream-test ================
	[13:28:02] =================== string (29 subtests) ===================
	[13:28:02] [PASSED] string_test_memset16
	[13:28:02] [PASSED] string_test_memset32
	[13:28:02] [PASSED] string_test_memset64
	[13:28:02] [PASSED] string_test_strlen
	[13:28:02] [PASSED] string_test_strnlen
	[13:28:02] [PASSED] string_test_strchr
	[13:28:02] [PASSED] string_test_strnchr
	[13:28:02] [PASSED] string_test_strrchr
	[13:28:02] [PASSED] string_test_strspn
	[13:28:02] [PASSED] string_test_strprefix
	[13:28:02] [PASSED] string_test_strcmp
	[13:28:02] [PASSED] string_test_strcmp_long_strings
	[13:28:02] [PASSED] string_test_strncmp
	[13:28:02] [PASSED] string_test_strncmp_long_strings
	[13:28:02] [PASSED] string_test_strcasecmp
	[13:28:02] [PASSED] string_test_strcasecmp_long_strings
	[13:28:02] [PASSED] string_test_strncasecmp
	[13:28:02] [PASSED] string_test_strncasecmp_long_strings
	[13:28:02] [PASSED] string_test_strscpy
	[13:28:02] [PASSED] string_test_strcat
	[13:28:02] [PASSED] string_test_strncat
	[13:28:02] [PASSED] string_test_strlcat
	[13:28:02] [PASSED] string_test_strtomem
	[13:28:02] [PASSED] string_test_memtostr
	[13:28:02] [PASSED] string_test_strends
	[13:28:02] [SKIPPED] string_bench_strlen (not enabled)
	[13:28:02] [SKIPPED] string_bench_strnlen (not enabled)
	[13:28:02] [SKIPPED] string_bench_strchr (not enabled)
	[13:28:02] [SKIPPED] string_bench_strrchr (not enabled)
	[13:28:02] ===================== [PASSED] string ======================
	[13:28:02] =============== string_helpers (4 subtests) ================
	[13:28:02] [PASSED] test_get_size
	[13:28:02] [PASSED] test_upper_lower
	[13:28:02] [PASSED] test_unescape
	[13:28:02] [PASSED] test_escape
	[13:28:02] ================= [PASSED] string_helpers ==================
	[13:28:02] ============================================================
	[13:28:02] Testing complete. Ran 45 tests: passed: 41, skipped: 4
	[13:28:02] Elapsed time: 15.583s total, 0.000s configuring, 15.366s building, 0.172s running
$ make allmodconfig
	  HOSTCC  scripts/basic/fixdep
	  HOSTCC  scripts/kconfig/conf.o
	  HOSTCC  scripts/kconfig/confdata.o
	  HOSTCC  scripts/kconfig/expr.o
	  LEX     scripts/kconfig/lexer.lex.c
	  YACC    scripts/kconfig/parser.tab.[ch]
	  HOSTCC  scripts/kconfig/lexer.lex.o
	  HOSTCC  scripts/kconfig/menu.o
	  HOSTCC  scripts/kconfig/parser.tab.o
	  HOSTCC  scripts/kconfig/preprocess.o
	  HOSTCC  scripts/kconfig/symbol.o
	  HOSTCC  scripts/kconfig/util.o
	  HOSTLD  scripts/kconfig/conf
	#
	# configuration written to .config
	#
$ time make -j24 | ts -s
	00:00:01   GEN     arch/x86/include/generated/asm/orc_hash.h
	00:00:01   UPD     arch/x86/include/generated/uapi/asm/bpf_perf_event.h
	00:00:01   UPD     arch/x86/include/generated/uapi/asm/errno.h
...
	00:21:49   LD [M]  net/qrtr/qrtr-tun.ko
	00:21:49   LD [M]  net/qrtr/qrtr-mhi.ko
	00:21:49   LD [M]  virt/lib/irqbypass.ko
	real	21m48.882s
	user	439m26.908s
	sys	25m20.132s

Have a lovely day!
Alex

Alejandro Colomar (7):
string: Add strprefix(), and deprecate str_has_prefix()
string: Warn if the prefix is the empty string device property: Fix fwnode_name_eq() for empty node names
stddef: Add const_cast()
string: Add [C]QChar_of() and QVoid_of()
string: Make strprefix() const-qualifier-generic
string: Implement strstarts() with strprefix()

 drivers/base/property.c  |  5 +---
 include/linux/stddef.h   | 11 +++++++++
 include/linux/string.h   | 52 ++++++++++++++++++++++++++++++++--------
 lib/string.c             | 24 +++++++++++++++++++
 lib/tests/string_kunit.c |  9 +++++++
 5 files changed, 87 insertions(+), 14 deletions(-)

Range-diff against v4:
1: a74fabd985a8 ! 1: f25bf43d7f18 string: Add strprefix(), and deprecate str_has_prefix()
@@ include/linux/string.h: void memcpy_and_pad(void *dest, size_t dest_len, const v
+ * and not a constant. Instead use strprefix().
+ *
+ * Returns:
-+ * * @s+strlen(@prefix) if @s starts with @prefix
++ * * @s + strlen(@prefix) if @s starts with @prefix
+ * * NULL if @s does not start with @prefix
+ */
+static inline char *strprefix(const char *s, const char *prefix)
2: cd09427148a0 ! 2: df3a35c469c6 string: Warn if the prefix is the empty string
@@ Commit message
Warn if it happens. Also, document that if such corner case
is needed, strprefix() is preferable.

+ We need to move str_has_prefix() to "lib/string.h" to be able to use
+ WARN_ON_ONCE().
+

+ Cc: Andy Shevchenko <[email protected]>

## include/linux/string.h ##
+@@ include/linux/string.h: extern char * strstr(const char *, const char *);
+ #ifndef __HAVE_ARCH_STRNSTR
+ extern char * strnstr(const char *, const char *, size_t);
+ #endif
++extern size_t str_has_prefix(const char *str, const char *prefix);
+ #ifndef __HAVE_ARCH_STRLEN
+ extern __kernel_size_t strlen(const char *);
+ #endif
@@ include/linux/string.h: static inline char *strprefix(const char *s, const char *prefix)
- * This is similar to strprefix(), but returns an offset instead of
- * a pointer.
- *
+ return strncmp(s, prefix, len) == 0 ? (char *) s + len : NULL;
+ }
+
+-/**
+- * str_has_prefix - Test if a string has a given prefix
+- * @str: The string to test
+- * @prefix: The string to see if @str starts with
+- *
+- * This is similar to strprefix(), but returns an offset instead of
+- * a pointer.
+- *
+- * Returns:
+- * * strlen(@prefix) if @str starts with @prefix
+- * * 0 if @str does not start with @prefix
+- */
+-static __always_inline size_t str_has_prefix(const char *str, const char *prefix) +-{
+- size_t len = strlen(prefix);
+- return strncmp(str, prefix, len) == 0 ? len : 0;
+-}
+-
+ /**
+ * strstarts - does @str start with @prefix?
+ * @str: string to examine
+
+ ## lib/string.c ##
+@@ lib/string.c: char *strnstr(const char *s1, const char *s2, size_t len)
+ EXPORT_SYMBOL(strnstr);
+ #endif
+
++/**
++ * str_has_prefix - Test if a string has a given prefix
++ * @str: The string to test
++ * @prefix: The string to see if @str starts with
++ *
++ * This is similar to strprefix(), but returns an offset instead of
++ * a pointer.
++ *
+ * Unlike for strprefix(), if the prefix is an empty string, the
+ * behavior is undefined. If you need to support arbitrary prefixes,
+ * use strprefix().
+ *
- * Returns:
- * * strlen(@prefix) if @str starts with @prefix
- * * 0 if @str does not start with @prefix
-@@ include/linux/string.h: static inline char *strprefix(const char *s, const char *prefix)
- static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
- {
- size_t len = strlen(prefix);
++ * Returns:
++ * * strlen(@prefix) if @str starts with @prefix
++ * * 0 if @str does not start with @prefix
++ */
++size_t str_has_prefix(const char *str, const char *prefix)
++{
++ size_t len = strlen(prefix);
+ WARN_ON_ONCE(len == 0);
- return strncmp(str, prefix, len) == 0 ? len : 0;
- }
-
++ return strncmp(str, prefix, len) == 0 ? len : 0;
++}
++EXPORT_SYMBOL(str_has_prefix);
++
+ #ifndef __HAVE_ARCH_MEMCHR
+ /**
+ * memchr - Find a character in an area of memory.

3:  0058b60f662c = 3:  49252e78c9b6 device property: Fix fwnode_name_eq() for empty node names
4:  ce623fe0432b ! 4:  b615b5afb05e stddef: Add const_cast()

@@ Commit message
It's similar to C++'s const_cast(), but it is more restrictive: it can only cast away 'const', and not arbitrary qualifiers.

- David made it a bit more robust.
-
- Link: https://lore.kernel.org/linux-hardening/20261111102535.434c2eee@pumpkin/
- Co-developed-by: David Laight <[email protected]>
+ Cc: David Laight <[email protected]>

## include/linux/stddef.h ##
@@ include/linux/stddef.h: enum {
+ * @T: Type for the cast.
+ * @p: Pointer to be casted.
+ *
-+ * If @p is of type const @T, then it is casted to @T.
-+ * If @p is of type @T, then it is returned without any casts. ++ * Cast @p to type @T. The macro verifies that @p is of type const @T.
+ * This avoids mistakes that change anything other than 'const' in a cast.
+ */
-+#define const_cast(T, p) _Generic(p, const T: (T) (p), T: (p)) ++#define const_cast(T, p) _Generic(p, const T: (T) (p))
+
#endif

5:  206d2045abcf = 5:  1294b3d64440 string: Add [C]QChar_of() and QVoid_of()
6:  69d1792e5edf ! 6:  0ca0b7aabcfc string: Make strprefix() const-qualifier-generic

@@ Commit message

## include/linux/string.h ##
@@ include/linux/string.h: void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
- * * @s+strlen(@prefix) if @s starts with @prefix
+ * * @s + strlen(@prefix) if @s starts with @prefix
* * NULL if @s does not start with @prefix
*/
-static inline char *strprefix(const char *s, const char *prefix)
@@ include/linux/string.h: void memcpy_and_pad(void *dest, size_t dest_len, const v
+#define strprefix(s, pfx) const_cast(CQChar_of(s) *, strprefix(s, pfx))

/**
- * str_has_prefix - Test if a string has a given prefix
+ * strstarts - does @str start with @prefix?
-: ------------ > 7: 382a44f7c686 string: Implement strstarts() with strprefix()

Signature · 1 line
2.55.0
#1 lore raw

PATCH v5 1/7 string: Add strprefix(), and deprecate str_has_prefix()

As Bartosz reported, str_has_prefix() has a bug: it can't differentiate between an empty prefix matching an empty string from a non-matching prefix. This is a fundamental design flaw in this API, and not something that can be fixed.

As always, this is a consequence of choosing sizes instead of pointers. Sizes are often the wrong choice. In this specific case, pointers are more capable of differentiating these cases, as NULL is a special sentinel value that doesn't need to overload a valid value.

Let's implement strprefix(), a function that returns a pointer right after the prefix if found, and a null pointer if not found.

We'll slowly migrate existing code from str_has_prefix() to strprefix().

Move the description from str_has_prefix(), and describe str_has_prefix() by comparing it to strprefix().

---
 include/linux/string.h   | 28 +++++++++++++++++++++++-----
 lib/tests/string_kunit.c |  9 +++++++++
 2 files changed, 32 insertions(+), 5 deletions(-)
include/linux/string.h +23 -5
index 6cb5cdd01158..04e8a8c0f05a 100644
@@ -545,16 +545,34 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
 	       sizeof(*(obj)) - offsetof(typeof(*(obj)), member));	\
 })
 
+/**
+ * strprefix - string prefix
+ * @s: The string to be tested
+ * @prefix: The string to see if @s starts with
+ *
+ * A common way to test a prefix of a string is to do:
+ *  strncmp(s, prefix, sizeof(prefix) - 1)
+ *
+ * But this can lead to bugs due to typos, or if prefix is a pointer
+ * and not a constant.  Instead use strprefix().
+ *
+ * Returns:
+ * *  @s + strlen(@prefix) if @s starts with @prefix
+ * *  NULL if @s does not start with @prefix
+ */
+static inline char *strprefix(const char *s, const char *prefix)
+{
+	size_t len = strlen(prefix);
+	return strncmp(s, prefix, len) == 0 ? (char *) s + len : NULL;
+}
+
 /**
  * str_has_prefix - Test if a string has a given prefix
  * @str: The string to test
  * @prefix: The string to see if @str starts with
  *
- * A common way to test a prefix of a string is to do:
- *  strncmp(str, prefix, sizeof(prefix) - 1)
- *
- * But this can lead to bugs due to typos, or if prefix is a pointer
- * and not a constant. Instead use str_has_prefix().
+ * This is similar to strprefix(), but returns an offset instead of
+ * a pointer.
  *
  * Returns:
  * * strlen(@prefix) if @str starts with @prefix
lib/tests/string_kunit.c +9 -0
index 0819ace5b027..73d2614b1474 100644
@@ -287,6 +287,14 @@ 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];
 
@@ -891,6 +899,7 @@ 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),
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v5 1/7] string: Add strprefix(), and deprecate str_has_prefix()

Quoted 13 lines · Alejandro Colomar

As Bartosz reported, str_has_prefix() has a bug: it can't differentiate between an empty prefix matching an empty string from a non-matching prefix. This is a fundamental design flaw in this API, and not something that can be fixed.

As always, this is a consequence of choosing sizes instead of pointers. Sizes are often the wrong choice. In this specific case, pointers are more capable of differentiating these cases, as NULL is a special sentinel value that doesn't need to overload a valid value.

Let's implement strprefix(), a function that returns a pointer right after the prefix if found, and a null pointer if not found.

Quoted 7 lines

We'll slowly migrate existing code from str_has_prefix() to strprefix().

Move the description from str_has_prefix(), and describe str_has_prefix() by comparing it to strprefix().

It's unreferred Link tag, spurious? Please, refer to it in the commit message to understand why it's here. Use Link: $URL [1] and use [1] in the text.

Quoted 1 line
Cc Andy Shevchenko <[email protected]>

No need to put my name explicitly in Cc list, I'm a designated reviewer for string library, you should get it via b4 or get_maintainer.pl. And in general is better to just move the Cc after the '---' line.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH v5 1/7] string: Add strprefix(), and deprecate str_has_prefix()

Quoted 16 lines · Andy Shevchenko
Quoted 15 lines · Alejandro Colomar

As Bartosz reported, str_has_prefix() has a bug: it can't differentiate between an empty prefix matching an empty string from a non-matching prefix. This is a fundamental design flaw in this API, and not something that can be fixed.

As always, this is a consequence of choosing sizes instead of pointers. Sizes are often the wrong choice. In this specific case, pointers are more capable of differentiating these cases, as NULL is a special sentinel value that doesn't need to overload a valid value.

Let's implement strprefix(), a function that returns a pointer right after the prefix if found, and a null pointer if not found.

We'll slowly migrate existing code from str_has_prefix() to strprefix().

Quoted 8 lines
Quoted 5 lines

Move the description from str_has_prefix(), and describe str_has_prefix() by comparing it to strprefix().

It's unreferred Link tag, spurious? Please, refer to it in the commit message to understand why it's here. Use Link: $URL [1] and use [1] in the text.

Ah, perhaps you wanted Closes tag instead?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Re: [PATCH v5 1/7] string: Add strprefix(), and deprecate str_has_prefix()

Hi Andy,

Quoted 23 lines

Date: 2026-11-13 16:34:13+0200
From: Andy Shevchenko <[email protected]>

Quoted 19 lines · Andy Shevchenko
Quoted 18 lines · Alejandro Colomar

As Bartosz reported, str_has_prefix() has a bug: it can't differentiate between an empty prefix matching an empty string from a non-matching prefix. This is a fundamental design flaw in this API, and not something that can be fixed.

As always, this is a consequence of choosing sizes instead of pointers. Sizes are often the wrong choice. In this specific case, pointers are more capable of differentiating these cases, as NULL is a special sentinel value that doesn't need to overload a valid value.

Let's implement strprefix(), a function that returns a pointer right after the prefix if found, and a null pointer if not found.

We'll slowly migrate existing code from str_has_prefix() to strprefix().

Move the description from str_has_prefix(), and describe str_has_prefix() by comparing it to strprefix().

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

It's unreferred Link tag, spurious? Please, refer to it in the commit message to understand why it's here. Use Link: $URL [1] and use [1] in the text.

Ah, perhaps you wanted Closes tag instead?

Yeah, it was for linking to Bart's report. BTW, is it okay to have two consecutive patches with the same Closes tag? I guess it is, because they close different aspects of the same issue. But at the same time, the first one doesn't fully close the link. I guess yeah, I should just put it in both. Thanks!

Cheers,
Alex

Signature · 1 line
<https://www.alejandro-colomar.es>
#5 lore raw

Re: [PATCH v5 1/7] string: Add strprefix(), and deprecate str_has_prefix()

Quoted 2 lines · Alejandro Colomar
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Alejandro Colomar

...

Quoted 10 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

It's unreferred Link tag, spurious? Please, refer to it in the commit message to understand why it's here. Use Link: $URL [1] and use [1] in the text.

Ah, perhaps you wanted Closes tag instead?

Yeah, it was for linking to Bart's report. BTW, is it okay to have two consecutive patches with the same Closes tag?

I think so.

Quoted 3 lines

I guess it is, because they close different aspects of the same issue. But at the same time, the first one doesn't fully close the link. I guess yeah, I should just put it in both. Thanks!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

PATCH v5 2/7 string: Warn if the prefix is the empty string

In reply to Alejandro Colomar

This corner case is mishandled by this function. Warn if it happens. Also, document that if such corner case is needed, strprefix() is preferable.

We need to move str_has_prefix() to "lib/string.h" to be able to use WARN_ON_ONCE().

---
 include/linux/string.h | 19 +------------------
 lib/string.c           | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+), 18 deletions(-)
include/linux/string.h +1 -18
index 04e8a8c0f05a..dcda1b7801bb 100644
@@ -194,6 +194,7 @@ extern char * strstr(const char *, const char *);
 #ifndef __HAVE_ARCH_STRNSTR
 extern char * strnstr(const char *, const char *, size_t);
 #endif
+extern size_t str_has_prefix(const char *str, const char *prefix);
 #ifndef __HAVE_ARCH_STRLEN
 extern __kernel_size_t strlen(const char *);
 #endif
@@ -566,24 +567,6 @@ static inline char *strprefix(const char *s, const char *prefix)
 	return strncmp(s, prefix, len) == 0 ? (char *) s + len : NULL;
 }
 
-/**
- * str_has_prefix - Test if a string has a given prefix
- * @str: The string to test
- * @prefix: The string to see if @str starts with
- *
- * This is similar to strprefix(), but returns an offset instead of
- * a pointer.
- *
- * Returns:
- * * strlen(@prefix) if @str starts with @prefix
- * * 0 if @str does not start with @prefix
- */
-static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
-{
-	size_t len = strlen(prefix);
-	return strncmp(str, prefix, len) == 0 ? len : 0;
-}
-
 /**
  * strstarts - does @str start with @prefix?
  * @str: string to examine
lib/string.c +24 -0
index a3778d5aab4a..61a1e79c1b87 100644
@@ -774,6 +774,30 @@ char *strnstr(const char *s1, const char *s2, size_t len)
 EXPORT_SYMBOL(strnstr);
 #endif
 
+/**
+ * str_has_prefix - Test if a string has a given prefix
+ * @str: The string to test
+ * @prefix: The string to see if @str starts with
+ *
+ * This is similar to strprefix(), but returns an offset instead of
+ * a pointer.
+ *
+ * Unlike for strprefix(), if the prefix is an empty string, the
+ * behavior is undefined.  If you need to support arbitrary prefixes,
+ * use strprefix().
+ *
+ * Returns:
+ * * strlen(@prefix) if @str starts with @prefix
+ * * 0 if @str does not start with @prefix
+ */
+size_t str_has_prefix(const char *str, const char *prefix)
+{
+	size_t len = strlen(prefix);
+	WARN_ON_ONCE(len == 0);
+	return strncmp(str, prefix, len) == 0 ? len : 0;
+}
+EXPORT_SYMBOL(str_has_prefix);
+
 #ifndef __HAVE_ARCH_MEMCHR
 /**
  * memchr - Find a character in an area of memory.
Signature · 1 line
2.55.0
#7 lore raw

Re: [PATCH v5 2/7] string: Warn if the prefix is the empty string

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] string: moving str_has_prefix() to lib/string.c bypasses FORTIFY_SOURCE - [Low] string: commit…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] string: moving str_has_prefix() to lib/string.c bypasses FORTIFY_SOURCE
- [Low] string: commit message cites non-existent lib/string.h and omits function name
--

commit 018109c1aa9a2d5e3def240ae4b9134b27f492d5
Author: Alejandro Colomar <[email protected]>

Quoted 6 lines

string: Warn if the prefix is the empty string

This corner case is mishandled by this function. Warn if it happens. Also, document that if such corner case is needed, strprefix() is preferable.

Quoted 8 lines

We need to move str_has_prefix() to "lib/string.h" to be able to use WARN_ON_ONCE().

[Severity: Low]
This isn't a bug, but the commit message says str_has_prefix() is moved to "lib/string.h", which does not exist; the patch moves it to lib/string.c. Also, neither the subject line nor the opening sentence names str_has_prefix().

[ ... ]

Quoted 21 lines
lib/string.c +13 -0
index a3778d5aab4af..61a1e79c1b875 100644
@@ -774,6 +774,30 @@ char *strnstr(const char *s1, const char *s2, size_t len)
 EXPORT_SYMBOL(strnstr);
 #endif
 
+/**
+ * str_has_prefix - Test if a string has a given prefix
+ * @str: The string to test
+ * @prefix: The string to see if @str starts with
+ *
+ * This is similar to strprefix(), but returns an offset instead of
+ * a pointer.
+ *
+ * Unlike for strprefix(), if the prefix is an empty string, the
+ * behavior is undefined.  If you need to support arbitrary prefixes,
+ * use strprefix().
+ *
+ * Returns:

lib/string.c char *strnstr(const char *s1, const char *s2, size_t len) · line 799

Quoted 10 lines
lib/string.c +10 -0
+ * * strlen(@prefix) if @str starts with @prefix
+ * * 0 if @str does not start with @prefix
+ */
+size_t str_has_prefix(const char *str, const char *prefix)
+{
+	size_t len = strlen(prefix);
+	WARN_ON_ONCE(len == 0);
+	return strncmp(str, prefix, len) == 0 ? len : 0;
+}
+EXPORT_SYMBOL(str_has_prefix);

[Severity: Low]
Does moving str_has_prefix() out-of-line into lib/string.c bypass CONFIG_FORTIFY_SOURCE and prevent compile-time constant folding of strlen(prefix)?

lib/string.c is built with __NO_FORTIFY and its top-of-file comment notes that it is reserved for routines that have architecture-specific alternatives in <asm/string.h> or get overloaded by FORTIFY_SOURCE, while other helpers belong in lib/string_helpers.c.

When str_has_prefix() was a static __always_inline helper in include/linux/string.h (introduced in 72921427d46b ("string.h: Add str_has_prefix() helper function")), strlen(prefix) folded string-literal prefixes into constants at compile time and ran fortified bounds checks on non-constant arrays. Moving it to lib/string.c forces an out-of-line call and an unfortified runtime strlen() scan on every invocation, and shares a single WARN_ON_ONCE() state across all callers in the kernel.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#8 lore raw

Re: [PATCH v5 2/7] string: Warn if the prefix is the empty string

Quoted 29 lines

Date: 2026-11-13 13:26:21+0000
From: [email protected]

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] string: moving str_has_prefix() to lib/string.c bypasses FORTIFY_SOURCE
- [Low] string: commit message cites non-existent lib/string.h and omits function name
--

commit 018109c1aa9a2d5e3def240ae4b9134b27f492d5
Author: Alejandro Colomar <[email protected]>

Quoted 6 lines

string: Warn if the prefix is the empty string

This corner case is mishandled by this function. Warn if it happens. Also, document that if such corner case is needed, strprefix() is preferable.

Quoted 8 lines

We need to move str_has_prefix() to "lib/string.h" to be able to use WARN_ON_ONCE().

Quoted 5 lines

[Severity: Low]
This isn't a bug, but the commit message says str_has_prefix() is moved to "lib/string.h", which does not exist; the patch moves it to lib/string.c. Also, neither the subject line nor the opening sentence names str_has_prefix().

Oops, typo and thinko. Will fix for v6.

Quoted 31 lines

[ ... ]

Quoted 28 lines
lib/string.c +20 -0
index a3778d5aab4af..61a1e79c1b875 100644
@@ -774,6 +774,30 @@ char *strnstr(const char *s1, const char *s2, size_t len)
 EXPORT_SYMBOL(strnstr);
 #endif
 
+/**
+ * str_has_prefix - Test if a string has a given prefix
+ * @str: The string to test
+ * @prefix: The string to see if @str starts with
+ *
+ * This is similar to strprefix(), but returns an offset instead of
+ * a pointer.
+ *
+ * Unlike for strprefix(), if the prefix is an empty string, the
+ * behavior is undefined.  If you need to support arbitrary prefixes,
+ * use strprefix().
+ *
+ * Returns:
+ * * strlen(@prefix) if @str starts with @prefix
+ * * 0 if @str does not start with @prefix
+ */
+size_t str_has_prefix(const char *str, const char *prefix)
+{
+	size_t len = strlen(prefix);
+	WARN_ON_ONCE(len == 0);
Quoted 8 lines

lib/string.c char *strnstr(const char *s1, const char *s2, size_t len) · line 799

Quoted 3 lines
lib/string.c +3 -0
+	return strncmp(str, prefix, len) == 0 ? len : 0;
+}
+EXPORT_SYMBOL(str_has_prefix);

[Severity: Low]
Does moving str_has_prefix() out-of-line into lib/string.c bypass CONFIG_FORTIFY_SOURCE and prevent compile-time constant folding of strlen(prefix)?

Preventing the compile-time constant folding of strlen(3) is indeed a performance degradation, but that's necessary to be able to WARN_ON_ONCE(), so there isn't anything we can do.

Quoted 5 lines

lib/string.c is built with __NO_FORTIFY and its top-of-file comment notes that it is reserved for routines that have architecture-specific alternatives in <asm/string.h> or get overloaded by FORTIFY_SOURCE, while other helpers belong in lib/string_helpers.c.

Do we want to move it to "lib/string_helpers.c"? I don't know enough about the organization in these files, so just let me know what I should do.

Quoted 7 lines

When str_has_prefix() was a static __always_inline helper in include/linux/string.h (introduced in 72921427d46b ("string.h: Add str_has_prefix() helper function")), strlen(prefix) folded string-literal prefixes into constants at compile time and ran fortified bounds checks on non-constant arrays. Moving it to lib/string.c forces an out-of-line call and an unfortified runtime strlen() scan on every invocation, and shares a single WARN_ON_ONCE() state across all callers in the kernel.

I guess that's bonus points for strprefix(). :)

Quoted 2 lines
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Signature · 1 line
<https://www.alejandro-colomar.es>
#9 lore raw

Re: [PATCH v5 2/7] string: Warn if the prefix is the empty string

On Fri, Nov 13, 2026 at 02:48:01PM +0100, Alejandro Colomar wrote:

...

Quoted 9 lines
Quoted 4 lines

[Severity: Low]
Does moving str_has_prefix() out-of-line into lib/string.c bypass CONFIG_FORTIFY_SOURCE and prevent compile-time constant folding of strlen(prefix)?

Preventing the compile-time constant folding of strlen(3) is indeed a performance degradation, but that's necessary to be able to WARN_ON_ONCE(), so there isn't anything we can do.

Quoted 8 lines
Quoted 4 lines

lib/string.c is built with __NO_FORTIFY and its top-of-file comment notes that it is reserved for routines that have architecture-specific alternatives in <asm/string.h> or get overloaded by FORTIFY_SOURCE, while other helpers belong in lib/string_helpers.c.

Do we want to move it to "lib/string_helpers.c"? I don't know enough about the organization in these files, so just let me know what I should do.

string library (generic) is for broad use, so it should really be clean from kernel things. string_helpers is exactly for some custom abstractions we use in kernel. Now you should answer these questions (or more than that):
- Are there or potentially be users that needs this API earlier at the boot?
- Is this API is detached from the rest of string library semantically? I.o.w. can we group semantically coupled APIs and consider them as a bundle?
- Due to the above, ask the first Q against semantically grouped APIs.

My personal feeling that what you are trying to do is no go for generic string as is. If you confirm that these APIs are not used / needed at early stages at boot, they can be moved completely to string_helpers.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw

Re: [PATCH v5 2/7] string: Warn if the prefix is the empty string

Hi Andy,

Quoted 33 lines

Date: 2026-11-13 16:25:35+0200
From: Andy Shevchenko <[email protected]>

On Fri, Nov 13, 2026 at 02:48:01PM +0100, Alejandro Colomar wrote:

...

Quoted 9 lines
Quoted 4 lines

[Severity: Low]
Does moving str_has_prefix() out-of-line into lib/string.c bypass CONFIG_FORTIFY_SOURCE and prevent compile-time constant folding of strlen(prefix)?

Preventing the compile-time constant folding of strlen(3) is indeed a performance degradation, but that's necessary to be able to WARN_ON_ONCE(), so there isn't anything we can do.

Quoted 8 lines
Quoted 4 lines

lib/string.c is built with __NO_FORTIFY and its top-of-file comment notes that it is reserved for routines that have architecture-specific alternatives in <asm/string.h> or get overloaded by FORTIFY_SOURCE, while other helpers belong in lib/string_helpers.c.

Do we want to move it to "lib/string_helpers.c"? I don't know enough about the organization in these files, so just let me know what I should do.

string library (generic) is for broad use, so it should really be clean from kernel things. string_helpers is exactly for some custom abstractions we use in kernel. Now you should answer these questions (or more than that):
- Are there or potentially be users that needs this API earlier at the boot?
- Is this API is detached from the rest of string library semantically? I.o.w. can we group semantically coupled APIs and consider them as a bundle?
- Due to the above, ask the first Q against semantically grouped APIs.

Quoted 3 lines

My personal feeling that what you are trying to do is no go for generic string as is. If you confirm that these APIs are not used / needed at early stages at boot, they can be moved completely to string_helpers.

Thanks! I guess I'll just drop the addition of WARN_ON_ONCE(). Let's leave the function as is, and document its problems. It's not worth the effort. After all, I think it's a bad function, and should not be used in new code.

Have a lovely day!
Alex

Quoted 6 lines
Signature · 3 lines
-- 
With Best Regards,
Andy Shevchenko
Signature · 1 line
<https://www.alejandro-colomar.es>
#11 lore raw

PATCH v5 3/7 device property: Fix fwnode_name_eq() for empty node names

In reply to Alejandro Colomar

str_has_prefix() returns the prefix length on match and 0 on mismatch, so the mistmatch case can't be told from "empty prefix matched an empty string".

For instance: the OF root node's name as returned by fwnode_get_name() is "" and will return 0 for any name matched against it and since len is 0 too, fwnode_name_eq() will return true.

Let's use strprefix() instead, which also makes this function simpler.

Fixes a9c8c738066b7 ("device property: Add fwnode_name_eq()")
Cc Andy Shevchenko <[email protected]>
---
 drivers/base/property.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/base/property.c +1 -4
index 51109794d727..7728116e2968 100644
@@ -692,15 +692,12 @@ const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
 bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name)
 {
 	const char *node_name;
-	ptrdiff_t len;
 
 	node_name = fwnode_get_name(fwnode);
 	if (!node_name)
 		return false;
 
-	len = strchrnul(node_name, '@') - node_name;
-
-	return str_has_prefix(node_name, name) == len;
+	return strprefix(node_name, name) == strchrnul(node_name, '@');
 }
 EXPORT_SYMBOL_GPL(fwnode_name_eq);
 
Signature · 1 line
2.55.0
#12 lore raw

PATCH v5 4/7 stddef: Add const_cast()

In reply to Alejandro Colomar

This macro serves to cast const away without risking unwanted conversions due to mistakes in the cast.

It's similar to C++'s const_cast(), but it is more restrictive: it can only cast away 'const', and not arbitrary qualifiers.

---
 include/linux/stddef.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
include/linux/stddef.h +11 -0
index e1851c50c89b..26c657465e60 100644
@@ -194,4 +194,15 @@ enum {
 #define TRAILING_OVERLAP(TYPE, NAME, FAM, MEMBERS)				\
 	__TRAILING_OVERLAP(TYPE, NAME, FAM, /* no attrs */, MEMBERS)
 
+/**
+ * const_cast() - At most, drop const in the left-most derivation of a pointer
+ *
+ * @T: Type for the cast.
+ * @p: Pointer to be casted.
+ *
+ * Cast @p to type @T.  The macro verifies that @p is of type const @T.
+ * This avoids mistakes that change anything other than 'const' in a cast.
+ */
+#define const_cast(T, p)  _Generic(p, const T: (T) (p))
+
 #endif
Signature · 1 line
2.55.0
#13 lore raw

PATCH v5 5/7 string: Add [C]QChar_of() and QVoid_of()

In reply to Alejandro Colomar

These macros serve to implement qualifier-generic macros as those specified in ISO C23.

---
 include/linux/string.h | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
include/linux/string.h +30 -0
index dcda1b7801bb..5f3ac760df2e 100644
@@ -14,6 +14,36 @@
 #include <linux/stdarg.h>
 #include <uapi/linux/string.h>
 
+/**
+ * QVoid_of - qualified void of
+ *
+ * @p: Pointer to test for qualifiers
+ *
+ * This macro produces the type name 'void',
+ * qualified with any qualifiers present in the element of @p.
+ */
+#define QVoid_of(p)       typeof(*(__QVoidptr_of(p)){0})
+#define __QVoidptr_of(p)  typeof(1?(p):(void*){0})
+
+/**
+ * QChar_of - qualified char of
+ *
+ * @Q: Qualifier to test.
+ * @p: Pointer to test for the @Q qualifier
+ *
+ * This macro produces the type name 'char',
+ * @Q-qualified iff the input pointer points to a @Q-qualified element.
+ */
+#define QChar_of(Q, p)  typeof                                        \
+(                                                                     \
+	_Generic((QVoid_of(p) *){0},                                  \
+		Q void *:  (Q char){0},                               \
+		void *:    (  char){0}                                \
+	)                                                             \
+)
+// CQChar_of - const qualified char of
+#define CQChar_of(p)  QChar_of(const, p)
+
 extern char *strndup_user(const char __user *, long);
 extern void *memdup_user(const void __user *, size_t) __realloc_size(2);
 extern void *vmemdup_user(const void __user *, size_t) __realloc_size(2);
Signature · 1 line
2.55.0
#14 lore raw

PATCH v5 6/7 string: Make strprefix() const-qualifier-generic

In reply to Alejandro Colomar

Add a macro of the same name, which returns a pointer that is const-qualified iff the input string is const-qualified. The return type is a possibly-const-qualified pointer to char, even if the input is a possibly-const-qualified pointer to void, which is why we require the CQChar_of() macro.

The function now needs to return 'const char *', because const_cast() can only drop const, but never add it.

---
 include/linux/string.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
include/linux/string.h +3 -2
index 5f3ac760df2e..b1468d3913f6 100644
@@ -591,11 +591,12 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
  * *  @s + strlen(@prefix) if @s starts with @prefix
  * *  NULL if @s does not start with @prefix
  */
-static inline char *strprefix(const char *s, const char *prefix)
+static inline const char *strprefix(const char *s, const char *prefix)
 {
 	size_t len = strlen(prefix);
-	return strncmp(s, prefix, len) == 0 ? (char *) s + len : NULL;
+	return strncmp(s, prefix, len) == 0 ? s + len : NULL;
 }
+#define strprefix(s, pfx)  const_cast(CQChar_of(s) *, strprefix(s, pfx))
 
 /**
  * strstarts - does @str start with @prefix?
Signature · 1 line
2.55.0
#15 lore raw

PATCH v5 7/7 string: Implement strstarts() with strprefix()

In reply to Alejandro Colomar

The compiler should be able to optimize out the unused pointer arithmetic done at the end, so performance-wise this shouldn't change anything.

This change de-duplicates the almost-identical implementations, and better shows the relationship between the two functions.

---
 include/linux/string.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/string.h +1 -1
index b1468d3913f6..015d2c7ee6c2 100644
@@ -608,7 +608,7 @@ static inline const char *strprefix(const char *s, const char *prefix)
  */
 static inline bool strstarts(const char *str, const char *prefix)
 {
-	return strncmp(str, prefix, strlen(prefix)) == 0;
+	return strprefix(str, prefix);
 }
 
 /**
Signature · 1 line
2.55.0
#16 lore raw

Re: [PATCH v5 7/7] string: Implement strstarts() with strprefix()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] string: strstarts() via strprefix() prevents optimizing out pointer offset --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] string: strstarts() via strprefix() prevents optimizing out pointer offset
--

commit 8cdc2e9c96584305ea94313f2618d17d8e28f303
Author: Alejandro Colomar <[email protected]>

string: Implement strstarts() with strprefix()

This commit changes strstarts() to call strprefix() instead of calling strncmp() and strlen() directly.

include/linux/string.h static inline const char *strprefix(const char *s, const char *prefix) · line 599

Quoted 11 lines
include/linux/string.h +1 -1
index 8c6a79d320474..8ca0deda61606 100644
@@ -595,7 +595,7 @@ static inline const char *strprefix(const char *s, const char *prefix)
  */
 static inline bool strstarts(const char *str, const char *prefix)
 {
-	return strncmp(str, prefix, strlen(prefix)) == 0;
+	return strprefix(str, prefix);
 }

[Severity: Low]
Can the compiler actually optimize away the s + len pointer arithmetic in strprefix() here?

strprefix() returns strncmp(s, prefix, len) == 0 ? s + len : NULL, and converting that result to bool requires proving that s + len is non-NULL whenever strncmp() returns 0. Because the kernel builds with -fno-delete-null-pointer-checks and -fno-strict-overflow, the compiler cannot assume that a runtime pointer is non-NULL or that s + len does not wrap to NULL, so callers of strstarts() end up keeping str and len live across strncmp() and emitting the extra addition and NULL check.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#17 lore raw

Re: [PATCH v5 7/7] string: Implement strstarts() with strprefix()

Quoted 33 lines

Date: 2026-11-13 13:20:16+0000
From: [email protected]

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] string: strstarts() via strprefix() prevents optimizing out pointer offset
--

commit 8cdc2e9c96584305ea94313f2618d17d8e28f303
Author: Alejandro Colomar <[email protected]>

string: Implement strstarts() with strprefix()

This commit changes strstarts() to call strprefix() instead of calling strncmp() and strlen() directly.

include/linux/string.h static inline const char *strprefix(const char *s, const char *prefix) · line 599

Quoted 11 lines
include/linux/string.h +1 -1
index 8c6a79d320474..8ca0deda61606 100644
@@ -595,7 +595,7 @@ static inline const char *strprefix(const char *s, const char *prefix)
  */
 static inline bool strstarts(const char *str, const char *prefix)
 {
-	return strncmp(str, prefix, strlen(prefix)) == 0;
+	return strprefix(str, prefix);
 }

[Severity: Low]
Can the compiler actually optimize away the s + len pointer arithmetic in strprefix() here?

Quoted 7 lines

strprefix() returns strncmp(s, prefix, len) == 0 ? s + len : NULL, and converting that result to bool requires proving that s + len is non-NULL whenever strncmp() returns 0. Because the kernel builds with -fno-delete-null-pointer-checks and -fno-strict-overflow, the compiler cannot assume that a runtime pointer is non-NULL or that s + len does not wrap to NULL, so callers of strstarts() end up keeping str and len live across strncmp() and emitting the extra addition and NULL check.

Hmmm, it seems it's right; the compiler can't optimize out. I hadn't taken those flags into account.

Here's a test which seems to corroborate:

	alx@devuan:~/tmp$ cat strstarts.c 
	#include <string.h>
	bool strstarts(const char *s, const char *prefix)
	{
		return strncmp(s, prefix, strlen(prefix)) == 0;
	}
	alx@devuan:~/tmp$ cat strprefix.c 
	#include <string.h>
	bool strstarts(const char *s, const char *prefix)
	{
		size_t len = strlen(prefix);
		return strncmp(s, prefix, len) == 0 ? s + len : NULL;
	}
	alx@devuan:~/tmp$ gcc -Wall -Wextra -O2 -fno-delete-null-pointer-checks -fno-strict-overflow -S strstarts.c 
	alx@devuan:~/tmp$ gcc -Wall -Wextra -O2 -fno-delete-null-pointer-checks -fno-strict-overflow -S strprefix.c 
	strprefix.c: In function ‘strstarts’:
	strprefix.c:5:9: warning: the comparison will always evaluate as ‘true’ for the pointer operand in ‘s + (sizetype)len’ must not be NULL [-Waddress]
	    5 |         return strncmp(s, prefix, len) == 0 ? s + len : NULL;
	      |         ^~~~~~
	alx@devuan:~/tmp$ diff -u strstarts.s strprefix.s
	--- strstarts.s	2026-11-13 14:33:48.478848453 +0100
	+++ strprefix.s	2026-11-13 14:33:51.802880696 +0100
	@@ -1,4 +1,4 @@
	-	.file	"strstarts.c"
	+	.file	"strprefix.c"
		.text
		.p2align 4
		.globl	strstarts
	@@ -6,24 +6,45 @@
	 strstarts:
	 .LFB0:
		.cfi_startproc
	-	pushq	%rbx
	+	pushq	%rbp
		.cfi_def_cfa_offset 16
	-	.cfi_offset 3, -16
	+	.cfi_offset 6, -16
	+	pushq	%rbx
	+	.cfi_def_cfa_offset 24
	+	.cfi_offset 3, -24
		movq	%rdi, %rbx
		movq	%rsi, %rdi
	-	subq	$16, %rsp
	-	.cfi_def_cfa_offset 32
	+	subq	$24, %rsp
	+	.cfi_def_cfa_offset 48
		movq	%rsi, 8(%rsp)
		call	strlen@PLT
		movq	8(%rsp), %rsi
		movq	%rbx, %rdi
		movq	%rax, %rdx
	+	movq	%rax, %rbp
		call	strncmp@PLT
		testl	%eax, %eax
	-	sete	%al
	-	addq	$16, %rsp
	+	jne	.L4
	+	addq	%rbp, %rbx
	+	setne	%al
	+	addq	$24, %rsp
	+	.cfi_remember_state
	+	.cfi_def_cfa_offset 24
	+	popq	%rbx
		.cfi_def_cfa_offset 16
	+	popq	%rbp
	+	.cfi_def_cfa_offset 8
	+	ret
	+	.p2align 4,,10
	+	.p2align 3
	+.L4:
	+	.cfi_restore_state
	+	addq	$24, %rsp
	+	.cfi_def_cfa_offset 24
	+	xorl	%eax, %eax
		popq	%rbx
	+	.cfi_def_cfa_offset 16
	+	popq	%rbp
		.cfi_def_cfa_offset 8
		ret
		.cfi_endproc

I'll drop patch 7/7 in v6.

Quoted 3 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Signature · 1 line
<https://www.alejandro-colomar.es>
#18 lore raw

Re: [PATCH v5 7/7] string: Implement strstarts() with strprefix()

In reply to Alejandro Colomar
Quoted 3 lines · Alejandro Colomar

The compiler should be able to optimize out the unused pointer arithmetic done at the end, so performance-wise this shouldn't change anything.

If strprefix() is not inline how would it be done? Or I missed the point?

Quoted 2 lines

This change de-duplicates the almost-identical implementations, and better shows the relationship between the two functions.

...

include/linux/string.h static inline const char *strprefix(const char *s, const char *prefix) · line 612

Quoted 5 lines

static inline bool strstarts(const char *str, const char *prefix) {

include/linux/string.h +1 -1
-	return strncmp(str, prefix, strlen(prefix)) == 0;
+	return strprefix(str, prefix);
 }

There is no sense to keep strstarts() at this point. Can it be simply redefinition? However, after looking at the uses in the kernel, I prefer to see the strprefix() be not implemented at all, just optimization against strstarts()/strends() if required can be performed.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#19 lore raw

Re: [PATCH v5 7/7] string: Implement strstarts() with strprefix()

Hi Andy,

Quoted 9 lines

Date: 2026-11-13 16:44:08+0200
From: Andy Shevchenko <[email protected]>

Quoted 3 lines · Alejandro Colomar

The compiler should be able to optimize out the unused pointer arithmetic done at the end, so performance-wise this shouldn't change anything.

If strprefix() is not inline how would it be done? Or I missed the point?

But strprefix() is inline.

Quoted 5 lines
Quoted 2 lines

This change de-duplicates the almost-identical implementations, and better shows the relationship between the two functions.

...

Quoted 7 lines

include/linux/string.h static inline const char *strprefix(const char *s, const char *prefix) · line 612

Quoted 5 lines

static inline bool strstarts(const char *str, const char *prefix) {

include/linux/string.h +1 -1
-	return strncmp(str, prefix, strlen(prefix)) == 0;
+	return strprefix(str, prefix);
 }

There is no sense to keep strstarts() at this point.

I agree. Actually, it seems my guess that strprefix() would be optimized out was wrong, because the kernel builds with -fno-delete-null-pointer-checks and other flags that prevent this optimization.

Quoted 1 line

Can it be simply redefinition?

I was thinking that we could just do a simple search and replace s/strstarts/strends/, prepending !! in some cases if needed.

By 'redefinition', do you mean making strstarts() return a pointer instead of a bool? A problem with strstarts() is again the function
name: it doesn't seem to naturally fit with a function that returns a pointer to the position. It also doesn't clarify whether it searches for a string, or for a list of characters (like for example strspn(3)).

I prefer replacing strstarts()/strends() with strprefix()/strsuffix().

But if you prefer changing strstarts() to return a pointer, I'm also okay-ish with it. It's not a perfect name, but I can live with it.

Quoted 3 lines

However, after looking at the uses in the kernel, I prefer
to see the strprefix() be not implemented at all, just optimization against strstarts()/strends() if required can be performed.

I don't understand this last sentence.

Cheers,
Alex

Quoted 6 lines
Signature · 3 lines
-- 
With Best Regards,
Andy Shevchenko
Signature · 1 line
<https://www.alejandro-colomar.es>
#20 lore raw

Re: [PATCH v5 7/7] string: Implement strstarts() with strprefix()

Quoted 1 line · Alejandro Colomar
Quoted 1 line · Alejandro Colomar

...

Quoted 15 lines
Quoted 7 lines

include/linux/string.h static inline const char *strprefix(const char *s, const char *prefix) · line 612

Quoted 5 lines

static inline bool strstarts(const char *str, const char *prefix) {

include/linux/string.h +1 -1
-	return strncmp(str, prefix, strlen(prefix)) == 0;
+	return strprefix(str, prefix);
 }

There is no sense to keep strstarts() at this point.

I agree. Actually, it seems my guess that strprefix() would be optimized out was wrong, because the kernel builds with -fno-delete-null-pointer-checks and other flags that prevent this optimization.

Quoted 1 line

Can it be simply redefinition?

Quoted 8 lines

I was thinking that we could just do a simple search and replace s/strstarts/strends/, prepending !! in some cases if needed.

By 'redefinition', do you mean making strstarts() return a pointer instead of a bool? A problem with strstarts() is again the function
name: it doesn't seem to naturally fit with a function that returns a pointer to the position. It also doesn't clarify whether it searches for a string, or for a list of characters (like for example strspn(3)).

We have str_has_*() for that?

Quoted 10 lines

I prefer replacing strstarts()/strends() with strprefix()/strsuffix().

But if you prefer changing strstarts() to return a pointer, I'm also okay-ish with it. It's not a perfect name, but I can live with it.

Quoted 3 lines

However, after looking at the uses in the kernel, I prefer
to see the strprefix() be not implemented at all, just optimization against strstarts()/strends() if required can be performed.

I don't understand this last sentence.

It's basically repeats the idea of having only one pair out of two.

With this series will be

str_has_previx()
strstarts()
strends()
strprefix()
// potentially strsuffix()

To me seems too many for the similar task. I understand that trace user is special one, but does it use strstarts()/strends()?

Despite on naming I think we just good to have only str_has*() and one of strprefix()/strsuffix(), strstarts()/strends().

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#21 lore raw

Re: [PATCH v5 7/7] string: Implement strstarts() with strprefix()

Hi Andy,

Quoted 26 lines

Date: 2026-11-13 17:07:35+0200
From: Andy Shevchenko <[email protected]>

Quoted 1 line · Alejandro Colomar
Quoted 1 line · Alejandro Colomar

...

Quoted 18 lines
Quoted 7 lines

include/linux/string.h static inline const char *strprefix(const char *s, const char *prefix) · line 612

Quoted 5 lines

static inline bool strstarts(const char *str, const char *prefix) {

include/linux/string.h +1 -1
-	return strncmp(str, prefix, strlen(prefix)) == 0;
+	return strprefix(str, prefix);
 }

There is no sense to keep strstarts() at this point.

I agree. Actually, it seems my guess that strprefix() would be optimized out was wrong, because the kernel builds with -fno-delete-null-pointer-checks and other flags that prevent this optimization.

Quoted 1 line

Can it be simply redefinition?

I was thinking that we could just do a simple search and replace s/strstarts/strends/, prepending !! in some cases if needed.

Quoted 7 lines
Quoted 5 lines

By 'redefinition', do you mean making strstarts() return a pointer instead of a bool? A problem with strstarts() is again the function
name: it doesn't seem to naturally fit with a function that returns a pointer to the position. It also doesn't clarify whether it searches for a string, or for a list of characters (like for example strspn(3)).

We have str_has_*() for that?

I don't fully understand. str_has_prefix() --which is the only str_has_*() function, BTW-- is meant to be the same as strprefix(), except for using an offset instead of a pointer.

- strstarts():
It could be improved by returning a boolean-like pointer, instead of a bool, but:

- We may need to fix some callers, since some of them might need an explicit conversion with !!. It'd be risky to change the implementation without first verifying that.

To avoid that risk, I prefer adding a new API, and deprecating strstarts() for new code. Over time, I could slowly migrate users from strstarts() to strprefix().

- The name isn't ideal, since "starts" doesn't specify whether it looks for a string that starts with a given prefix, or if it looks for a string that starts with any of the given characters.

That is, by the name, I wouldn't be able to know whether strstarts implements

bool strstarts(const char *s, const char *xxx) {
return strspn(s, xxx);
}
or
bool strstarts(const char *s, const char *xxx) {
return strprefix(s, xxx);
}

- strends():
Same as with strstarts() but from the end.

- str_has_prefix():
It was an attempt at strprefix(), but with a problematic approach. It's frozen because of trace code.

- strprefix()/strsuffix():
I'd add them to slowly replace strstarts()/strends().

Quoted 6 lines
Quoted 6 lines

I prefer replacing strstarts()/strends() with strprefix()/strsuffix().

But if you prefer changing strstarts() to return a pointer, I'm also okay-ish with it. It's not a perfect name, but I can live with it.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

However, after looking at the uses in the kernel, I prefer
to see the strprefix() be not implemented at all, just optimization against strstarts()/strends() if required can be performed.

I don't understand this last sentence.

It's basically repeats the idea of having only one pair out of two.

Ok.

Quoted 7 lines

With this series will be

str_has_previx()
strstarts()
strends()
strprefix()
// potentially strsuffix()

Yes. I'd like to replace strstarts()/strends() with strprefix()/strsuffix(), but prefer to do that slowly, to avoid introducing bugs due to missing explicit conversions to bool.

Should I add notes that the strstarts()/strends() APIs are deprecated?

Quoted 2 lines

To me seems too many for the similar task. I understand that trace user is special one, but does it use strstarts()/strends()?

I didn't find any uses:

	$ git ls-files | grep kernel/trace/ | xargs grep strstarts
	$ git ls-files | grep kernel/trace/ | xargs grep strends
Quoted 2 lines

Despite on naming I think we just good to have only str_has*() and one of strprefix()/strsuffix(), strstarts()/strends().

In the long term, I agree. But I think the transition should be careful. What do you think?

Cheers,
Alex

Quoted 6 lines
Signature · 3 lines
-- 
With Best Regards,
Andy Shevchenko
Signature · 1 line
<https://www.alejandro-colomar.es>
#22 lore raw

Re: [PATCH v5 7/7] string: Implement strstarts() with strprefix()

Quoted 2 lines · Alejandro Colomar
Quoted 2 lines · Alejandro Colomar
Quoted 1 line
Quoted 1 line · Alejandro Colomar

...

Quoted 24 lines
Quoted 24 lines
Quoted 23 lines
Quoted 7 lines

include/linux/string.h static inline const char *strprefix(const char *s, const char *prefix) · line 612

Quoted 5 lines

static inline bool strstarts(const char *str, const char *prefix) {

include/linux/string.h +1 -1
-	return strncmp(str, prefix, strlen(prefix)) == 0;
+	return strprefix(str, prefix);
 }

There is no sense to keep strstarts() at this point.

I agree. Actually, it seems my guess that strprefix() would be optimized out was wrong, because the kernel builds with -fno-delete-null-pointer-checks and other flags that prevent this optimization.

Quoted 1 line

Can it be simply redefinition?

I was thinking that we could just do a simple search and replace s/strstarts/strends/, prepending !! in some cases if needed.

By 'redefinition', do you mean making strstarts() return a pointer instead of a bool? A problem with strstarts() is again the function
name: it doesn't seem to naturally fit with a function that returns a pointer to the position. It also doesn't clarify whether it searches for a string, or for a list of characters (like for example strspn(3)).

Quoted 5 lines
Quoted 1 line

We have str_has_*() for that?

I don't fully understand. str_has_prefix() --which is the only str_has_*() function, BTW-- is meant to be the same as strprefix(), except for using an offset instead of a pointer.

You argued that strstarts() versus strprefix() is a naming problem, I answered that str_has_*() is for that kind of ops to solve naming issue. So, semantically str_has_*() should be like a boolean checker (or behave close to it, which it does).

Quoted 7 lines

- strstarts():
It could be improved by returning a boolean-like pointer, instead of a bool, but:

- We may need to fix some callers, since some of them might need an explicit conversion with !!. It'd be risky to change the implementation without first verifying that.

How many cases do we have that needs special attention? Can you run spatch to grep those?

Quoted 3 lines

To avoid that risk, I prefer adding a new API, and deprecating strstarts() for new code. Over time, I could slowly migrate users from strstarts() to strprefix().

I believe this is unneeded step which may last for a few releases without even guarantee that it will be finished. In such a case I would like to see a branch with full conversion done before you start sending per-subsystem pieces out.

Quoted 9 lines
	-  The name isn't ideal, since "starts" doesn't specify whether
	   it looks for a string that starts with a given prefix, or if
	   it looks for a string that starts with any of the given
	   characters.

	   That is, by the name, I wouldn't be able to know whether
	   strstarts implements

		bool strstarts(const char *s, const char *xxx)
Quoted 8 lines
		{
			return strspn(s, xxx);
		}
	   or
		bool strstarts(const char *s, const char *xxx)
		{
			return strprefix(s, xxx);
		}

I didn't get this. We should not really need an implementation details of the API beneath. Documentation what should be read before use. Do we have documentation for strstarts()? Is it clear? Maybe that what should be addressed first?

Quoted 9 lines

- strends():
Same as with strstarts() but from the end.

- str_has_prefix():
It was an attempt at strprefix(), but with a problematic approach. It's frozen because of trace code.

- strprefix()/strsuffix():
I'd add them to slowly replace strstarts()/strends().

See above about "slowly"...

Quoted 23 lines
Quoted 5 lines
Quoted 5 lines

I prefer replacing strstarts()/strends() with strprefix()/strsuffix().

But if you prefer changing strstarts() to return a pointer, I'm also okay-ish with it. It's not a perfect name, but I can live with it.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

However, after looking at the uses in the kernel, I prefer
to see the strprefix() be not implemented at all, just optimization against strstarts()/strends() if required can be performed.

I don't understand this last sentence.

It's basically repeats the idea of having only one pair out of two.

Ok.

Quoted 7 lines

With this series will be

str_has_previx()
strstarts()
strends()
strprefix()
// potentially strsuffix()

Quoted 5 lines

Yes. I'd like to replace strstarts()/strends() with strprefix()/strsuffix(), but prefer to do that slowly, to avoid introducing bugs due to missing explicit conversions to bool.

Should I add notes that the strstarts()/strends() APIs are deprecated?

Isn't Steven against that? I already forgot his points. Keeping them just as aliases to strprefix()/strsuffix() perhaps the good start (see below).

Quoted 5 lines
Quoted 2 lines

To me seems too many for the similar task. I understand that trace user is special one, but does it use strstarts()/strends()?

I didn't find any uses:

Quoted 8 lines
	$ git ls-files | grep kernel/trace/ | xargs grep strstarts
	$ git ls-files | grep kernel/trace/ | xargs grep strends
Quoted 2 lines

Despite on naming I think we just good to have only str_has*() and one of strprefix()/strsuffix(), strstarts()/strends().

In the long term, I agree. But I think the transition should be careful. What do you think?

Yes, it should take a few phases, first one perhaps to align strstarts() to not rely on the returned value to be boolean.

In parallel with that, your current series can also be considered. Then similar, if even required, to be done against strends(). And one more phase is to replace strstarts()/strends() with strprefix()/strsuffix() all over the kernel.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#23 lore raw

Re: [PATCH v5 7/7] string: Implement strstarts() with strprefix()

Hi Andy,

Quoted 48 lines

Date: 2026-11-13 18:05:00+0200
From: Andy Shevchenko <[email protected]>

Quoted 2 lines · Alejandro Colomar
Quoted 2 lines · Alejandro Colomar
Quoted 1 line
Quoted 1 line · Alejandro Colomar

...

Quoted 24 lines
Quoted 24 lines
Quoted 23 lines
Quoted 7 lines

include/linux/string.h static inline const char *strprefix(const char *s, const char *prefix) · line 612

Quoted 5 lines

static inline bool strstarts(const char *str, const char *prefix) {

include/linux/string.h +1 -1
-	return strncmp(str, prefix, strlen(prefix)) == 0;
+	return strprefix(str, prefix);
 }

There is no sense to keep strstarts() at this point.

I agree. Actually, it seems my guess that strprefix() would be optimized out was wrong, because the kernel builds with -fno-delete-null-pointer-checks and other flags that prevent this optimization.

Quoted 1 line

Can it be simply redefinition?

I was thinking that we could just do a simple search and replace s/strstarts/strends/, prepending !! in some cases if needed.

By 'redefinition', do you mean making strstarts() return a pointer instead of a bool? A problem with strstarts() is again the function
name: it doesn't seem to naturally fit with a function that returns a pointer to the position. It also doesn't clarify whether it searches for a string, or for a list of characters (like for example strspn(3)).

Quoted 5 lines
Quoted 1 line

We have str_has_*() for that?

I don't fully understand. str_has_prefix() --which is the only str_has_*() function, BTW-- is meant to be the same as strprefix(), except for using an offset instead of a pointer.

You argued that strstarts() versus strprefix() is a naming problem, I answered that str_has_*() is for that kind of ops to solve naming issue. So, semantically str_has_*() should be like a boolean checker (or behave close to it, which it does).

Quoted 4 lines

- strstarts():
It could be improved by returning a boolean-like pointer, instead of a bool, but:

Quoted 6 lines
Quoted 3 lines

- We may need to fix some callers, since some of them might need an explicit conversion with !!. It'd be risky to change the implementation without first verifying that.

How many cases do we have that needs special attention? Can you run spatch to grep those?

I *think* the only ones I expect this would be necessary are functions that return a boolean-like int.

	// returns 1:true 0:false -1:error=
	int
	foo(...)
	{
		...
		return !!strprefix(...);
	}
Quoted 8 lines
Quoted 4 lines

To avoid that risk, I prefer adding a new API, and deprecating strstarts() for new code. Over time, I could slowly migrate users from strstarts() to strprefix().

I believe this is unneeded step which may last for a few releases without even guarantee that it will be finished. In such a case I would like to see a branch with full conversion done before you start sending per-subsystem pieces out.

Okay; I can do that.

Quoted 58 lines
Quoted 9 lines
	-  The name isn't ideal, since "starts" doesn't specify whether
	   it looks for a string that starts with a given prefix, or if
	   it looks for a string that starts with any of the given
	   characters.

	   That is, by the name, I wouldn't be able to know whether
	   strstarts implements

		bool strstarts(const char *s, const char *xxx)
Quoted 8 lines
		{
			return strspn(s, xxx);
		}
	   or
		bool strstarts(const char *s, const char *xxx)
		{
			return strprefix(s, xxx);
		}

I didn't get this. We should not really need an implementation details of the API beneath. Documentation what should be read before use. Do we have documentation for strstarts()? Is it clear? Maybe that what should be addressed first?

Quoted 9 lines

- strends():
Same as with strstarts() but from the end.

- str_has_prefix():
It was an attempt at strprefix(), but with a problematic approach. It's frozen because of trace code.

- strprefix()/strsuffix():
I'd add them to slowly replace strstarts()/strends().

See above about "slowly"...

Quoted 23 lines
Quoted 5 lines
Quoted 5 lines

I prefer replacing strstarts()/strends() with strprefix()/strsuffix().

But if you prefer changing strstarts() to return a pointer, I'm also okay-ish with it. It's not a perfect name, but I can live with it.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

However, after looking at the uses in the kernel, I prefer
to see the strprefix() be not implemented at all, just optimization against strstarts()/strends() if required can be performed.

I don't understand this last sentence.

It's basically repeats the idea of having only one pair out of two.

Ok.

Quoted 7 lines

With this series will be

str_has_previx()
strstarts()
strends()
strprefix()
// potentially strsuffix()

Quoted 7 lines
Quoted 5 lines

Yes. I'd like to replace strstarts()/strends() with strprefix()/strsuffix(), but prefer to do that slowly, to avoid introducing bugs due to missing explicit conversions to bool.

Should I add notes that the strstarts()/strends() APIs are deprecated?

Isn't Steven against that? I already forgot his points.

As far as I understood, he was against deprecating str_has_prefix(), but we didn't yet talk about strstarts().

Quoted 3 lines

Keeping them
just as aliases to strprefix()/strsuffix() perhaps the good start (see below).

Ok.

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

To me seems too many for the similar task. I understand that trace user is special one, but does it use strstarts()/strends()?

I didn't find any uses:

	$ git ls-files | grep kernel/trace/ | xargs grep strstarts
	$ git ls-files | grep kernel/trace/ | xargs grep strends
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Despite on naming I think we just good to have only str_has*() and one of strprefix()/strsuffix(), strstarts()/strends().

In the long term, I agree. But I think the transition should be careful. What do you think?

Yes, it should take a few phases, first one perhaps to align strstarts() to not rely on the returned value to be boolean.

Ok.

Quoted 2 lines

In parallel with that, your current series can also be considered. Then similar, if even required, to be done against strends().

Agree.

Quoted 3 lines

And one more phase
is to replace strstarts()/strends() with strprefix()/strsuffix() all over the kernel.

Ok.

Thanks!

Have a lovely night!
Alex

Quoted 7 lines
Signature · 3 lines
-- 
With Best Regards,
Andy Shevchenko
Signature · 1 line
<https://www.alejandro-colomar.es>
#24 lore raw

Re: [PATCH v5 7/7] string: Implement strstarts() with strprefix()

Quoted 3 lines

Date: 2026-11-13 21:57:24+0100
From: Alejandro Colomar <[email protected]>

[...]

Quoted 14 lines
Quoted 10 lines
Quoted 7 lines

- strstarts():
It could be improved by returning a boolean-like pointer, instead of a bool, but:

- We may need to fix some callers, since some of them might need an explicit conversion with !!. It'd be risky to change the implementation without first verifying that.

How many cases do we have that needs special attention? Can you run spatch to grep those?

I *think* the only ones I expect this would be necessary are functions that return a boolean-like int.

Quoted 7 lines
	// returns 1:true 0:false -1:error=
	int
	foo(...)
	{
		...
		return !!strprefix(...);
	}

I've searched for cases of this, and also for cases of assignment to a variable that might be of type int (I expect this to be less frequent). I haven't used spatch(1) so far, but just grep(1):

	$ grep -rn -e '= .*strstarts(' -e 'return .*strstarts('
	drivers/mtd/spi-nor/issi.c:141:	return name && strstarts(name, "pm25lv");
	drivers/platform/x86/dell/dell-lis3lv02d.c:109:	return strstarts(adap->name, "SMBus I801 adapter");
	arch/arm/kernel/module.c:25:	return strstarts(name, ".init") ||
	arch/arm/kernel/module.c:32:	return strstarts(name, ".exit") ||
	arch/powerpc/perf/imc-pmu.c:1026:	return strstarts(event->pmu->name, "thread_imc");
	tools/perf/util/map.c:21:	return strstarts(filename, "/data/app-lib/") ||
	tools/perf/util/annotate-arch/annotate-x86.c:144:	if (arch->family >= 0x15 && (strstarts(ins1, "test") ||
	tools/perf/util/annotate-arch/annotate-x86.c:150:	if (arch->family >= 0x19 && (strstarts(ins1, "add") ||
	tools/perf/util/annotate-arch/annotate-x86.c:200:		arch->ins_is_fused = strstarts(cpuid, "AuthenticAMD") ?
	tools/perf/util/symbol.c:854:	if (e_machine == EM_S390 && strstarts(name, "psw_idle")) {
	tools/perf/util/env.c:733:		return strstarts(perf_arch, "ppc64") ? EM_PPC64 : EM_PPC;
	tools/perf/util/env.c:1049:		is_amd = env->cpuid && strstarts(env->cpuid, "AuthenticAMD") ? 1 : -1;
	tools/perf/util/env.c:1072:		is_intel = env->cpuid && strstarts(env->cpuid, "GenuineIntel") ? 1 : -1;
	tools/perf/util/symbol.h:55:	return strstarts(str, KLP_SYM_PREFIX);
	tools/perf/builtin-trace.c:2272:			if (*after == '=' && (strstarts(after + 1, "0x") ||
	tools/perf/perf.c:143:	return (strstarts(var, header) && !strcmp(var + strlen(header), c->cmd));
	tools/perf/tests/symbols.c:155:	if (dso != text_dso && strstarts(dso__short_name(dso), dso__short_name(text_dso)))
	tools/testing/selftests/bpf/prog_tests/sockmap_redir.c:321:	if (type == SK_SKB_INGRESS && strstarts(out, "v_"))
	tools/objtool/klp-sympos.c:372:	return strstarts(sym->sec->name, ".init");
	tools/objtool/klp-checksum.c:66:	return strstarts(sym->sec->name, ".rodata.cst") &&
	tools/objtool/klp-diff.c:204:		export->mod_ns = strstarts(namespace, "module:");
	tools/objtool/klp-diff.c:339:	return strstarts(sym->name, ".L");
	tools/objtool/klp-diff.c:380:	return strstarts(sec->name, ".discard.");
	tools/objtool/klp-diff.c:422:	return strstarts(sym->name, "__initcall__") ||
	kernel/module/main.c:1866:	return strstarts(name, ".init");
	kernel/module/main.c:1871:	return strstarts(name, ".exit");

From all these, only one case was using int. Here's a patch for making it more robust:

	diff --git i/tools/perf/util/map.c w/tools/perf/util/map.c
	index 41cdddc987ee..944f10d32796 100644
	--- i/tools/perf/util/map.c
	+++ w/tools/perf/util/map.c
	@@ -16,7 +16,7 @@
	 #include "thread.h"
	 #include "vdso.h"
	 
	-static inline int is_android_lib(const char *filename)
	+static inline bool is_android_lib(const char *filename)
	 {
		return strstarts(filename, "/data/app-lib/") ||
		       strstarts(filename, "/system/lib/");

It's technically not necessary, since the '||' operator converts to bool, but just in case someone removes one of those calls, using bool will prevent a bug.

All the other cases were either using 'bool', or didn't need a change for other reasons.

So, I can include this diff as a preparation patch in this set, and then make strstarts() be

	#define strstarts(s, prefix)  strprefix(s, prefix)

And then have a global search-and-replace for it.

Cheers,
Alex

Quoted 133 lines
Quoted 9 lines
Quoted 5 lines

To avoid that risk, I prefer adding a new API, and deprecating strstarts() for new code. Over time, I could slowly migrate users from strstarts() to strprefix().

I believe this is unneeded step which may last for a few releases without even guarantee that it will be finished. In such a case I would like to see a branch with full conversion done before you start sending per-subsystem pieces out.

Okay; I can do that.

Quoted 58 lines
Quoted 9 lines
	-  The name isn't ideal, since "starts" doesn't specify whether
	   it looks for a string that starts with a given prefix, or if
	   it looks for a string that starts with any of the given
	   characters.

	   That is, by the name, I wouldn't be able to know whether
	   strstarts implements

		bool strstarts(const char *s, const char *xxx)
Quoted 8 lines
		{
			return strspn(s, xxx);
		}
	   or
		bool strstarts(const char *s, const char *xxx)
		{
			return strprefix(s, xxx);
		}

I didn't get this. We should not really need an implementation details of the API beneath. Documentation what should be read before use. Do we have documentation for strstarts()? Is it clear? Maybe that what should be addressed first?

Quoted 9 lines

- strends():
Same as with strstarts() but from the end.

- str_has_prefix():
It was an attempt at strprefix(), but with a problematic approach. It's frozen because of trace code.

- strprefix()/strsuffix():
I'd add them to slowly replace strstarts()/strends().

See above about "slowly"...

Quoted 23 lines
Quoted 5 lines
Quoted 5 lines

I prefer replacing strstarts()/strends() with strprefix()/strsuffix().

But if you prefer changing strstarts() to return a pointer, I'm also okay-ish with it. It's not a perfect name, but I can live with it.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

However, after looking at the uses in the kernel, I prefer
to see the strprefix() be not implemented at all, just optimization against strstarts()/strends() if required can be performed.

I don't understand this last sentence.

It's basically repeats the idea of having only one pair out of two.

Ok.

Quoted 7 lines

With this series will be

str_has_previx()
strstarts()
strends()
strprefix()
// potentially strsuffix()

Quoted 7 lines
Quoted 5 lines

Yes. I'd like to replace strstarts()/strends() with strprefix()/strsuffix(), but prefer to do that slowly, to avoid introducing bugs due to missing explicit conversions to bool.

Should I add notes that the strstarts()/strends() APIs are deprecated?

Isn't Steven against that? I already forgot his points.

As far as I understood, he was against deprecating str_has_prefix(), but we didn't yet talk about strstarts().

Quoted 3 lines

Keeping them
just as aliases to strprefix()/strsuffix() perhaps the good start (see below).

Ok.

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

To me seems too many for the similar task. I understand that trace user is special one, but does it use strstarts()/strends()?

I didn't find any uses:

	$ git ls-files | grep kernel/trace/ | xargs grep strstarts
	$ git ls-files | grep kernel/trace/ | xargs grep strends
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Despite on naming I think we just good to have only str_has*() and one of strprefix()/strsuffix(), strstarts()/strends().

In the long term, I agree. But I think the transition should be careful. What do you think?

Yes, it should take a few phases, first one perhaps to align strstarts() to not rely on the returned value to be boolean.

Ok.

Quoted 2 lines

In parallel with that, your current series can also be considered. Then similar, if even required, to be done against strends().

Agree.

Quoted 3 lines

And one more phase
is to replace strstarts()/strends() with strprefix()/strsuffix() all over the kernel.

Ok.

Thanks!

Have a lovely night!
Alex

Quoted 7 lines
Signature · 3 lines
-- 
With Best Regards,
Andy Shevchenko
Signature · 1 line
<https://www.alejandro-colomar.es>
Signature · 1 line
<https://www.alejandro-colomar.es>
#25 lore raw

Re: [PATCH v5 7/7] string: Implement strstarts() with strprefix()

Quoted 52 lines

Date: 2026-11-13 22:13:10+0100
From: Alejandro Colomar <[email protected]>

Quoted 3 lines

Date: 2026-11-13 21:57:24+0100
From: Alejandro Colomar <[email protected]>

[...]

Quoted 14 lines
Quoted 10 lines
Quoted 7 lines

- strstarts():
It could be improved by returning a boolean-like pointer, instead of a bool, but:

- We may need to fix some callers, since some of them might need an explicit conversion with !!. It'd be risky to change the implementation without first verifying that.

How many cases do we have that needs special attention? Can you run spatch to grep those?

I *think* the only ones I expect this would be necessary are functions that return a boolean-like int.

Quoted 7 lines
	// returns 1:true 0:false -1:error=
	int
	foo(...)
	{
		...
		return !!strprefix(...);
	}

I've searched for cases of this, and also for cases of assignment to a variable that might be of type int (I expect this to be less frequent). I haven't used spatch(1) so far, but just grep(1):

	$ grep -rn -e '= .*strstarts(' -e 'return .*strstarts('
	drivers/mtd/spi-nor/issi.c:141:	return name && strstarts(name, "pm25lv");
	drivers/platform/x86/dell/dell-lis3lv02d.c:109:	return strstarts(adap->name, "SMBus I801 adapter");
	arch/arm/kernel/module.c:25:	return strstarts(name, ".init") ||
	arch/arm/kernel/module.c:32:	return strstarts(name, ".exit") ||
	arch/powerpc/perf/imc-pmu.c:1026:	return strstarts(event->pmu->name, "thread_imc");
	tools/perf/util/map.c:21:	return strstarts(filename, "/data/app-lib/") ||
	tools/perf/util/annotate-arch/annotate-x86.c:144:	if (arch->family >= 0x15 && (strstarts(ins1, "test") ||
	tools/perf/util/annotate-arch/annotate-x86.c:150:	if (arch->family >= 0x19 && (strstarts(ins1, "add") ||
	tools/perf/util/annotate-arch/annotate-x86.c:200:		arch->ins_is_fused = strstarts(cpuid, "AuthenticAMD") ?
	tools/perf/util/symbol.c:854:	if (e_machine == EM_S390 && strstarts(name, "psw_idle")) {
	tools/perf/util/env.c:733:		return strstarts(perf_arch, "ppc64") ? EM_PPC64 : EM_PPC;
	tools/perf/util/env.c:1049:		is_amd = env->cpuid && strstarts(env->cpuid, "AuthenticAMD") ? 1 : -1;
	tools/perf/util/env.c:1072:		is_intel = env->cpuid && strstarts(env->cpuid, "GenuineIntel") ? 1 : -1;
	tools/perf/util/symbol.h:55:	return strstarts(str, KLP_SYM_PREFIX);
	tools/perf/builtin-trace.c:2272:			if (*after == '=' && (strstarts(after + 1, "0x") ||
	tools/perf/perf.c:143:	return (strstarts(var, header) && !strcmp(var + strlen(header), c->cmd));
	tools/perf/tests/symbols.c:155:	if (dso != text_dso && strstarts(dso__short_name(dso), dso__short_name(text_dso)))
	tools/testing/selftests/bpf/prog_tests/sockmap_redir.c:321:	if (type == SK_SKB_INGRESS && strstarts(out, "v_"))
Quoted 8 lines
	tools/objtool/klp-sympos.c:372:	return strstarts(sym->sec->name, ".init");
	tools/objtool/klp-checksum.c:66:	return strstarts(sym->sec->name, ".rodata.cst") &&
	tools/objtool/klp-diff.c:204:		export->mod_ns = strstarts(namespace, "module:");
	tools/objtool/klp-diff.c:339:	return strstarts(sym->name, ".L");
	tools/objtool/klp-diff.c:380:	return strstarts(sec->name, ".discard.");
	tools/objtool/klp-diff.c:422:	return strstarts(sym->name, "__initcall__") ||
	kernel/module/main.c:1866:	return strstarts(name, ".init");
	kernel/module/main.c:1871:	return strstarts(name, ".exit");

With a multi-line search with pcre2grep(1) I've checked that I didn't miss any more complex cases. I'm quite confident that there's only one case that needs to be improved (the one below).

Cheers,
Alex

Quoted 173 lines

From all these, only one case was using int. Here's a patch for making it more robust:

	diff --git i/tools/perf/util/map.c w/tools/perf/util/map.c
	index 41cdddc987ee..944f10d32796 100644
	--- i/tools/perf/util/map.c
	+++ w/tools/perf/util/map.c
	@@ -16,7 +16,7 @@
	 #include "thread.h"
	 #include "vdso.h"
	 
	-static inline int is_android_lib(const char *filename)
	+static inline bool is_android_lib(const char *filename)
	 {
		return strstarts(filename, "/data/app-lib/") ||
		       strstarts(filename, "/system/lib/");

It's technically not necessary, since the '||' operator converts to bool, but just in case someone removes one of those calls, using bool will prevent a bug.

All the other cases were either using 'bool', or didn't need a change for other reasons.

So, I can include this diff as a preparation patch in this set, and then make strstarts() be

	#define strstarts(s, prefix)  strprefix(s, prefix)

And then have a global search-and-replace for it.

Cheers,
Alex

Quoted 133 lines
Quoted 9 lines
Quoted 5 lines

To avoid that risk, I prefer adding a new API, and deprecating strstarts() for new code. Over time, I could slowly migrate users from strstarts() to strprefix().

I believe this is unneeded step which may last for a few releases without even guarantee that it will be finished. In such a case I would like to see a branch with full conversion done before you start sending per-subsystem pieces out.

Okay; I can do that.

Quoted 58 lines
Quoted 9 lines
	-  The name isn't ideal, since "starts" doesn't specify whether
	   it looks for a string that starts with a given prefix, or if
	   it looks for a string that starts with any of the given
	   characters.

	   That is, by the name, I wouldn't be able to know whether
	   strstarts implements

		bool strstarts(const char *s, const char *xxx)
Quoted 8 lines
		{
			return strspn(s, xxx);
		}
	   or
		bool strstarts(const char *s, const char *xxx)
		{
			return strprefix(s, xxx);
		}

I didn't get this. We should not really need an implementation details of the API beneath. Documentation what should be read before use. Do we have documentation for strstarts()? Is it clear? Maybe that what should be addressed first?

Quoted 9 lines

- strends():
Same as with strstarts() but from the end.

- str_has_prefix():
It was an attempt at strprefix(), but with a problematic approach. It's frozen because of trace code.

- strprefix()/strsuffix():
I'd add them to slowly replace strstarts()/strends().

See above about "slowly"...

Quoted 23 lines
Quoted 5 lines
Quoted 5 lines

I prefer replacing strstarts()/strends() with strprefix()/strsuffix().

But if you prefer changing strstarts() to return a pointer, I'm also okay-ish with it. It's not a perfect name, but I can live with it.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

However, after looking at the uses in the kernel, I prefer
to see the strprefix() be not implemented at all, just optimization against strstarts()/strends() if required can be performed.

I don't understand this last sentence.

It's basically repeats the idea of having only one pair out of two.

Ok.

Quoted 7 lines

With this series will be

str_has_previx()
strstarts()
strends()
strprefix()
// potentially strsuffix()

Quoted 7 lines
Quoted 5 lines

Yes. I'd like to replace strstarts()/strends() with strprefix()/strsuffix(), but prefer to do that slowly, to avoid introducing bugs due to missing explicit conversions to bool.

Should I add notes that the strstarts()/strends() APIs are deprecated?

Isn't Steven against that? I already forgot his points.

As far as I understood, he was against deprecating str_has_prefix(), but we didn't yet talk about strstarts().

Quoted 3 lines

Keeping them
just as aliases to strprefix()/strsuffix() perhaps the good start (see below).

Ok.

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

To me seems too many for the similar task. I understand that trace user is special one, but does it use strstarts()/strends()?

I didn't find any uses:

	$ git ls-files | grep kernel/trace/ | xargs grep strstarts
	$ git ls-files | grep kernel/trace/ | xargs grep strends
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Despite on naming I think we just good to have only str_has*() and one of strprefix()/strsuffix(), strstarts()/strends().

In the long term, I agree. But I think the transition should be careful. What do you think?

Yes, it should take a few phases, first one perhaps to align strstarts() to not rely on the returned value to be boolean.

Ok.

Quoted 2 lines

In parallel with that, your current series can also be considered. Then similar, if even required, to be done against strends().

Agree.

Quoted 3 lines

And one more phase
is to replace strstarts()/strends() with strprefix()/strsuffix() all over the kernel.

Ok.

Thanks!

Have a lovely night!
Alex

Quoted 7 lines
Signature · 3 lines
-- 
With Best Regards,
Andy Shevchenko
Signature · 1 line
<https://www.alejandro-colomar.es>
Signature · 1 line
<https://www.alejandro-colomar.es>
Signature · 1 line
<https://www.alejandro-colomar.es>