From: Josh Law <objecting@objecting.org>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Brendan Higgins <brendan.higgins@linux.dev>,
David Gow <david@davidgow.net>, Rae Moar <raemoar63@gmail.com>,
linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com,
linux-kernel@vger.kernel.org, Josh Law <objecting@objecting.org>
Subject: [PATCH v2 5/8] lib/glob: add glob_validate() for pattern syntax checking
Date: Sun, 15 Mar 2026 20:18:13 +0000 [thread overview]
Message-ID: <20260315201816.362559-6-objecting@objecting.org> (raw)
In-Reply-To: <20260315201816.362559-1-objecting@objecting.org>
Add glob_validate() which checks whether a glob pattern is
syntactically well-formed before matching. It detects:
- Unclosed character classes: a '[' with no matching ']'
- Trailing backslash: a '\' at end of pattern with nothing to escape
glob_match() already handles these gracefully (unclosed brackets are
matched literally, a trailing backslash matches itself), but callers
like ftrace filters or sysfs attribute stores that accept patterns
from userspace may want to reject malformed input upfront with a
clear error rather than silently falling back to literal matching.
Signed-off-by: Josh Law <objecting@objecting.org>
---
include/linux/glob.h | 1 +
lib/glob.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 45 insertions(+)
diff --git a/include/linux/glob.h b/include/linux/glob.h
index 36527ae89730..deceaa2e4a74 100644
--- a/include/linux/glob.h
+++ b/include/linux/glob.h
@@ -7,5 +7,6 @@
bool __pure glob_match(char const *pat, char const *str);
bool __pure glob_match_nocase(char const *pat, char const *str);
+bool __pure glob_validate(char const *pat);
#endif /* _LINUX_GLOB_H */
diff --git a/lib/glob.c b/lib/glob.c
index 9c71ccc15abc..800163ed4dbf 100644
--- a/lib/glob.c
+++ b/lib/glob.c
@@ -186,3 +186,47 @@ bool __pure glob_match_nocase(char const *pat, char const *str)
return __glob_match(pat, str, true);
}
EXPORT_SYMBOL(glob_match_nocase);
+
+/**
+ * glob_validate - Check whether a glob pattern is well-formed
+ * @pat: Shell-style pattern to validate.
+ *
+ * Return: true if @pat is a syntactically valid glob pattern, false
+ * if it contains malformed constructs. The following are considered
+ * invalid:
+ *
+ * - An opening '[' with no matching ']' (unclosed character class).
+ * - A trailing '\' with no character following it.
+ *
+ * Note that glob_match() handles these gracefully (an unclosed bracket
+ * is matched literally, a trailing backslash matches itself), but
+ * callers that accept patterns from user input may wish to reject
+ * malformed patterns early with a clear error.
+ */
+bool __pure glob_validate(char const *pat)
+{
+ while (*pat) {
+ switch (*pat++) {
+ case '\\':
+ if (*pat == '\0')
+ return false;
+ pat++;
+ break;
+ case '[': {
+ if (*pat == '!' || *pat == '^')
+ pat++;
+ /* ] as first character is literal, not end of class */
+ if (*pat == ']')
+ pat++;
+ while (*pat && *pat != ']')
+ pat++;
+ if (*pat == '\0')
+ return false;
+ pat++; /* skip ']' */
+ break;
+ }
+ }
+ }
+ return true;
+}
+EXPORT_SYMBOL(glob_validate);
--
2.34.1
next prev parent reply other threads:[~2026-03-15 20:18 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-15 20:18 [PATCH v2 0/8] lib/glob: bug fixes, new features, and tests Josh Law
2026-03-15 20:18 ` [PATCH v2 1/8] lib/glob: normalize inverted character class ranges Josh Law
2026-03-15 20:18 ` [PATCH v2 2/8] lib/glob: treat trailing backslash as literal character Josh Law
2026-03-15 20:18 ` [PATCH v2 3/8] lib/glob: accept [^...] as character class negation syntax Josh Law
2026-03-15 20:18 ` [PATCH v2 4/8] lib/glob: add case-insensitive glob_match_nocase() Josh Law
2026-03-15 20:18 ` Josh Law [this message]
2026-03-15 20:18 ` [PATCH v2 6/8] lib/tests: add glob test cases for escapes, edge cases, and new features Josh Law
2026-03-15 20:18 ` [PATCH v2 7/8] lib/tests: add kunit tests for glob_match_nocase() and glob_validate() Josh Law
2026-03-15 20:18 ` [PATCH v2 8/8] kunit: validate glob filter patterns before use Josh Law
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=20260315201816.362559-6-objecting@objecting.org \
--to=objecting@objecting.org \
--cc=akpm@linux-foundation.org \
--cc=brendan.higgins@linux.dev \
--cc=david@davidgow.net \
--cc=kunit-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=raemoar63@gmail.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.