Skip to content

Commit 8fc3b60

Browse files
committed
fixes dlclark#49 when extracting literals from pattern copy the bytes to prevent concats from smashing the pattern later
1 parent 5596495 commit 8fc3b60

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

regexp_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,3 +1220,24 @@ func TestFuzzBytes_Match(t *testing.T) {
12201220
})
12211221
}
12221222
}
1223+
1224+
func TestConcatAccidentalPatternCharge(t *testing.T) {
1225+
// originally this pattern would parse incorrectly
1226+
// specifically the closing group would concat the string literals
1227+
// together but the raw rune slice would blow over the original pattern
1228+
// so the final bit of pattern parsing would be wrong
1229+
// fixed in #49
1230+
r, err := Compile(`(?<=1234\.\*56).*(?=890)`, 0)
1231+
1232+
if err != nil {
1233+
panic(err)
1234+
}
1235+
1236+
m, err := r.FindStringMatch(`1234.*567890`)
1237+
if err != nil {
1238+
panic(err)
1239+
}
1240+
if m == nil {
1241+
t.Fatal("Expected non-nil, got nil")
1242+
}
1243+
}

syntax/parser.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2067,7 +2067,8 @@ func (p *parser) addToConcatenate(pos, cch int, isReplacement bool) {
20672067
}
20682068

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

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

0 commit comments

Comments
 (0)