Skip to content

Commit

Permalink
Fix empty regular expression matches (fix #2565)
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny authored and nicowilliams committed Jul 9, 2023
1 parent cac216a commit 600e602
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ static jv f_match(jq_state *jq, jv input, jv regex, jv modifiers, jv testmode) {
jv_string((char*)ebuf)));
break;
}
} while (global && start != end);
} while (global && start <= end);
onig_region_free(region,1);
region = NULL;
if (region)
Expand Down
24 changes: 13 additions & 11 deletions tests/onig.test
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# match builtin
[match("( )*"; "g")]
"abc"
[{"offset":0, "length":0, "string":"", "captures":[]},{"offset":1, "length":0, "string":"", "captures":[]},{"offset":2, "length":0, "string":"", "captures":[]}]
[{"offset":0, "length":0, "string":"", "captures":[]}, {"offset":1, "length":0, "string":"", "captures":[]}, {"offset":2, "length":0, "string":"", "captures":[]}, {"offset":3, "length":0, "string":"", "captures":[]}]

[match("( )*"; "gn")]
"abc"
[]

[match(""; "g")]
"ab"
[{"offset":0,"length":0,"string":"","captures":[]},{"offset":1,"length":0,"string":"","captures":[]},{"offset":2,"length":0,"string":"","captures":[]}]

[match("a"; "gi")]
"āáàä"
[]
Expand Down Expand Up @@ -71,29 +75,27 @@ gsub("a";"b")
"aaaaa"
"bbbbb"

gsub( "(.*)"; ""; "x")
gsub("(.*)"; ""; "x")
""
""

gsub( ""; "a"; "g")
gsub(""; "a"; "g")
""
"a"

gsub( "^"; ""; "g")
gsub("^"; ""; "g")
"a"
"a"


# The following is a regression test and should not be construed as a requirement other than that execution should terminate:
gsub( ""; "a"; "g")
gsub(""; "a"; "g")
"a"
"aa"
"aaa"

gsub( "$"; "a"; "g")
gsub("$"; "a"; "g")
"a"
"aa"

gsub( "^"; "a")
gsub("^"; "a")
""
"a"

Expand Down Expand Up @@ -163,5 +165,5 @@ sub("(?<x>.)"; "\(.x)!")
# splits and _nwise
[splits("")]
"ab"
["","a","b"]
["","a","b",""]

0 comments on commit 600e602

Please sign in to comment.