From: Bernard Ladenthin <bernard.ladenthin@gmail.com>
To: akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org, pablo@netfilter.org, fw@strlen.de,
netfilter-devel@vger.kernel.org, kunit-dev@googlegroups.com,
davem@davemloft.net,
Bernard Ladenthin <bernard.ladenthin@gmail.com>
Subject: [PATCH 4/4] lib/ts_fsm: document that a match must consume the remaining data
Date: Sun, 16 Aug 2026 19:05:40 +0200 [thread overview]
Message-ID: <20260816170541.3384-5-bernard.ladenthin@gmail.com> (raw)
In-Reply-To: <20260816170541.3384-1-bernard.ladenthin@gmail.com>
fsm_find() reports a match only once the token chain has matched and the
data is exhausted:
for (tok_idx = 0; tok_idx < fsm->ntokens; tok_idx++) { ... }
if (end_of_data())
goto found_match;
no_match:
return UINT_MAX;
A chain of three specific tokens therefore matches the text "abc" but not
"abcd". [TS_FSM_HEAD_IGNORE, a, b] does not find "ab" in "xxabyy".
Searching for a pattern in the middle of the data needs TS_FSM_HEAD_IGNORE
at the front and a TS_FSM_ANY token at the end. The latter short-circuits
through "if (next == NULL) goto found_match".
The file header explains the head anchoring but says nothing about the
tail, which makes the interface easy to misuse. Describe it.
This documents the behaviour as it stands. If the end-of-data requirement
is not intended, the fix belongs in fsm_find() and this patch should be
dropped in favour of that.
Signed-off-by: Bernard Ladenthin <bernard.ladenthin@gmail.com>
---
This is my first kernel submission. Corrections on anything I got wrong in
the process are welcome.
lib/ts_fsm.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/lib/ts_fsm.c b/lib/ts_fsm.c
index 053615f4fcd7..ceec6295505c 100644
--- a/lib/ts_fsm.c
+++ b/lib/ts_fsm.c
@@ -18,6 +18,13 @@
* is enabled by default and can be disabled by inserting
* TS_FSM_HEAD_IGNORE as the first token in the chain.
*
+ * A match is only reported once the data has been consumed as well: the
+ * token chain has to account for every remaining octet, not just for the
+ * pattern itself. A chain of three specific tokens therefore matches the
+ * text "abc" but not "abcd". To look for a pattern somewhere in the
+ * middle of the data, prepend a token with TS_FSM_HEAD_IGNORE and append
+ * one with TS_FSM_ANY, the latter matching whatever follows.
+ *
* The runtime performance of the algorithm should be around O(n),
* however while in strict mode the average runtime can be better.
*/
--
2.49.0.windows.1
prev parent reply other threads:[~2026-08-16 17:07 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-16 17:05 [PATCH 0/4] lib/textsearch: fix ts_bm resume offset, add tests, two small cleanups Bernard Ladenthin
2026-08-16 17:05 ` [PATCH 1/4] lib/ts_bm: advance state->offset past the reported match Bernard Ladenthin
2026-08-16 20:37 ` Pablo Neira Ayuso
2026-08-16 17:05 ` [PATCH 2/4] lib/tests: add KUnit tests for the textsearch infrastructure Bernard Ladenthin
2026-08-16 17:05 ` [PATCH 3/4] textsearch: align ts_state.cb like skb->cb Bernard Ladenthin
2026-08-16 17:05 ` Bernard Ladenthin [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260816170541.3384-5-bernard.ladenthin@gmail.com \
--to=bernard.ladenthin@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=davem@davemloft.net \
--cc=fw@strlen.de \
--cc=kunit-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=pablo@netfilter.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®