Skip to content

Commit 648c7b5

Browse files
keegancsmithbradfitz
authored andcommitted
regexp/syntax: exclude full range from String negation case
If the char class is 0x0-0x10ffff we mistakenly would String that to `[^]`, which is not a valid regex. Fixes #31807 Change-Id: I9ceeaddc28b67b8e1de12b6703bcb124cc784556 Reviewed-on: https://go-review.googlesource.com/c/go/+/175679 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 24b4301 commit 648c7b5

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

src/regexp/syntax/parse_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ var parseTests = []parseTest{
185185
{`(?-s).`, `dnl{}`},
186186
{`(?:(?:^).)`, `cat{bol{}dot{}}`},
187187
{`(?-s)(?:(?:^).)`, `cat{bol{}dnl{}}`},
188+
{`[\s\S]a`, `cat{cc{0x0-0x10ffff}lit{a}}`},
188189

189190
// RE2 prefix_tests
190191
{`abc|abd`, `cat{str{ab}cc{0x63-0x64}}`},

src/regexp/syntax/regexp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func writeRegexp(b *strings.Builder, re *Regexp) {
139139
b.WriteRune('[')
140140
if len(re.Rune) == 0 {
141141
b.WriteString(`^\x00-\x{10FFFF}`)
142-
} else if re.Rune[0] == 0 && re.Rune[len(re.Rune)-1] == unicode.MaxRune {
142+
} else if re.Rune[0] == 0 && re.Rune[len(re.Rune)-1] == unicode.MaxRune && len(re.Rune) > 2 {
143143
// Contains 0 and MaxRune. Probably a negated class.
144144
// Print the gaps.
145145
b.WriteRune('^')

0 commit comments

Comments
 (0)