]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: avoid type names in __debug__modinfo__ variable injected in package main
authorBryan C. Mills <bcmills@google.com>
Wed, 12 Sep 2018 14:41:29 +0000 (10:41 -0400)
committerBryan C. Mills <bcmills@google.com>
Wed, 12 Sep 2018 18:46:57 +0000 (18:46 +0000)
If we use the name 'string' to refer to the built-in type, that name
can be shadowed by a local declaration. Use a string constant instead,
but keep the init function to populate it so that //go:linkname will
still work properly.

Fixes #27584.

Change-Id: I850cad6663e566f70fd123107d2e4e742c93b450
Reviewed-on: https://go-review.googlesource.com/134915
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/go/internal/modload/build.go
src/cmd/go/testdata/script/mod_string_alias.txt [new file with mode: 0644]

index cebb802db9fc9f06ae8fb32023ade4c827bc574f..06636c4f4ff4277900030763458b1b9e5f3b4a50 100644 (file)
@@ -232,11 +232,16 @@ func findModule(target, path string) module.Version {
 }
 
 func ModInfoProg(info string) []byte {
+       // Inject a variable with the debug information as runtime/debug.modinfo,
+       // but compile it in package main so that it is specific to the binary.
+       // Populate it in an init func so that it will work with go:linkname,
+       // but use a string constant instead of the name 'string' in case
+       // package main shadows the built-in 'string' with some local declaration.
        return []byte(fmt.Sprintf(`
                package main
                import _ "unsafe"
                //go:linkname __debug_modinfo__ runtime/debug.modinfo
-               var __debug_modinfo__ string
+               var __debug_modinfo__ = ""
                func init() {
                        __debug_modinfo__ = %q
                }
diff --git a/src/cmd/go/testdata/script/mod_string_alias.txt b/src/cmd/go/testdata/script/mod_string_alias.txt
new file mode 100644 (file)
index 0000000..5c3d428
--- /dev/null
@@ -0,0 +1,14 @@
+[short] skip
+
+env GO111MODULE=on
+
+go mod init golang.org/issue/27584
+
+go build .
+
+-- main.go --
+package main
+
+type string = []int
+
+func main() {}