Skip to content

Commit

Permalink
Only fail on invalid \k back references, allowing things like '\'' an…
Browse files Browse the repository at this point in the history
…d '\<' to pass.
  • Loading branch information
dop251 authored and dlclark committed Nov 16, 2020
1 parent 9ff549b commit a2a8dda
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 12 additions & 0 deletions regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,18 @@ func TestECMANamedGroup(t *testing.T) {
if err == nil {
t.Fatal("Expected error")
}

re = MustCompile(`\'|\<?`, 0)
if m, err := re.MatchString("'"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
if m, err := re.MatchString("<"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
}

func TestECMAInvalidEscapeCharClass(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion syntax/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,7 @@ func (p *parser) scanBasicBackslash(scanOnly bool) (*regexNode, error) {
return nil, p.getErr(ErrIllegalEndEscape)
}
angled := false
k := false
close := '\x00'

backpos := p.textpos()
Expand Down Expand Up @@ -1217,6 +1218,7 @@ func (p *parser) scanBasicBackslash(scanOnly bool) (*regexNode, error) {
}

ch = p.rightChar(0)
k = true

} else if !p.useOptionE() && (ch == '<' || ch == '\'') && p.charsRight() > 1 { // Note angle without \g
angled = true
Expand Down Expand Up @@ -1275,7 +1277,9 @@ func (p *parser) scanBasicBackslash(scanOnly bool) (*regexNode, error) {
}
return nil, p.getErr(ErrUndefinedNameRef, capname)
} else {
return nil, p.getErr(ErrMalformedNameRef)
if k {
return nil, p.getErr(ErrMalformedNameRef)
}
}
}

Expand Down

0 comments on commit a2a8dda

Please sign in to comment.