[PATCH v7 2/4] string: Add strprefix()
From: Alejandro Colomar
Date: Wed Nov 25 2026 - 16:03:56 EST
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().
Closes: https://lore.kernel.org/linux-hardening/20261104-fwnode-for-each-prop-v1-1-6e28fe1c72c4@xxxxxxxxxxxxxxxx/
Reported-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxxxxxxxx>
Signed-off-by: Alejandro Colomar <alx@xxxxxxxxxx>
---
include/linux/string.h | 28 +++++++++++++++++++++++-----
lib/tests/string_kunit.c | 9 +++++++++
2 files changed, 32 insertions(+), 5 deletions(-)
diff --git a/include/linux/string.h b/include/linux/string.h
index 9b3e578d9aca..a6a1953c52e3 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -557,16 +557,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
+ *
+ * This function determines whether the string pointed to by @s
+ * starts with the sequence of characters
+ * (excluding the terminating null character)
+ * in the string pointed to by @prefix.
+ *
+ * Returns:
+ * * @s + strlen(@prefix) if @s starts with @prefix
+ * * NULL if @s does not start with @prefix
+ */
+__nonnull_args(1, 2)
+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
diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c
index f84a60d4414c..6c5cc5f85516 100644
--- a/lib/tests/string_kunit.c
+++ b/lib/tests/string_kunit.c
@@ -299,6 +299,14 @@ static void string_test_streq(struct kunit *test)
KUNIT_ASSERT_FALSE(test, streq("", "bar"));
}
+static void string_test_strprefix(struct kunit *test)
+{
+ KUNIT_ASSERT_TRUE(test, streq(strprefix("foobar", "foo"), "bar"));
+ KUNIT_ASSERT_TRUE(test, streq(strprefix("foobar", ""), "foobar"));
+ KUNIT_ASSERT_TRUE(test, streq(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];
@@ -904,6 +912,7 @@ static struct kunit_case string_test_cases[] = {
KUNIT_CASE(string_test_strrchr),
KUNIT_CASE(string_test_strspn),
KUNIT_CASE(string_test_streq),
+ KUNIT_CASE(string_test_strprefix),
KUNIT_CASE(string_test_strcmp),
KUNIT_CASE(string_test_strcmp_long_strings),
KUNIT_CASE(string_test_strncmp),
--
2.55.0
Attachment:
signature.asc
Description: PGP signature