Skip to content

Commit

Permalink
fixes dlclark#49 when extracting literals from pattern copy the bytes…
Browse files Browse the repository at this point in the history
… to prevent concats from smashing the pattern later
  • Loading branch information
dlclark committed May 2, 2022
1 parent 5596495 commit 8fc3b60
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
21 changes: 21 additions & 0 deletions regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1220,3 +1220,24 @@ func TestFuzzBytes_Match(t *testing.T) {
})
}
}

func TestConcatAccidentalPatternCharge(t *testing.T) {
// originally this pattern would parse incorrectly
// specifically the closing group would concat the string literals
// together but the raw rune slice would blow over the original pattern
// so the final bit of pattern parsing would be wrong
// fixed in #49
r, err := Compile(`(?<=1234\.\*56).*(?=890)`, 0)

if err != nil {
panic(err)
}

m, err := r.FindStringMatch(`1234.*567890`)
if err != nil {
panic(err)
}
if m == nil {
t.Fatal("Expected non-nil, got nil")
}
}
3 changes: 2 additions & 1 deletion syntax/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2067,7 +2067,8 @@ func (p *parser) addToConcatenate(pos, cch int, isReplacement bool) {
}

if cch > 1 {
str := p.pattern[pos : pos+cch]
str := make([]rune, cch)
copy(str, p.pattern[pos:pos+cch])

if p.useOptionI() && !isReplacement {
// We do the ToLower character by character for consistency. With surrogate chars, doing
Expand Down

0 comments on commit 8fc3b60

Please sign in to comment.