Skip to content

Commit

Permalink
regex: fix '\0' terminator always matched as last character(fix #19802)…
Browse files Browse the repository at this point in the history
… (#20104)
  • Loading branch information
shove70 committed Dec 6, 2023
1 parent 55e86fe commit 772f114
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion vlib/regex/regex.v
Expand Up @@ -2429,7 +2429,8 @@ pub fn (mut re RE) match_base(in_txt &u8, in_txt_len int) (int, int) {
// println("ist_simple_char")
state.match_flag = false

if re.prog[state.pc].ch == ch {
if re.prog[state.pc].ch == ch
&& (state.i < in_txt_len - 1 || re.prog[state.pc].ch != 0) {
state.match_flag = true
l_ist = regex.ist_simple_char

Expand Down
8 changes: 8 additions & 0 deletions vlib/regex/regex_test.v
Expand Up @@ -185,6 +185,14 @@ match_test_suite = [
// test last charr classes neg class
TestItem{"/a/", r"^/a/[^/]+$", -1,3},
TestItem{"/a/b",r"^/a/[^/]+$", 0,4},

// test `\0` as terminator
TestItem{"abc", "^abc\0$", -1,3},
TestItem{"abc\0", "^abc\0$", 0,4},

// test has `\0` chars
TestItem{"abcxyz", "^abc\0xyz$", -1,3},
TestItem{"abc\0xyz", "^abc\0xyz$", 0,7},
]
)

Expand Down

1 comment on commit 772f114

@penguindark
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tnx for the patch 💯

Please sign in to comment.