Fwd: [postcr] 2 possible bugs in 11a3bc02374b ("libc/fnmatch/rangematch: fix uninitialized variable read in a condition")

From: Andriy Gapon <avg_at_FreeBSD.org>
Date: Sun, 22 Nov 2026 16:43:57 UTC
I've got this feedback from the post-commit "AI" reviewer.
I think that those are valid (pre-existing) issues.
But I do not have time right now to work on them.


-------- Forwarded Message --------
Subject: [postcr] 2 possible bugs in 11a3bc02374b ("libc/fnmatch/rangematch: fix 
uninitialized variable read in a condition")
Date: Sun, 22 Nov 2026 16:34:06 +0000
From: Post-Commit LLM Review Service <markj@freebsd.org>
To: Andriy Gapon <avg@FreeBSD.org>
CC: Mark Johnston <markj@FreeBSD.org>

This mail was generated by an LLM (claude-opus-5-5). It may be wrong. Contact 
Mark Johnston <markj@FreeBSD.org> with any complaints or suggestions.

Please add the following line to the log message of any commit which fixes a bug 
reported here:

Reported by:	llm-postcr

commit 11a3bc02374b6517e9bfcaaf68c9b5b9902c043c
https://cgit.freebsd.org/src/commit/?id=11a3bc02374b6517e9bfcaaf68c9b5b9902c043c

POSTCR-11a3bc02374b6517e9bfcaaf68c9b5b9902c043c-1: pre-existing: rangematch() 
end-of-bracket skip loop ignores backslash escapes, so fnmatch("[a\\]]", "a", 0) 
fails and "a]" matches

This commit does not add the bug. It is an older bug in rangematch(), introduced 
by af79566b9315 ("fnmatch: Add support for collating symbols, equivalence 
classes, and character classes"). Since that commit, the main loop stops (break) 
as soon as one element matches. A separate "go to end of bracket expression" 
loop (fnmatch.c:460-485) then finds the closing ']'. That loop does not parse 
the pattern the same way the main loop does.

The skip loop does not handle backslash escapes. If an escaped ']' comes after 
the matching element, the loop takes it as the end of the bracket, and the real 
']' is left behind as a literal. Before af79566b9315, [a\]] matched "a" and "]". 
Now it matches "]" and "a]". [a\]]b no longer matches "ab", and [!...\]]-style 
patterns end early in the same way. This is visible through find(1) -name, 
.gitignore-style matchers and anything else that calls fnmatch(3).

Suggested fix (covers this bug and 
POSTCR-11a3bc02374b6517e9bfcaaf68c9b5b9902c043c-2): loop on special != 0 || 
*pattern != ']', and when special == 0 and FNM_NOESCAPE is clear, skip the 
character after a backslash. The reviewer reproduced the bug with this tree's 
fnmatch.c built in a harness, and on the host's 16.0-CURRENT libc. 
fnmatch_testcases.h does not cover this case, so a test for it is worth adding.

POSTCR-11a3bc02374b6517e9bfcaaf68c9b5b9902c043c-2: pre-existing: rangematch() 
skip loop stops at ']' inside [.].] / [=]=], so fnmatch("[a[.].]]x", "ax", 0) fails

This commit does not add the bug either. It was also introduced by af79566b9315. 
As described in POSTCR-11a3bc02374b6517e9bfcaaf68c9b5b9902c043c-1, the main loop 
breaks on the first matching element. A separate skip loop (fnmatch.c:460-485) 
then finds the closing ']', but it does not parse the pattern the same way the 
main loop does.

In the skip loop, while (*pattern != ']') is tested before the special state. 
After "[." or "[=", a ']' at the start of the symbol's contents ends the loop. 
In the main loop, [.].] is a legal collating symbol for ']'.

Suggested fix: the same as for 
POSTCR-11a3bc02374b6517e9bfcaaf68c9b5b9902c043c-1. Loop on special != 0 || 
*pattern != ']', and when special == 0 and FNM_NOESCAPE is clear, skip the 
character after a backslash. The reviewer reproduced the bug with this tree's 
fnmatch.c built in a harness, and on the host's 16.0-CURRENT libc. 
fnmatch_testcases.h does not cover this case, so a test for it is worth adding.