aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2018-12-04 15:42:32 -0500
committerBryan C. Mills <bcmills@google.com>2018-12-07 14:48:50 +0000
commitd01ccd8ee871d6eaaaed52851c6ce8b49993f33a (patch)
treeaf61bde31a31e53a4e5e1089eaf28bed540d542a
parent5aedc8af94c0a8ffc58cbd09993192dea9b238db (diff)
downloadgo-d01ccd8ee871d6eaaaed52851c6ce8b49993f33a.tar.gz
go-d01ccd8ee871d6eaaaed52851c6ce8b49993f33a.zip
[release-branch.go1.11-security] cmd/go/internal/get: use a strings.Replacer in expand
This should be a no-op, but produces deterministic (and more correct) behavior if we have accidentally failed to sanitize one of the inputs. Change-Id: I1271d0ffd01a691ec8c84906c4e02d9e2be19c72 Reviewed-on: https://team-review.git.corp.google.com/c/372705 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
-rw-r--r--src/cmd/go/internal/get/vcs.go8
1 files changed, 6 insertions, 2 deletions
diff --git a/src/cmd/go/internal/get/vcs.go b/src/cmd/go/internal/get/vcs.go
index 5cd164f2ff..ff4f9d12ef 100644
--- a/src/cmd/go/internal/get/vcs.go
+++ b/src/cmd/go/internal/get/vcs.go
@@ -966,10 +966,14 @@ func matchGoImport(imports []metaImport, importPath string) (metaImport, error)
// expand rewrites s to replace {k} with match[k] for each key k in match.
func expand(match map[string]string, s string) string {
+ // We want to replace each match exactly once, and the result of expansion
+ // must not depend on the iteration order through the map.
+ // A strings.Replacer has exactly the properties we're looking for.
+ oldNew := make([]string, 0, 2*len(match))
for k, v := range match {
- s = strings.Replace(s, "{"+k+"}", v, -1)
+ oldNew = append(oldNew, "{"+k+"}", v)
}
- return s
+ return strings.NewReplacer(oldNew...).Replace(s)
}
// vcsPaths defines the meaning of import paths referring to