Re: [PATCH v1 0/2] Add strprefix() and fix a bug with it
From: Alejandro Colomar
Date: Thu Nov 05 2026 - 06:44:49 EST
Hi Andy,
> Date: 2026-11-05 13:24:18+0200
> From: Andy Shevchenko <andy.shevchenko@xxxxxxxxx>
>
> On Thu, Nov 5, 2026 at 1:13 PM Alejandro Colomar <alx@xxxxxxxxxx> wrote:
> >
> > str_has_prefix() is based on sizes and lengths, which means it must
> > overload the valid length 0 to mean not found. This is a typical
> > consequence of choosing to design an API around sizes and lengths, and
> > vanishes when moving to pointer-based similar APIs.
> >
> > This patch set adds strprefix(), deprecates str_has_prefix(), and uses
> > strprefix() in one place to fix a bug reported by Bartosz.
> >
> > We can later work on replacing the other uses of str_has_prefix(), which
> > might hide similar bugs.
> >
> > Testing
> > ~~~~~~~
> >
> > I've built this patch set with 'allmodconfig'. I've not run the
> > resulting kernel, though.
>
> No, all lib/ new code should be accompanied with new (Kunit) test
> cases. Update the respective lib/tests/* for that or if not yet
> converted to Kunit something in lib/*test*.c
Thanks! Does this sound good to you?
diff --git i/lib/tests/string_kunit.c w/lib/tests/string_kunit.c
index 0819ace5b027..702030dbb93b 100644
--- i/lib/tests/string_kunit.c
+++ w/lib/tests/string_kunit.c
@@ -287,6 +287,14 @@ static void string_test_strspn(struct kunit *test)
}
}
+static void string_test_strprefix(struct kunit *test)
+{
+ KUNIT_ASSERT_EQ(test, strcmp(strprefix("foobar", "foo"), "bar"), 0);
+ KUNIT_ASSERT_EQ(test, strcmp(strprefix("foobar", ""), "foobar"), 0);
+ KUNIT_ASSERT_EQ(test, strcmp(strprefix("foobar", "foobar"), ""), 0);
+ KUNIT_ASSERT_NULL(test, strprefix("foobar", "bar"));
+}
+
static char strcmp_buffer1[STRCMP_LARGE_BUF_LEN];
static char strcmp_buffer2[STRCMP_LARGE_BUF_LEN];
BTW, I don't see any existing tests of str_has_prefix() --the API I'm
replacing-- under lib/.
$ grep -rn str_has_prefix lib/
$
Have a lovely day!
Alex
--
<https://www.alejandro-colomar.es>
Attachment:
signature.asc
Description: PGP signature