git: 11a3bc02374b - main - libc/fnmatch/rangematch: fix uninitialized variable read in a condition
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Sun, 22 Nov 2026 16:16:41 UTC
The branch main has been updated by avg:
URL: https://cgit.FreeBSD.org/src/commit/?id=11a3bc02374b6517e9bfcaaf68c9b5b9902c043c
commit 11a3bc02374b6517e9bfcaaf68c9b5b9902c043c
Author: Andriy Gapon <avg@FreeBSD.org>
AuthorDate: 2026-11-22 15:21:01 +0000
Commit: Andriy Gapon <avg@FreeBSD.org>
CommitDate: 2026-11-22 16:16:31 +0000
libc/fnmatch/rangematch: fix uninitialized variable read in a condition
The condition was logically incorrect and could also result in access to
an uninitialized variable. The bug happened because of misplaced
parentheses in the complex condition. There is another very similar
condition in the code and it has the correct placement, so it does not
have the bug. I used it as an example for the fix. Both conditions
came from the same commit af79566b9315.
For me, the bug appeared as a random failure to match a simple pattern
with '[0-9]' in it. The value of the uninitialized variable depended on
the value left on the stack by other code, so the bug appeared only
under very specific conditions.
FWIW, gcc catches and reports the uninitialized variable problem.
clang does it only with -Wconditional-uninitialized but that flag leads
to too many false positives.
I also found a pattern that deterministically reveals the bug in a
different way without depending on the uninitialized value, through
incorrect logic. The pattern is: '[[:alpha:]0-9]'.
Easy demonstration:
$ mkdir test && cd test
$ touch a 5
$ find . -name '[[:alpha:]0-9]'
./a
File '5' should also have been found.
I've added a test case to cover that.
Fixes: af79566b9315
MFC after: 1 week
---
lib/libc/gen/fnmatch.c | 4 ++--
lib/libc/tests/gen/fnmatch_test.c | 3 +++
2 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/lib/libc/gen/fnmatch.c b/lib/libc/gen/fnmatch.c
index 1bb020da229b..7605cf61315f 100644
--- a/lib/libc/gen/fnmatch.c
+++ b/lib/libc/gen/fnmatch.c
@@ -410,8 +410,8 @@ treat_like_collating_symbol:
if (c2 == EOS)
return (RANGE_ERROR);
- if ((c2 == '[' && (special = *pattern) == '.') ||
- special == '=' || special == ':') {
+ if (c2 == '[' && ((special = *pattern) == '.' ||
+ special == '=' || special == ':')) {
/*
* No equivalence classes or character
diff --git a/lib/libc/tests/gen/fnmatch_test.c b/lib/libc/tests/gen/fnmatch_test.c
index 0ff7400a4a4f..360a242cdad5 100644
--- a/lib/libc/tests/gen/fnmatch_test.c
+++ b/lib/libc/tests/gen/fnmatch_test.c
@@ -198,6 +198,9 @@ ATF_TC_BODY(fnmatch_characterclass, tc)
ATF_CHECK(fnmatch("[[:punct:]]", ".", 0) == 0);
ATF_CHECK(fnmatch("[[:xdigit:]]", "f", 0) == 0);
+ /* A range following a character class in the same bracket. */
+ ATF_CHECK(fnmatch("[[:alpha:]0-9]", "5", 0) == 0);
+
/*
* POSIX.1, section 9.3.5. states that '[:' and ':]'
* should be interpreted as character classes symbol only