Skip to content

Commit cbdad4f

Browse files
fengyoulinmatloob
authored andcommitted
cmd/go: check pattern for utf8 validity before call regexp.MustCompile
Do not panic if the package path or the package version contains invalid UTF-8 characters. Fixes #75251 Change-Id: Ib787e74277cf814253857b911d378ea5e53d8824 Reviewed-on: https://go-review.googlesource.com/c/go/+/700815 Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Ian Alexander <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent c2d85eb commit cbdad4f

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/cmd/go/internal/modget/query.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"regexp"
1111
"strings"
1212
"sync"
13+
"unicode/utf8"
1314

1415
"cmd/go/internal/base"
1516
"cmd/go/internal/gover"
@@ -285,6 +286,11 @@ func reportError(q *query, err error) {
285286
// TODO(bcmills): Use errors.As to unpack these errors instead of parsing
286287
// strings with regular expressions.
287288

289+
if !utf8.ValidString(q.pattern) || !utf8.ValidString(q.version) {
290+
base.Errorf("go: %s", errStr)
291+
return
292+
}
293+
288294
patternRE := regexp.MustCompile("(?m)(?:[ \t(\"`]|^)" + regexp.QuoteMeta(q.pattern) + "(?:[ @:;)\"`]|$)")
289295
if patternRE.MatchString(errStr) {
290296
if q.rawVersion == "" {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Issue #75251: Don't panic if the package path or the package version
2+
# contains invalid UTF-8 characters.
3+
4+
go mod init m
5+
6+
! go get golang.org/x/net/http/httpguts�v0.43.0 # contains 0xff byte
7+
! stderr panic
8+
stderr 'malformed module path'
9+
10+
! go get golang.org/x/net/http/httpguts�@v0.43.0 # contains 0xff byte
11+
! stderr panic
12+
stderr 'malformed module path'
13+
14+
! go get golang.org/x/net/http/httpguts@�v0.43.0 # contains 0xff byte
15+
! stderr panic
16+
stderr 'disallowed version string'

src/cmd/internal/pkgpattern/pkgpattern.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package pkgpattern
77
import (
88
"regexp"
99
"strings"
10+
"unicode/utf8"
1011
)
1112

1213
// Note: most of this code was originally part of the cmd/go/internal/search
@@ -71,7 +72,7 @@ func matchPatternInternal(pattern string, vendorExclude bool) func(name string)
7172

7273
const vendorChar = "\x00"
7374

74-
if vendorExclude && strings.Contains(pattern, vendorChar) {
75+
if vendorExclude && strings.Contains(pattern, vendorChar) || !utf8.ValidString(pattern) {
7576
return func(name string) bool { return false }
7677
}
7778

0 commit comments

Comments
 (0)