Skip to content

Commit

Permalink
fix: missing regexp capture panic (#477)
Browse files Browse the repository at this point in the history
Fix RegExp panic when a capture doesn't match but the full expression
does.
  • Loading branch information
stevenh authored Dec 5, 2022
1 parent 233dfa4 commit 898a889
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 7 additions & 2 deletions builtin_regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,17 @@ func builtinRegExpTest(call FunctionCall) Value {
var start int
n := 1
re := call.runtime.global.RegExp
empty := stringValue("")
for i, v := range result[2:] {
if i%2 == 0 {
start = v
} else {
re.defineProperty(fmt.Sprintf("$%d", n), stringValue(target[start:v]), 0o100, false)
if v == -1 {
// No match for this part.
re.defineProperty(fmt.Sprintf("$%d", n), empty, 0o100, false)
} else {
re.defineProperty(fmt.Sprintf("$%d", n), stringValue(target[start:v]), 0o100, false)
}
n++
if n == 10 {
break
Expand All @@ -81,7 +87,6 @@ func builtinRegExpTest(call FunctionCall) Value {

if n <= 9 {
// Erase remaining.
empty := stringValue("")
for i := n; i <= 9; i++ {
re.defineProperty(fmt.Sprintf("$%d", i), empty, 0o100, false)
}
Expand Down
5 changes: 5 additions & 0 deletions issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,11 @@ func Test_issue317(t *testing.T) {
regexp: "(1+)-(2+)-(3+)-(4+)-(5+)-(6+)-(7+)-(8+)-(9+)-(X+)",
want: "",
},
"missing-match": {
input: "data:image/png;base64,",
regexp: "^data:(.*?)(;(.*?))??(;base64)?,",
want: "data:image/png;base64,,image/png,,,;base64,,,,,",
},
}

previous := ",,,,,,,,,"
Expand Down

0 comments on commit 898a889

Please sign in to comment.