[PATCH v3 5/8] lib/glob: add glob_validate() for pattern syntax checking
From: Josh Law
Date: Sun Mar 15 2026 - 17:18:15 EST
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
that accept patterns from userspace may want to reject malformed input
upfront with a clear error rather than silently falling back to
literal matching.
For example, the kunit executor accepts a filter_glob module parameter
to select which tests to run. A user who types "snd_*.[codec_test"
(forgetting the closing bracket) would currently see the '[' matched
literally instead of starting a character class, producing silently
wrong filter results. With glob_validate(), the executor can reject
the pattern early and report -EINVAL, as done in the following patch.
Signed-off-by: Josh Law <objecting@xxxxxxxxxxxxx>
---
include/linux/glob.h | 1 +
lib/glob.c | 43 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 44 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 172b8ba3cd8e..8ee539d19cc4 100644
--- a/lib/glob.c
+++ b/lib/glob.c
@@ -186,3 +186,46 @@ 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