public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/7] lib/glob: fixes, new features, and test coverage
@ 2026-03-15 17:10 Josh Law
  2026-03-15 17:10 ` [PATCH 1/7] lib/glob: normalize inverted character class ranges Josh Law
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Josh Law @ 2026-03-15 17:10 UTC (permalink / raw)
  To: Andrew Morton, Josh Law; +Cc: linux-kernel

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(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/7] lib/glob: normalize inverted character class ranges
  2026-03-15 17:10 [PATCH 0/7] lib/glob: fixes, new features, and test coverage Josh Law
@ 2026-03-15 17:10 ` Josh Law
  2026-03-15 17:10 ` [PATCH 2/7] lib/glob: treat trailing backslash as literal character Josh Law
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Josh Law @ 2026-03-15 17:10 UTC (permalink / raw)
  To: Andrew Morton, Josh Law; +Cc: linux-kernel

When 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.

Signed-off-by: Josh Law <objecting@objecting.org>
---
 lib/glob.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/lib/glob.c b/lib/glob.c
index 7aca76c25bcb..cb45a9a47f28 100644
--- a/lib/glob.c
+++ b/lib/glob.c
@@ -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;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/7] lib/glob: treat trailing backslash as literal character
  2026-03-15 17:10 [PATCH 0/7] lib/glob: fixes, new features, and test coverage Josh Law
  2026-03-15 17:10 ` [PATCH 1/7] lib/glob: normalize inverted character class ranges Josh Law
@ 2026-03-15 17:10 ` Josh Law
  2026-03-15 17:11 ` [PATCH 3/7] lib/glob: accept [^...] as character class negation syntax Josh Law
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Josh Law @ 2026-03-15 17:10 UTC (permalink / raw)
  To: Andrew Morton, Josh Law; +Cc: linux-kernel

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.

Signed-off-by: Josh Law <objecting@objecting.org>
---
 lib/glob.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/glob.c b/lib/glob.c
index cb45a9a47f28..df7f00619b1b 100644
--- a/lib/glob.c
+++ b/lib/glob.c
@@ -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:
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 3/7] lib/glob: accept [^...] as character class negation syntax
  2026-03-15 17:10 [PATCH 0/7] lib/glob: fixes, new features, and test coverage Josh Law
  2026-03-15 17:10 ` [PATCH 1/7] lib/glob: normalize inverted character class ranges Josh Law
  2026-03-15 17:10 ` [PATCH 2/7] lib/glob: treat trailing backslash as literal character Josh Law
@ 2026-03-15 17:11 ` Josh Law
  2026-03-15 17:11 ` [PATCH 4/7] lib/glob: add case-insensitive glob_match_nocase() Josh Law
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Josh Law @ 2026-03-15 17:11 UTC (permalink / raw)
  To: Andrew Morton, Josh Law; +Cc: linux-kernel

glob(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.

Signed-off-by: Josh Law <objecting@objecting.org>
---
 lib/glob.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/glob.c b/lib/glob.c
index df7f00619b1b..c5508be0d215 100644
--- a/lib/glob.c
+++ b/lib/glob.c
@@ -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++;
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 4/7] lib/glob: add case-insensitive glob_match_nocase()
  2026-03-15 17:10 [PATCH 0/7] lib/glob: fixes, new features, and test coverage Josh Law
                   ` (2 preceding siblings ...)
  2026-03-15 17:11 ` [PATCH 3/7] lib/glob: accept [^...] as character class negation syntax Josh Law
@ 2026-03-15 17:11 ` Josh Law
  2026-03-15 17:11 ` [PATCH 5/7] lib/glob: add glob_validate() for pattern syntax checking Josh Law
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Josh Law @ 2026-03-15 17:11 UTC (permalink / raw)
  To: Andrew Morton, Josh Law; +Cc: linux-kernel

Add 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.

Signed-off-by: Josh Law <objecting@objecting.org>
---
 include/linux/glob.h |  1 +
 lib/glob.c           | 73 ++++++++++++++++++++++++++++++++++++++------
 2 files changed, 64 insertions(+), 10 deletions(-)

diff --git a/include/linux/glob.h b/include/linux/glob.h
index 861327b33e41..36527ae89730 100644
--- a/include/linux/glob.h
+++ b/include/linux/glob.h
@@ -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 */
diff --git a/lib/glob.c b/lib/glob.c
index c5508be0d215..9c71ccc15abc 100644
--- a/lib/glob.c
+++ b/lib/glob.c
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
+#include <linux/ctype.h>
 #include <linux/module.h>
 #include <linux/glob.h>
 #include <linux/export.h>
@@ -11,8 +12,8 @@
 MODULE_DESCRIPTION("glob(7) matching");
 MODULE_LICENSE("Dual MIT/GPL");
 
-/**
- * glob_match - Shell-style pattern matching, like !fnmatch(pat, str, 0)
+/*
+ * __glob_match - Shell-style pattern matching, like !fnmatch(pat, str, 0)
  * @pat: Shell-style pattern to match, e.g. "*.[ch]".
  * @str: String to match.  The pattern must match the entire string.
  *
@@ -39,7 +40,7 @@ MODULE_LICENSE("Dual MIT/GPL");
  *
  * An opening bracket without a matching close is matched literally.
  */
-bool __pure glob_match(char const *pat, char const *str)
+static bool __pure __glob_match(char const *pat, char const *str, bool nocase)
 {
 	/*
 	 * Backtrack to previous * on mismatch and retry starting one
@@ -58,6 +59,11 @@ bool __pure glob_match(char const *pat, char const *str)
 		unsigned char c = *str++;
 		unsigned char d = *pat++;
 
+		if (nocase) {
+			c = tolower(c);
+			d = tolower(d);
+		}
+
 		switch (d) {
 		case '?':	/* Wildcard: anything but nul */
 			if (c == '\0')
@@ -94,13 +100,17 @@ bool __pure glob_match(char const *pat, char const *str)
 						goto literal;
 
 					class += 2;
-					/* Normalize inverted ranges like [z-a] */
-					if (a > b) {
-						unsigned char tmp = a;
+				}
+				if (nocase) {
+					a = tolower(a);
+					b = tolower(b);
+				}
+				/* Normalize inverted ranges like [z-a] */
+				if (a > b) {
+					unsigned char tmp = a;
 
-						a = b;
-						b = tmp;
-					}
+					a = b;
+					b = tmp;
 				}
 				if (a <= c && c <= b)
 					match = true;
@@ -112,8 +122,11 @@ bool __pure glob_match(char const *pat, char const *str)
 			}
 			break;
 		case '\\':
-			if (*pat != '\0')
+			if (*pat != '\0') {
 				d = *pat++;
+				if (nocase)
+					d = tolower(d);
+			}
 			fallthrough;
 		default:	/* Literal character */
 literal:
@@ -132,4 +145,44 @@ bool __pure glob_match(char const *pat, char const *str)
 		}
 	}
 }
+
+/**
+ * glob_match - Shell-style pattern matching, like !fnmatch(pat, str, 0)
+ * @pat: Shell-style pattern to match, e.g. "*.[ch]".
+ * @str: String to match.  The pattern must match the entire string.
+ *
+ * Perform shell-style glob matching, returning true (1) if the match
+ * succeeds, or false (0) if it fails.  Equivalent to !fnmatch(@pat, @str, 0).
+ *
+ * Pattern metacharacters are ?, *, [ and \.
+ * (And, inside character classes, !, ^ - and ].)
+ *
+ * This is small and simple and non-recursive, with run-time at most
+ * quadratic: strlen(@str)*strlen(@pat).  It does not preprocess the
+ * patterns.  An opening bracket without a matching close is matched
+ * literally.
+ *
+ * Return: true if @str matches @pat, false otherwise.
+ */
+bool __pure glob_match(char const *pat, char const *str)
+{
+	return __glob_match(pat, str, false);
+}
 EXPORT_SYMBOL(glob_match);
+
+/**
+ * glob_match_nocase - Case-insensitive shell-style pattern matching
+ * @pat: Shell-style pattern to match.
+ * @str: String to match.  The pattern must match the entire string.
+ *
+ * Identical to glob_match(), but performs case-insensitive comparisons
+ * for literal characters and character class contents using tolower().
+ * Metacharacters (?, *, [, ]) are not affected by case folding.
+ *
+ * Return: true if @str matches @pat (case-insensitive), false otherwise.
+ */
+bool __pure glob_match_nocase(char const *pat, char const *str)
+{
+	return __glob_match(pat, str, true);
+}
+EXPORT_SYMBOL(glob_match_nocase);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 5/7] lib/glob: add glob_validate() for pattern syntax checking
  2026-03-15 17:10 [PATCH 0/7] lib/glob: fixes, new features, and test coverage Josh Law
                   ` (3 preceding siblings ...)
  2026-03-15 17:11 ` [PATCH 4/7] lib/glob: add case-insensitive glob_match_nocase() Josh Law
@ 2026-03-15 17:11 ` Josh Law
  2026-03-15 17:11 ` [PATCH 6/7] lib/tests: add glob test cases for escapes, edge cases, and new features Josh Law
  2026-03-15 17:11 ` [PATCH 7/7] lib/tests: add kunit tests for glob_match_nocase() and glob_validate() Josh Law
  6 siblings, 0 replies; 8+ messages in thread
From: Josh Law @ 2026-03-15 17:11 UTC (permalink / raw)
  To: Andrew Morton, Josh Law; +Cc: linux-kernel

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


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 6/7] lib/tests: add glob test cases for escapes, edge cases, and new features
  2026-03-15 17:10 [PATCH 0/7] lib/glob: fixes, new features, and test coverage Josh Law
                   ` (4 preceding siblings ...)
  2026-03-15 17:11 ` [PATCH 5/7] lib/glob: add glob_validate() for pattern syntax checking Josh Law
@ 2026-03-15 17:11 ` Josh Law
  2026-03-15 17:11 ` [PATCH 7/7] lib/tests: add kunit tests for glob_match_nocase() and glob_validate() Josh Law
  6 siblings, 0 replies; 8+ messages in thread
From: Josh Law @ 2026-03-15 17:11 UTC (permalink / raw)
  To: Andrew Morton, Josh Law; +Cc: linux-kernel

The 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.

Signed-off-by: Josh Law <objecting@objecting.org>
---
 lib/tests/glob_kunit.c | 47 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/lib/tests/glob_kunit.c b/lib/tests/glob_kunit.c
index 362b1eda8e5b..9b53060e7c8e 100644
--- a/lib/tests/glob_kunit.c
+++ b/lib/tests/glob_kunit.c
@@ -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 },
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 7/7] lib/tests: add kunit tests for glob_match_nocase() and glob_validate()
  2026-03-15 17:10 [PATCH 0/7] lib/glob: fixes, new features, and test coverage Josh Law
                   ` (5 preceding siblings ...)
  2026-03-15 17:11 ` [PATCH 6/7] lib/tests: add glob test cases for escapes, edge cases, and new features Josh Law
@ 2026-03-15 17:11 ` Josh Law
  6 siblings, 0 replies; 8+ messages in thread
From: Josh Law @ 2026-03-15 17:11 UTC (permalink / raw)
  To: Andrew Morton, Josh Law; +Cc: linux-kernel

Add 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.

Signed-off-by: Josh Law <objecting@objecting.org>
---
 lib/tests/glob_kunit.c | 77 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)

diff --git a/lib/tests/glob_kunit.c b/lib/tests/glob_kunit.c
index 9b53060e7c8e..711cb44d1fc8 100644
--- a/lib/tests/glob_kunit.c
+++ b/lib/tests/glob_kunit.c
@@ -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),
 	{}
 };
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-03-15 17:11 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-15 17:10 [PATCH 0/7] lib/glob: fixes, new features, and test coverage Josh Law
2026-03-15 17:10 ` [PATCH 1/7] lib/glob: normalize inverted character class ranges Josh Law
2026-03-15 17:10 ` [PATCH 2/7] lib/glob: treat trailing backslash as literal character Josh Law
2026-03-15 17:11 ` [PATCH 3/7] lib/glob: accept [^...] as character class negation syntax Josh Law
2026-03-15 17:11 ` [PATCH 4/7] lib/glob: add case-insensitive glob_match_nocase() Josh Law
2026-03-15 17:11 ` [PATCH 5/7] lib/glob: add glob_validate() for pattern syntax checking Josh Law
2026-03-15 17:11 ` [PATCH 6/7] lib/tests: add glob test cases for escapes, edge cases, and new features Josh Law
2026-03-15 17:11 ` [PATCH 7/7] lib/tests: add kunit tests for glob_match_nocase() and glob_validate() Josh Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox