From 38650d994e8a2e6aa78348083efdd949fb103c52 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Sun, 29 Mar 2026 02:20:16 +0700 Subject: [PATCH] Fix fnmatch caused oom --- src/header/fnmatch/mod.rs | 7 +++++++ tests/expected/bins_dynamic/fnmatch.stdout | 1 + tests/expected/bins_static/fnmatch.stdout | 1 + tests/fnmatch.c | 1 + 4 files changed, 10 insertions(+) diff --git a/src/header/fnmatch/mod.rs b/src/header/fnmatch/mod.rs index eb856a08c0..8b09bc0759 100644 --- a/src/header/fnmatch/mod.rs +++ b/src/header/fnmatch/mod.rs @@ -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 }; diff --git a/tests/expected/bins_dynamic/fnmatch.stdout b/tests/expected/bins_dynamic/fnmatch.stdout index 14e0ae0140..48ba8aef2e 100644 --- a/tests/expected/bins_dynamic/fnmatch.stdout +++ b/tests/expected/bins_dynamic/fnmatch.stdout @@ -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" diff --git a/tests/expected/bins_static/fnmatch.stdout b/tests/expected/bins_static/fnmatch.stdout index 14e0ae0140..48ba8aef2e 100644 --- a/tests/expected/bins_static/fnmatch.stdout +++ b/tests/expected/bins_static/fnmatch.stdout @@ -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" diff --git a/tests/fnmatch.c b/tests/fnmatch.c index a126cbd152..ab88cfd3ad 100644 --- a/tests/fnmatch.c +++ b/tests/fnmatch.c @@ -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); }