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 v3 1/8] lib/glob: normalize inverted character class ranges
Date: Sun, 15 Mar 2026 21:16:30 +0000 [thread overview]
Message-ID: <20260315211641.408318-3-objecting@objecting.org> (raw)
In-Reply-To: <20260315211641.408318-1-objecting@objecting.org>
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
next prev parent reply other threads:[~2026-03-15 21:16 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-15 21:16 [PATCH v3 0/8] lib/glob: bug fixes, new features, and tests Josh Law
2026-03-15 21:16 ` [PATCH v2 1/5] lib/glob: add case-insensitive glob_match_nocase() Josh Law
2026-03-15 21:16 ` Josh Law [this message]
2026-03-15 21:16 ` [PATCH v2 2/5] lib/glob: add glob_validate() for pattern syntax checking Josh Law
2026-03-15 21:16 ` [PATCH v3 2/8] lib/glob: treat trailing backslash as literal character Josh Law
2026-03-15 21:16 ` [PATCH v3 3/8] lib/glob: accept [^...] as character class negation syntax Josh Law
2026-03-15 21:16 ` [PATCH v2 3/5] lib/tests: add glob test cases for escapes, edge cases, and new features Josh Law
2026-03-15 21:16 ` [PATCH v3 4/8] lib/glob: add case-insensitive glob_match_nocase() Josh Law
2026-03-15 21:16 ` [PATCH v2 4/5] lib/tests: add kunit tests for glob_match_nocase() and glob_validate() Josh Law
2026-03-15 21:16 ` [PATCH v2 5/5] kunit: validate glob filter patterns before use Josh Law
2026-03-15 21:16 ` [PATCH v3 5/8] lib/glob: add glob_validate() for pattern syntax checking Josh Law
2026-03-15 21:16 ` [PATCH v3 6/8] lib/tests: add glob test cases for escapes, edge cases, and new features Josh Law
2026-03-15 21:16 ` [PATCH v3 7/8] lib/tests: add kunit tests for glob_match_nocase() and glob_validate() Josh Law
2026-03-15 21:16 ` [PATCH v3 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=20260315211641.408318-3-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox