Repository navigation
libs/libc/fnmatch: Fix the match of a set; add FNM_CASEFOLD and FNM_LEADING_DIR. - #21097
Merged
Merged
Conversation
A set without ranges skipped every second character: "[xyz]" did not
match "y", and "[a-cxyz]" did not match "y". The loop read one
character as a possible start of a range and then stepped over the next
one. A '-' before the closing ']' ("[a-]") was taken as a range to ']'.
Read the set one item at a time: a character, or a range when a '-' and
one more character other than ']' follow. A set without the closing ']'
does not match any more.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
|
acassis
previously approved these changes
Oct 28, 2026
Programs use two flags of fnmatch() that POSIX does not have, but other C libraries do: FNM_CASEFOLD ignores the case, and FNM_LEADING_DIR lets the pattern match the string up to a '/'. <fnmatch.h> did not define them, so these programs did not compile. Define them with the values that the other C libraries use (8 and 16), and the aliases FNM_IGNORECASE and FNM_FILE_NAME. With FNM_CASEFOLD, fnmatch() compares the characters and the members of a set without case; a range matches if the character in either case is in it. With FNM_LEADING_DIR, a match ends at a '/' in the string as at its end. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
Add a page for the fnmatch() of libc: the pattern syntax that it supports, the flags FNM_CASEFOLD and FNM_LEADING_DIR, and the flags that have no effect. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
casaroli
force-pushed
the
c-fnmatch-flags-up
branch
from
October 29, 2026 17:18
6b9f187 to
321cd2a
Compare
acassis
approved these changes
Oct 30, 2026
xiaoxiang781216
approved these changes
Oct 30, 2026
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two problems in
fnmatch()(libs/libc/misc/lib_fnmatch.c):[xyz]did not matchy, and[a-cxyz]did not matchy. The loop read one character as a possible start of a range and then stepped over the next one. A-before the closing]([a-]) was taken as a range to]. The first commit reads the set one item at a time: a character, or a range when a-and one more character other than]follow. A set without the closing]no longer matches.FNM_CASEFOLDignores the case, andFNM_LEADING_DIRlets the pattern match the string up to a/.<fnmatch.h>did not define them, so these programs did not compile. The second commit defines them with the values of the other C libraries (16 and 8) and the aliasesFNM_IGNORECASEandFNM_FILE_NAME, and implements them, also in sets and ranges.The third commit adds
Documentation/os/libs/libc/fnmatch.rst: the pattern syntax that this small matcher supports, the two flags, and the flags that have no effect.Impact
fnmatch()results change for patterns with sets:[xyz]matchesy(was no match),[a-]matches-, and a set without]does not match. Patterns without sets do not change.glob()usesfnmatch(), so a glob pattern such as*.[ch]now also matchesx.h. The procfs patterns ([0-9]*, one range) give the same results.<fnmatch.h>; without them nothing else changes.All build modes.
Testing
Tested on master aeab6fb and on this PR rebased on it.
Test programs on
qemu-armv8a:knsh(QEMU with HVF):fnmset.c: 9 set cases without flags[xyz]/y,[a-cxyz]/y,[a-]/-fnmtest.c: the 9 set cases + 26 cases withFNM_CASEFOLD,FNM_LEADING_DIRand their combinations withFNM_PATHNAME/FNM_PERIODqemu-armv8a:knshandqemu-armv8a:nshBuild, each OK:
qemu-armv8a:nsh(flat, arm64),qemu-armv8a:knsh(kernel, arm64),lm3s6965-ek:qemu-protected(protected, armv7-m),rv-virt:nsh64(flat, rv64),rv-virt:pnsh64(protected, rv64),rv-virt:knsh64(kernel, rv64).tools/checkpatch.sh -c -u -m -gpasses.