]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: fix panic on references to non-existing C types
authorDidier Spezia <didier.06@gmail.com>
Sat, 3 Oct 2015 15:50:45 +0000 (15:50 +0000)
committerIan Lance Taylor <iant@golang.org>
Mon, 5 Oct 2015 22:37:07 +0000 (22:37 +0000)
cgo panics in Package.rewriteRef for:

var a = C.enum_test(1)
or
p := new(C.enum_test)

when the corresponding enum type is not defined.

Check nil values for Type fields and issue a proper
error instead.

Fixes #11097
Updates #12160

Change-Id: I5821d29097ef0a36076ec5273125b09846c7d832
Reviewed-on: https://go-review.googlesource.com/15264
Reviewed-by: Ian Lance Taylor <iant@golang.org>
misc/cgo/errors/issue11097a.go [new file with mode: 0644]
misc/cgo/errors/issue11097b.go [new file with mode: 0644]
misc/cgo/errors/test.bash
src/cmd/cgo/gcc.go

diff --git a/misc/cgo/errors/issue11097a.go b/misc/cgo/errors/issue11097a.go
new file mode 100644 (file)
index 0000000..4508213
--- /dev/null
@@ -0,0 +1,15 @@
+// Copyright 2015 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
+
+/*
+//enum test { foo, bar };
+*/
+import "C"
+
+func main() {
+       var a = C.enum_test(1) // ERROR HERE
+       _ = a
+}
diff --git a/misc/cgo/errors/issue11097b.go b/misc/cgo/errors/issue11097b.go
new file mode 100644 (file)
index 0000000..68c5c7c
--- /dev/null
@@ -0,0 +1,15 @@
+// Copyright 2015 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
+
+/*
+//enum test { foo, bar };
+*/
+import "C"
+
+func main() {
+       p := new(C.enum_test) // ERROR HERE
+       _ = p
+}
index c880ad65c279a3ed1d3c82ef18298f703513bfc5..25ab2499408e91f5d327969e1a7d78fd20279665 100755 (executable)
@@ -31,6 +31,8 @@ check err2.go
 check err3.go
 check issue7757.go
 check issue8442.go
+check issue11097a.go
+check issue11097b.go
 
 rm -rf errs _obj
 exit 0
index b64849a8d16f37f577cad456b40b8822f4371b6c..b65b6cb7a954de0d7360ed52cb3d6eebac34cc64 100644 (file)
@@ -607,6 +607,10 @@ func (p *Package) rewriteRef(f *File) {
                        if r.Name.Kind != "func" {
                                if r.Name.Kind == "type" {
                                        r.Context = "type"
+                                       if r.Name.Type == nil {
+                                               error_(r.Pos(), "invalid conversion to C.%s: undefined C type '%s'", fixGo(r.Name.Go), r.Name.C)
+                                               break
+                                       }
                                        expr = r.Name.Type.Go
                                        break
                                }
@@ -658,6 +662,10 @@ func (p *Package) rewriteRef(f *File) {
                                }
                        } else if r.Name.Kind == "type" {
                                // Okay - might be new(T)
+                               if r.Name.Type == nil {
+                                       error_(r.Pos(), "expression C.%s: undefined C type '%s'", fixGo(r.Name.Go), r.Name.C)
+                                       break
+                               }
                                expr = r.Name.Type.Go
                        } else if r.Name.Kind == "var" {
                                expr = &ast.StarExpr{Star: (*r.Expr).Pos(), X: expr}