Fix fnmatch caused oom

This commit is contained in:
Wildan M
2026-03-29 02:20:16 +07:00
parent ab654db071
commit 38650d994e
4 changed files with 10 additions and 0 deletions
+7
View File
@@ -43,6 +43,7 @@ unsafe fn tokenize(mut pattern: *const u8, flags: c_int) -> Tree {
}
let mut leading = true;
let mut need_collapsing = false;
let mut builder = TreeBuilder::default();
builder.start_internal(Token::Root, Range(1, Some(1)));
@@ -55,6 +56,12 @@ unsafe fn tokenize(mut pattern: *const u8, flags: c_int) -> Tree {
let c = unsafe { *pattern };
pattern = unsafe { pattern.offset(1) };
match (c == b'*', need_collapsing) {
(true, true) => continue,
(true, false) => need_collapsing = true,
(false, _) => need_collapsing = false,
}
let (token, range) = match c {
b'\\' if flags & FNM_NOESCAPE == FNM_NOESCAPE => {
let c = unsafe { *pattern };
@@ -29,3 +29,4 @@ Should fail:
"[a!][a!]" doesn't match "ab"
"hello[/+]world" doesn't match "hello/world"
"hello world" doesn't match "HELLO WORLD"
"********************a" doesn't match "xxxxxxxxxxxxxxxxxxxxb"
@@ -29,3 +29,4 @@ Should fail:
"[a!][a!]" doesn't match "ab"
"hello[/+]world" doesn't match "hello/world"
"hello world" doesn't match "HELLO WORLD"
"********************a" doesn't match "xxxxxxxxxxxxxxxxxxxxb"
+1
View File
@@ -44,4 +44,5 @@ int main(void) {
test("[a!][a!]", "ab", 0);
test("hello[/+]world", "hello/world", FNM_PATHNAME);
test("hello world", "HELLO WORLD", 0);
test("********************a", "xxxxxxxxxxxxxxxxxxxxb", 0);
}