Skip to content

libs/libc/fnmatch: Fix the match of a set; add FNM_CASEFOLD and FNM_LEADING_DIR. - #21097

Merged
xiaoxiang781216 merged 3 commits into
apache:masterfrom
casaroli:c-fnmatch-flags-up
Oct 30, 2026
Merged

xiaoxiang781216 merged 3 commits into
apache:masterfrom
casaroli:c-fnmatch-flags-up

Conversation

@casaroli

Copy link
Copy Markdown
Contributor

Summary

Two problems in fnmatch() (libs/libc/misc/lib_fnmatch.c):

  • 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 ]. 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.
  • Programs use two flags 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. The second commit defines them with the values of the other C libraries (16 and 8) and the aliases FNM_IGNORECASE and FNM_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] matches y (was no match), [a-] matches -, and a set without ] does not match. Patterns without sets do not change. glob() uses fnmatch(), so a glob pattern such as *.[ch] now also matches x.h. The procfs patterns ([0-9]*, one range) give the same results.
  • New flags and macros in <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):

Test master this PR
fnmset.c: 9 set cases without flags 3 of 9 FAIL: [xyz]/y, [a-cxyz]/y, [a-]/- 9 of 9 PASS
fnmtest.c: the 9 set cases + 26 cases with FNM_CASEFOLD, FNM_LEADING_DIR and their combinations with FNM_PATHNAME/FNM_PERIOD does not compile (flags not defined) 35 of 35 PASS
ostest, qemu-armv8a:knsh and qemu-armv8a:nsh status 0 status 0

Build, 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 -g passes.

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]>
@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium labels Oct 28, 2026
@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

esp32-devkitc

  • ROM: .flash.text +168 B (+0.1%, 127,952 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +168 B (+0.2%, 93,928 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +176 B (+0.2%, 84,708 B / 4,194,304 B, total: 2% used)

qemu-armv8a

  • Code: .text.fnmatch +12 B (+0.1%, 353,936 B)

qemu-intel64

  • Code: .text +324 B (+0.0%, 8,687,307 B)

s698pm-dkit

  • Code: .text +672 B (+0.2%, 383,264 B)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

acassis
acassis previously approved these changes Oct 28, 2026
Comment thread libs/libc/misc/lib_fnmatch.c Resolved
Comment thread libs/libc/misc/lib_fnmatch.c Resolved
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]>
@xiaoxiang781216
xiaoxiang781216 merged commit 23f5a4f into apache:master Oct 30, 2026
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants