]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: make C function pointers non-assignable
authorMatthew Dempsky <mdempsky@google.com>
Tue, 12 Aug 2014 19:55:12 +0000 (12:55 -0700)
committerIan Lance Taylor <iant@golang.org>
Tue, 12 Aug 2014 19:55:12 +0000 (12:55 -0700)
Fixes #7757.
Fixes #8488.

LGTM=iant
R=iant
CC=golang-codereviews
https://golang.org/cl/118690044

misc/cgo/errors/issue7757.go [new file with mode: 0644]
misc/cgo/errors/test.bash
src/cmd/cgo/gcc.go
src/cmd/cgo/out.go

diff --git a/misc/cgo/errors/issue7757.go b/misc/cgo/errors/issue7757.go
new file mode 100644 (file)
index 0000000..5eafd22
--- /dev/null
@@ -0,0 +1,14 @@
+// Copyright 2014 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+/*
+void foo() {}
+*/
+import "C"
+
+func main() {
+       C.foo = C.foo // ERROR HERE
+}
index f0f60c84458b39907b7521301b8edc456e25789e..e5bf47a0dd40af1553930e712f6047b67143c698 100755 (executable)
@@ -27,6 +27,7 @@ check() {
 check err1.go
 check err2.go
 check err3.go
+check issue7757.go
 
 rm -rf errs _obj
 exit 0
index b79725ab01a1bf0b36ed8b93b0074175e36317d9..26def654d0850b0f3a7b38d621755af2642ac679 100644 (file)
@@ -650,7 +650,13 @@ func (p *Package) rewriteRef(f *File) {
                                        f.Name[fpName] = name
                                }
                                r.Name = name
-                               expr = ast.NewIdent(name.Mangle)
+                               // Rewrite into call to _Cgo_ptr to prevent assignments.  The _Cgo_ptr
+                               // function is defined in out.go and simply returns its argument. See
+                               // issue 7757.
+                               expr = &ast.CallExpr{
+                                       Fun:  &ast.Ident{NamePos: (*r.Expr).Pos(), Name: "_Cgo_ptr"},
+                                       Args: []ast.Expr{ast.NewIdent(name.Mangle)},
+                               }
                        } else if r.Name.Kind == "type" {
                                // Okay - might be new(T)
                                expr = r.Name.Type.Go
index 1ef78b757c21f0fb2617b97964caa73d64051533..6322e0604a4aadf654df2e629c30d5277de07b36 100644 (file)
@@ -64,7 +64,7 @@ func (p *Package) writeDefs() {
        if !*gccgo && *importRuntimeCgo {
                fmt.Fprintf(fgo2, "import _ \"runtime/cgo\"\n\n")
        }
-       fmt.Fprintf(fgo2, "type _ unsafe.Pointer\n\n")
+       fmt.Fprintf(fgo2, "func _Cgo_ptr(ptr unsafe.Pointer) unsafe.Pointer { return ptr }\n\n")
        if *importSyscall {
                fmt.Fprintf(fgo2, "func _Cerrno(dst *error, x int32) { *dst = syscall.Errno(x) }\n")
        }