lib/glob: bug fixes, new features, and tests
Subsystems (3)
PATCH v2 7/8 lib/tests: add kunit tests for glob_match_nocase() and glob_validate()
In reply to Josh LawAdd parameterized test cases for the new glob API functions:
glob_match_nocase() tests verify case-insensitive matching for
literal characters, wildcards, character class ranges, and backslash escapes -- e.g., pattern "a*c" matches string "ABC", and range [A-Z] matches lowercase 'm'.
glob_validate() tests verify detection of malformed patterns:
well-formed patterns (character classes, escapes, wildcards) return true, while unclosed brackets ("[abc") and trailing backslashes ("abc\") return false.
--- lib/tests/glob_kunit.c | 77 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+)
lib/tests/glob_kunit.c +77 -0
@@ -157,8 +157,85 @@ static void glob_test_match(struct kunit *test) params->pat, params->str, params->expected); } +/* Case-insensitive matching tests */ +static const struct glob_test_case glob_nocase_test_cases[] = { + { .pat ="abc" , .str ="ABC" , .expected = true }, + { .pat ="ABC" , .str ="abc" , .expected = true }, + { .pat ="aBc" , .str ="AbC" , .expected = true }, + { .pat ="a*c" , .str ="ABC" , .expected = true }, + { .pat ="A?C" , .str ="abc" , .expected = true }, + { .pat ="[A-Z]" , .str ="m" , .expected = true }, + { .pat ="[a-z]" , .str ="M" , .expected = true }, + { .pat ="abc" , .str ="abd" , .expected = false }, + { .pat ="ABC" , .str ="ABD" , .expected = false }, + { .pat ="a*z" , .str ="ABZ" , .expected = true }, + { .pat ="\\A" , .str ="a" , .expected = true }, +}; + +KUNIT_ARRAY_PARAM (glob_nocase, glob_nocase_test_cases, glob_case_to_desc); + +static void glob_test_match_nocase (struct kunit *test) +{ +const struct glob_test_case *params = test->param_value; + +KUNIT_EXPECT_EQ_MSG (test, +glob_match_nocase (params->pat, params->str), + params->expected, +"nocase Pattern: \"%s\", String: \"%s\", Expected: %d" , + params->pat, params->str, params->expected); +} + +/* Pattern validation tests */ +struct glob_validate_case { +const char *pat; +bool expected; +}; + +static const struct glob_validate_case glob_validate_test_cases[] = { + { .pat ="abc" , .expected = true }, + { .pat ="*" , .expected = true }, + { .pat ="?" , .expected = true }, + { .pat ="[abc]" , .expected = true }, + { .pat ="[!abc]" , .expected = true }, + { .pat ="[^abc]" , .expected = true }, + { .pat ="[a-z]" , .expected = true }, + { .pat ="[]abc]" , .expected = true }, + { .pat ="\\*" , .expected = true }, + { .pat ="\\\\" , .expected = true }, + { .pat ="" , .expected = true }, +/* Invalid patterns */ + { .pat ="[" , .expected = false }, + { .pat ="[abc" , .expected = false }, + { .pat ="[!abc" , .expected = false }, + { .pat ="abc\\" , .expected = false }, + { .pat ="\\" , .expected = false }, + { .pat ="abc[def" , .expected = false }, +}; + +static void glob_validate_case_to_desc (const struct glob_validate_case *t, +char *desc) +{ +snprintf (desc,KUNIT_PARAM_DESC_SIZE ,"pat:\"%s\"" , t->pat); +} + +KUNIT_ARRAY_PARAM (glob_validate, glob_validate_test_cases, + glob_validate_case_to_desc); + +static void glob_test_validate (struct kunit *test) +{ +const struct glob_validate_case *params = test->param_value; + +KUNIT_EXPECT_EQ_MSG (test, +glob_validate (params->pat), + params->expected, +"validate Pattern: \"%s\", Expected: %d" , + params->pat, params->expected); +} +static struct kunit_case glob_kunit_test_cases[] = {KUNIT_CASE_PARAM (glob_test_match, glob_gen_params), +KUNIT_CASE_PARAM (glob_test_match_nocase, glob_nocase_gen_params), +KUNIT_CASE_PARAM (glob_test_validate, glob_validate_gen_params), {} };
Signature · 1 line
2.34.1
v2 cover lib/glob: bug fixes, new features, and tests
Bug fixes, extensions, and test coverage for lib/glob.c:
Patches 1-3 fix corner cases in the existing glob_match():
- Inverted character class ranges like [z-a] are now normalized instead of silently failing to match.
- A trailing backslash is treated as a literal '\' rather than reading past the end of the pattern string.
- [^...] is accepted as an alias for [!...] to match the regex-style negation syntax documented in glob(7).
Patches 4-5 add two new utility functions:
- glob_match_nocase() for case-insensitive matching, useful for subsystems like ATA that need case-folded denylist comparisons.
- glob_validate() for checking pattern syntax before use, so callers can reject malformed patterns early with a clear error.
Patches 6-7 add kunit test coverage:
- 47 new test cases for glob_match covering escapes, inverted ranges, caret negation, edge cases, and unclosed brackets.
- 11 test cases for glob_match_nocase.
- 17 test cases for glob_validate.
Patch 8 adds a real in-tree caller for glob_validate() in the kunit executor, validating user-provided filter_glob patterns and returning -EINVAL for malformed ones.
Changes since v1:
- Added patch 8 (kunit executor caller for glob_validate) so that both new exported functions have in-tree users or documented intended users.
- Updated glob_match_nocase() commit message to reference the ATA denylist as the intended caller (follow-up patch).
Josh Law (8):
lib/glob: normalize inverted character class ranges lib/glob: treat trailing backslash as literal character lib/glob: accept [^...] as character class negation syntax lib/glob: add case-insensitive glob_match_nocase() lib/glob: add glob_validate() for pattern syntax checking lib/tests: add glob test cases for escapes, edge cases, and new
features
lib/tests: add kunit tests for glob_match_nocase() and glob_validate()
kunit: validate glob filter patterns before use
include/linux/glob.h | 2 + lib/glob.c | 122 +++++++++++++++++++++++++++++++++++++--- lib/kunit/executor.c | 13 +++++ lib/tests/glob_kunit.c | 124 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 252 insertions(+), 9 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 2/8 lib/glob: treat trailing backslash as literal character
A pattern ending with a lone backslash (e.g., "path\\") reads the NUL terminator as the escaped character, making the backslash effectively match end-of-string rather than a literal '\' character. This means glob_match("path\\", "path\\") would fail since the pattern consumes the NUL early, while glob_match("path\\", "path") would unexpectedly succeed.
Guard the escape so that a trailing backslash keeps d = '\\' and falls through to literal matching, which is the intuitive behavior:
a trailing backslash matches a literal backslash character.
--- lib/glob.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/glob.c +2 -1
@@ -112,7 +112,8 @@ bool __pure glob_match(char const *pat, char const *str) }break ;case '\\' : - d = *pat++; +if (*pat !='\0' ) + d = *pat++; fallthrough;default :/* Literal character */ literal:
Signature · 1 line
2.34.1
PATCH v2 6/8 lib/tests: add glob test cases for escapes, edge cases, and new features
In reply to Josh LawThe glob kunit test suite had no coverage for backslash escape sequences, and was missing edge cases around wildcards with empty strings, inverted ranges, unclosed brackets, and consecutive wildcards.
Add test cases covering:
- Backslash escapes: \*, \?, \[, \\, non-metachar after backslash, and trailing backslash matching a literal '\' character.
- Wildcards matching special characters in the string (*, [, \, ?).
- Wildcards with empty strings: * matches empty, ** matches empty, ? does not match empty.
- Inverted (backwards) ranges: [z-a] and [9-0] now match thanks to endpoint normalization.
- Single-character ranges: [a-a] matches 'a'.
- Caret negation: [^a] and [^a-z] as alias for [!...].
- Unclosed bracket: [ treated as literal.
- Consecutive wildcards: ***a, *?*, *?*?* patterns.
--- lib/tests/glob_kunit.c | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)
lib/tests/glob_kunit.c +47 -0
@@ -80,6 +80,53 @@ static const struct glob_test_case glob_test_cases[] = { { .pat = "*bc", .str = "bc", .expected = true }, { .pat = "*bc", .str = "bbc", .expected = true }, { .pat = "*bc", .str = "bcbc", .expected = true }, + /* Backslash escape sequences */ + { .pat = "\\*", .str = "*", .expected = true }, + { .pat = "\\*", .str = "a", .expected = false }, + { .pat = "\\?", .str = "?", .expected = true }, + { .pat = "\\?", .str = "a", .expected = false }, + { .pat = "\\[", .str = "[", .expected = true }, + { .pat = "\\[", .str = "a", .expected = false }, + { .pat = "a\\bc", .str = "abc", .expected = true }, + { .pat = "\\\\", .str = "\\", .expected = true }, + { .pat = "\\\\", .str = "a", .expected = false }, + /* Trailing backslash (matches literal backslash) */ + { .pat = "a\\", .str = "a\\", .expected = true }, + { .pat = "a\\", .str = "a", .expected = false }, + /* Wildcards matching special characters in string */ + { .pat = "?", .str = "*", .expected = true }, + { .pat = "?", .str = "[", .expected = true }, + { .pat = "?", .str = "\\", .expected = true }, + { .pat = "?", .str = "?", .expected = true }, + /* Wildcards with empty strings */ + { .pat = "*", .str = "", .expected = true }, + { .pat = "*", .str = "a", .expected = true }, + { .pat = "**", .str = "", .expected = true }, + { .pat = "**", .str = "abc", .expected = true }, + { .pat = "?", .str = "", .expected = false }, + /* Inverted (backwards) ranges */ + { .pat = "[z-a]", .str = "m", .expected = true }, + { .pat = "[z-a]", .str = "z", .expected = true }, + { .pat = "[z-a]", .str = "a", .expected = true }, + { .pat = "[9-0]", .str = "5", .expected = true }, + { .pat = "[9-0]", .str = "a", .expected = false }, + /* Single-character range */ + { .pat = "[a-a]", .str = "a", .expected = true }, + { .pat = "[a-a]", .str = "b", .expected = false }, + /* Caret negation syntax */ + { .pat = "[^a]", .str = "b", .expected = true }, + { .pat = "[^a]", .str = "a", .expected = false }, + { .pat = "[^a-z]", .str = "0", .expected = true }, + { .pat = "[^a-z]", .str = "m", .expected = false }, + /* Unclosed bracket (treated as literal) */ + { .pat = "[", .str = "[", .expected = true }, + { .pat = "[", .str = "a", .expected = false }, + { .pat = "[abc", .str = "[abc", .expected = true }, + /* Consecutive wildcards */ + { .pat = "***a", .str = "a", .expected = true }, + { .pat = "*?*", .str = "a", .expected = true }, + { .pat = "*?*?*", .str = "ab", .expected = true }, + { .pat = "*?*?*", .str = "a", .expected = false }, /* Multiple asterisks (complex backtracking) */ { .pat = "*ac*", .str = "abacadaeafag", .expected = true }, { .pat = "*ac*ae*ag*", .str = "abacadaeafag", .expected = true },
Signature · 1 line
2.34.1
PATCH v2 5/8 lib/glob: add glob_validate() for pattern syntax checking
In reply to Josh LawAdd 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.
--- include/linux/glob.h | 1 + lib/glob.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+)
include/linux/glob.h +1 -0
@@ -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 */
lib/glob.c +44 -0
47 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 8/8 kunit: validate glob filter patterns before use
In reply to Josh Lawkunit_parse_glob_filter() accepts user-provided glob patterns via the filter_glob module parameter but does not check whether they are well-formed. A malformed pattern like "suite[.test" (unclosed bracket) or "suite\" (trailing backslash) is silently passed to glob_match() which handles it gracefully but not in the way the user likely intended — the bracket is matched as a literal character rather than starting a character class.
Use glob_validate() to reject malformed patterns early with -EINVAL, so users get a clear error instead of silently wrong filter results.
--- lib/kunit/executor.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
lib/kunit/executor.c +13 -0
@@ -91,6 +91,8 @@ static int kunit_parse_glob_filter(struct kunit_glob_filter *parsed,const char *period =strchr (filter_glob,'.' );if (!period) { +if (!glob_validate (filter_glob)) +return -EINVAL ; parsed->suite_glob =kstrdup (filter_glob,GFP_KERNEL );if (!parsed->suite_glob)return -ENOMEM ;
@@ -102,12 +104,23 @@ static int kunit_parse_glob_filter(struct kunit_glob_filter *parsed,if (!parsed->suite_glob)return -ENOMEM ; +if (!glob_validate (parsed->suite_glob)) { +kfree (parsed->suite_glob); +return -EINVAL ; + } + parsed->test_glob =kstrdup (period +1 ,GFP_KERNEL );if (!parsed->test_glob) {kfree (parsed->suite_glob);return -ENOMEM ; } +if (!glob_validate (parsed->test_glob)) { +kfree (parsed->test_glob); +kfree (parsed->suite_glob); +return -EINVAL ; + } +return 0 ; }
Signature · 1 line
2.34.1
PATCH v2 3/8 lib/glob: accept [^...] as character class negation syntax
In reply to Josh Lawglob(7) specifies [!...] for negated character classes, but many users are more familiar with the regex-style [^...] syntax. Tools like git, rsync, and bash all accept both forms, and the difference is a common source of confusion when writing glob patterns.
Accept '^' as an alias for '!' at the start of a character class so that both [!a-z] and [^a-z] work as expected.
--- lib/glob.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
lib/glob.c +4 -4
@@ -33,9 +33,9 @@ MODULE_LICENSE("Dual MIT/GPL");* Like !fnmatch(@pat, @str, 0) and unlike the shell, this does NOT * treat / or leading . specially; it isn't actually used for pathnames. * -* Note that according to glob(7) (and unlike bash), character classes -* are complemented by a leading !; this does not support the regex-style -* [^a-z] syntax. +* Note that according to glob(7), character classes are complemented by +* a leading !. The regex-style [^a-z] syntax is also accepted as an +* alias. * * An opening bracket without a matching close is matched literally. */
@@ -72,7 +72,7 @@ bool __pure glob_match(char const *pat, char const *str)case '[' : {/* Character class */ if (c =='\0' )/* No possible match */ return false; -bool match = false, inverted = (*pat =='!' ); +bool match = false, inverted = (*pat =='!' || *pat =='^' );char const *class = inverted ? pat +1 : pat;unsigned char a = *class++;
Signature · 1 line
2.34.1
PATCH v2 4/8 lib/glob: add case-insensitive glob_match_nocase()
In reply to Josh LawAdd a case-insensitive variant of glob_match() for callers that need to match patterns regardless of case.
The immediate motivation is the ATA device quirk denylist in drivers/ata/libata-core.c, which uses glob_match() to match drive model and firmware revision strings. ATA IDENTIFY DEVICE data is space-padded ASCII and the specification does not mandate a particular case, so a denylist entry could miss a drive that reports its model string in unexpected casing. Switching the denylist to glob_match_nocase() (in a follow-up ATA patch) would make it robust against such variation.
Refactor the matching logic into a static __glob_match() helper that takes a bool nocase parameter, and implement both glob_match() and glob_match_nocase() as thin wrappers. When nocase is true, literal characters and character class endpoints are folded through tolower() before comparison.
--- include/linux/glob.h | 1 + lib/glob.c | 73 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 64 insertions(+), 10 deletions(-)
include/linux/glob.h +1 -0
@@ -6,5 +6,6 @@#include <linux/compiler.h> /* For __pure */ bool __pure glob_match (char const *pat,char const *str); +bool __pure glob_match_nocase (char const *pat,char const *str);#endif /* _LINUX_GLOB_H */
lib/glob.c +63 -10
113 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 1/8 lib/glob: normalize inverted character class ranges
In reply to Josh LawWhen a character class range has its endpoints reversed (e.g., [z-a]), the comparison a <= c && c <= b can never be true, so the range silently matches nothing. A pattern like "file[9-0]" intended to match any digit would fail to match anything, with no indication that the range is backwards.
Swap the endpoints when a > b so that inverted ranges behave the same as their forward equivalents: [z-a] matches the same characters as [a-z], and [9-0] matches the same as [0-9]. This is consistent with how GNU fnmatch and other glob implementations handle reversed ranges.
--- lib/glob.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
lib/glob.c +7 -1
@@ -94,7 +94,13 @@ bool __pure glob_match(char const *pat, char const *str) goto literal; class +=2 ; -/* Any special action if a > b? */ +/* Normalize inverted ranges like [z-a] */ +if (a > b) { +unsigned char tmp = a; + + a = b; + b = tmp; + } }if (a <= c && c <= b) match = true;
Signature · 1 line
2.34.1