From d951737f2eff664d128ba7a1c893d5256f24d55c Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 26 Feb 2026 20:41:53 +0300 Subject: [PATCH] builtin: fix error when matching literal '*' using match_glob (fixes #22867) --- vlib/builtin/string.v | 39 ++++++++------------------- vlib/builtin/string_match_glob_test.v | 3 +++ 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 47520ddf6..d60b78e0a 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -2776,42 +2776,25 @@ pub fn (name string) match_glob(pattern string) bool { `[` { if nx < nlen { wanted_c := name[nx] - mut bstart := px mut is_inverted := false mut inner_match := false - mut inner_idx := bstart + 1 - mut inner_c := 0 - if inner_idx < plen { - inner_c = pattern[inner_idx] - if inner_c == `^` { - is_inverted = true - inner_idx++ - } + mut inner_idx := px + 1 + if inner_idx < plen && pattern[inner_idx] == `^` { + is_inverted = true + inner_idx++ } - for ; inner_idx < plen; inner_idx++ { - inner_c = pattern[inner_idx] - if inner_c == `]` { - break - } - if inner_c == wanted_c { + for ; inner_idx < plen && pattern[inner_idx] != `]`; inner_idx++ { + if pattern[inner_idx] == wanted_c { inner_match = true - for px < plen && pattern[px] != `]` { - px++ - } - break } } - if is_inverted { - if inner_match { - return false - } else { - px = inner_idx - } + if inner_idx < plen && ((inner_match && !is_inverted) + || (!inner_match && is_inverted)) { + px = inner_idx + 1 + nx++ + continue } } - px++ - nx++ - continue } else { // an ordinary character diff --git a/vlib/builtin/string_match_glob_test.v b/vlib/builtin/string_match_glob_test.v index 60ff5ffb3..7a878109d 100644 --- a/vlib/builtin/string_match_glob_test.v +++ b/vlib/builtin/string_match_glob_test.v @@ -68,6 +68,9 @@ fn test_match_glob_with_none_of_charset_patterns() { } fn test_match_glob_with_escaped_metachars() { + assert '10?x'.match_glob('*[?]x') + assert '10*x'.match_glob('*[*]x') + assert !'10+x'.match_glob('*[*]x') assert 'axbx?cxdxe'.match_glob('*x[?]c*') assert !'axbxXcxdxe'.match_glob('*x[?]c*') assert 'zaxbx*cxdxez'.match_glob('*x[Q*W]c*') -- 2.39.5