Skip to content

Commit aaa5f47

Browse files
author
Bryan C. Mills
committed
[release-branch.go1.10-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/372707 Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 7ef6ee2 commit aaa5f47

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/cmd/go/internal/get/vcs.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,10 +970,14 @@ func matchGoImport(imports []metaImport, importPath string) (metaImport, error)
970970

971971
// expand rewrites s to replace {k} with match[k] for each key k in match.
972972
func expand(match map[string]string, s string) string {
973+
// We want to replace each match exactly once, and the result of expansion
974+
// must not depend on the iteration order through the map.
975+
// A strings.Replacer has exactly the properties we're looking for.
976+
oldNew := make([]string, 0, 2*len(match))
973977
for k, v := range match {
974-
s = strings.Replace(s, "{"+k+"}", v, -1)
978+
oldNew = append(oldNew, "{"+k+"}", v)
975979
}
976-
return s
980+
return strings.NewReplacer(oldNew...).Replace(s)
977981
}
978982

979983
// vcsPaths defines the meaning of import paths referring to

0 commit comments

Comments
 (0)