[PATCH v6 03/19] string: Document corner-case misbehavior in str_has_prefix() for empty prefix

From: Alejandro Colomar

Date: Sat Nov 14 2026 - 10:22:59 EST


For an empty prefix, this function returns a value that can't be
distinguished from a non-matching prefix. This is wrong, since an empty
prefix is present in every string, and has resulted in bugs in code that
called this function without knowing this issue. Let's document it.

Closes: https://lore.kernel.org/linux-hardening/20261104-fwnode-for-each-prop-v1-1-6e28fe1c72c4@xxxxxxxxxxxxxxxx/
Reported-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxxxxxxxx>
Suggested-by: Steven Rostedt <rostedt@xxxxxxxxxxx>
Signed-off-by: Alejandro Colomar <alx@xxxxxxxxxx>
---
include/linux/string.h | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 14e825e902eb..58b8b7c02b8d 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -587,6 +587,10 @@ static inline char *strprefix(const char *s, const char *prefix)
* 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
--
2.55.0

Attachment: signature.asc
Description: PGP signature