]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/get: use a strings.Replacer in expand
authorBryan C. Mills <bcmills@google.com>
Tue, 4 Dec 2018 20:42:32 +0000 (15:42 -0500)
committerDmitri Shuralyov <dmitshur@golang.org>
Fri, 14 Dec 2018 01:02:16 +0000 (01:02 +0000)
This should be a no-op, but produces deterministic (and more correct)
behavior if we have accidentally failed to sanitize one of the inputs.

Updates #29231

Change-Id: I1271d0ffd01a691ec8c84906c4e02d9e2be19c72
Reviewed-on: https://team-review.git.corp.google.com/c/370575
Reviewed-by: Russ Cox <rsc@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-on: https://go-review.googlesource.com/c/154103
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/internal/get/vcs.go

index 173934b84ec96d4f435953f9d0fa88d9e3ab74cf..052c82b7b564d692296a317cc0258756babb8d84 100644 (file)
@@ -963,10 +963,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.ReplaceAll(s, "{"+k+"}", v)
+               oldNew = append(oldNew, "{"+k+"}", v)
        }
-       return s
+       return strings.NewReplacer(oldNew...).Replace(s)
 }
 
 // vcsPaths defines the meaning of import paths referring to