lib/glob: fixes, new features, and test coverage
cover lib/glob: fixes, new features, and test coverage
This series addresses several issues found in lib/glob.c and adds new functionality with comprehensive test coverage.
Bug fixes:
- Normalize inverted character class ranges like [z-a] so they match the same characters as [a-z] instead of silently matching nothing (patch 1).
- Treat a trailing backslash as a literal '\\' character instead of consuming the NUL terminator and matching end-of-string (patch 2).
New features:
- Accept [^...] as an alias for [!...] in character class negation, matching the behavior of git, rsync, and bash (patch 3).
- Add glob_match_nocase() for case-insensitive pattern matching, useful for device model strings and filter expressions where case should not matter (patch 4).
- Add glob_validate() to check pattern syntax upfront, detecting unclosed character classes and trailing backslashes so callers that accept user input can reject malformed patterns with a clear error (patch 5).
Test coverage:
- Add 47 test cases covering backslash escapes, inverted ranges, caret negation, wildcards with empty strings, unclosed brackets, consecutive wildcards, and special characters (patch 6).
- Add parameterized tests for glob_match_nocase() and
glob_validate() (patch 7).
Build-tested with both lib/glob.o and lib/tests/glob_kunit.o.
Josh Law (7):
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()
include/linux/glob.h | 2 + lib/glob.c | 122 +++++++++++++++++++++++++++++++++++++--- lib/tests/glob_kunit.c | 124 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 239 insertions(+), 9 deletions(-)
Signature · 1 line
2.34.1
PATCH 2/7 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 4/7 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. For example, ATA device model strings or ftrace function filters may benefit from case-insensitive matching without requiring both the pattern and target string to be lowercased beforehand.
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 1/7 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
PATCH 3/7 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 5/7 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 6/7 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 7/7 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