aboutsummaryrefslogtreecommitdiffhomepage
path: root/cgo
diff options
context:
space:
mode:
authorCrypt Keeper <[email protected]>2022-09-27 01:08:23 +0800
committerGitHub <[email protected]>2022-09-26 19:08:23 +0200
commit725864d8dceba0468b741e6037e74585e19832d8 (patch)
tree2d38a906c9768472dd6f683d6d3e8a1d8bb9144f /cgo
parentfca2de21b1555c8a238e40618fa0217e568eaf02 (diff)
downloadtinygo-725864d8dceba0468b741e6037e74585e19832d8.tar.gz
tinygo-725864d8dceba0468b741e6037e74585e19832d8.zip
cgo: fixes panic when FuncType.Results is nil (#3136)
* cgo: fixes panic when FuncType.Results is nil FuncType.Results can be nil. This fixes the comparison and backfills relevant tests. Signed-off-by: Adrian Cole <[email protected]> Co-authored-by: Ayke <[email protected]>
Diffstat (limited to 'cgo')
-rw-r--r--cgo/cgo.go3
-rw-r--r--cgo/cgo_test.go78
2 files changed, 81 insertions, 0 deletions
diff --git a/cgo/cgo.go b/cgo/cgo.go
index dfdd223ed..803af428e 100644
--- a/cgo/cgo.go
+++ b/cgo/cgo.go
@@ -948,6 +948,9 @@ func (p *cgoPackage) isEquivalentAST(a, b ast.Node) bool {
if !ok {
return false
}
+ if node == nil || b == nil {
+ return node == b
+ }
if len(node.List) != len(b.List) {
return false
}
diff --git a/cgo/cgo_test.go b/cgo/cgo_test.go
index f919972b2..e25da7a1d 100644
--- a/cgo/cgo_test.go
+++ b/cgo/cgo_test.go
@@ -115,6 +115,84 @@ func TestCGo(t *testing.T) {
}
}
+func Test_cgoPackage_isEquivalentAST(t *testing.T) {
+ fieldA := &ast.Field{Type: &ast.BasicLit{Kind: token.STRING, Value: "a"}}
+ fieldB := &ast.Field{Type: &ast.BasicLit{Kind: token.STRING, Value: "b"}}
+ listOfFieldA := &ast.FieldList{List: []*ast.Field{fieldA}}
+ listOfFieldB := &ast.FieldList{List: []*ast.Field{fieldB}}
+ funcDeclA := &ast.FuncDecl{Name: &ast.Ident{Name: "a"}, Type: &ast.FuncType{Params: &ast.FieldList{}, Results: listOfFieldA}}
+ funcDeclB := &ast.FuncDecl{Name: &ast.Ident{Name: "b"}, Type: &ast.FuncType{Params: &ast.FieldList{}, Results: listOfFieldB}}
+ funcDeclNoResults := &ast.FuncDecl{Name: &ast.Ident{Name: "C"}, Type: &ast.FuncType{Params: &ast.FieldList{}}}
+
+ testCases := []struct {
+ name string
+ a, b ast.Node
+ expected bool
+ }{
+ {
+ name: "both nil",
+ expected: true,
+ },
+ {
+ name: "not same type",
+ a: fieldA,
+ b: &ast.FuncDecl{},
+ expected: false,
+ },
+ {
+ name: "Field same",
+ a: fieldA,
+ b: fieldA,
+ expected: true,
+ },
+ {
+ name: "Field different",
+ a: fieldA,
+ b: fieldB,
+ expected: false,
+ },
+ {
+ name: "FuncDecl Type Results nil",
+ a: funcDeclNoResults,
+ b: funcDeclNoResults,
+ expected: true,
+ },
+ {
+ name: "FuncDecl Type Results same",
+ a: funcDeclA,
+ b: funcDeclA,
+ expected: true,
+ },
+ {
+ name: "FuncDecl Type Results different",
+ a: funcDeclA,
+ b: funcDeclB,
+ expected: false,
+ },
+ {
+ name: "FuncDecl Type Results a nil",
+ a: funcDeclNoResults,
+ b: funcDeclB,
+ expected: false,
+ },
+ {
+ name: "FuncDecl Type Results b nil",
+ a: funcDeclA,
+ b: funcDeclNoResults,
+ expected: false,
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ p := &cgoPackage{}
+ if got := p.isEquivalentAST(tc.a, tc.b); tc.expected != got {
+ t.Errorf("expected %v, got %v", tc.expected, got)
+ }
+ })
+ }
+}
+
// simpleImporter implements the types.Importer interface, but only allows
// importing the unsafe package.
type simpleImporter struct {