[go] cmd/go: compiler accepts invalid declaration of methods on aliases to C types

28 views
Skip to first unread message

xie cui (Gerrit)

unread,
Jun 14, 2023, 1:20:34 PM6/14/23
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

xie cui has uploaded this change for review.

View Change

cmd/go: compiler accepts invalid declaration of methods on aliases to C types

Fixes #60725

Change-Id: Iae49d04e4c432f28f6c6a011237a1333cfab6c8b
---
M src/cmd/go/internal/work/gc.go
M src/cmd/go/testdata/script/cgo_badmethod_issue57926.txt
A src/cmd/go/testdata/script/cgo_badmethod_issue60725.txt
3 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/src/cmd/go/internal/work/gc.go b/src/cmd/go/internal/work/gc.go
index 6043ad5..119321d 100644
--- a/src/cmd/go/internal/work/gc.go
+++ b/src/cmd/go/internal/work/gc.go
@@ -177,6 +177,11 @@
}

args := []any{cfg.BuildToolexec, base.Tool("compile"), "-o", ofile, "-trimpath", a.trimpath(), defaultGcFlags, gcflags}
+
+ if len(p.CgoFiles) > 0 {
+ cgoIsLocalFile := "cgo.islocal"
+ args = append(args, "-islocaljsonfile="+objdir+cgoIsLocalFile)
+ }
if p.Internal.LocalPrefix == "" {
args = append(args, "-nolocalimports")
} else {
diff --git a/src/cmd/go/testdata/script/cgo_badmethod_issue57926.txt b/src/cmd/go/testdata/script/cgo_badmethod_issue57926.txt
index 81ef850..b6abff0 100644
--- a/src/cmd/go/testdata/script/cgo_badmethod_issue57926.txt
+++ b/src/cmd/go/testdata/script/cgo_badmethod_issue57926.txt
@@ -7,7 +7,6 @@
! go build
stderr 'cannot define new methods on non-local type C.T'
stderr 'cannot define new methods on non-local type \*C.T'
-! stderr 'Alias'

-- go.mod --
module example.com
@@ -23,9 +22,3 @@

func (C.T) f() {}
func (recv *C.T) g() {}
-
-// The check is more education than enforcement,
-// and is easily defeated using a type alias.
-type Alias = C.T
-func (Alias) h() {}
-func (*Alias) i() {}
diff --git a/src/cmd/go/testdata/script/cgo_badmethod_issue60725.txt b/src/cmd/go/testdata/script/cgo_badmethod_issue60725.txt
new file mode 100644
index 0000000..5c77315
--- /dev/null
+++ b/src/cmd/go/testdata/script/cgo_badmethod_issue60725.txt
@@ -0,0 +1,41 @@
+[short] skip
+[!cgo] skip
+
+# Test that cgo rejects attempts to declare methods
+
+! go build ./a
+stderr 'cannot define new methods on non-local type Alias'
+stderr 'cannot define new methods on non-local type \*Alias'
+
+
+! go build ./b
+stderr 'cannot define new methods on non-local type foo'
+-- go.mod --
+module example.com
+go 1.12
+
+-- a/a.go --
+package a
+
+/*
+typedef int T;
+*/
+import "C"
+
+type Alias = C.T
+func (Alias) h() {}
+func (*Alias) i() {}
+
+-- b/b1.go --
+package b
+
+/*
+typedef int T;
+*/
+import "C"
+type foo = C.T
+
+-- b/b2.go --
+package b
+
+func (foo) h() {}
\ No newline at end of file

To view, visit change 503397. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Iae49d04e4c432f28f6c6a011237a1333cfab6c8b
Gerrit-Change-Number: 503397
Gerrit-PatchSet: 1
Gerrit-Owner: xie cui <5235...@qq.com>

xie cui (Gerrit)

unread,
Jun 15, 2023, 1:13:01 AM6/15/23
to goph...@pubsubhelper.golang.org, Michael Matloob, Bryan Mills, Russ Cox, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com

Attention is currently required from: Bryan Mills, Michael Matloob.

View Change

1 comment:

To view, visit change 503397. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Iae49d04e4c432f28f6c6a011237a1333cfab6c8b
Gerrit-Change-Number: 503397
Gerrit-PatchSet: 1
Gerrit-Owner: xie cui <5235...@qq.com>
Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: Russ Cox <r...@golang.org>
Gerrit-Attention: Bryan Mills <bcm...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Thu, 15 Jun 2023 05:12:58 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No

xie cui (Gerrit)

unread,
Jun 15, 2023, 1:13:59 AM6/15/23
to goph...@pubsubhelper.golang.org, Michael Matloob, Bryan Mills, Russ Cox, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com

xie cui abandoned this change.

View Change

Abandoned

To view, visit change 503397. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: abandon

xie cui (Gerrit)

unread,
Jun 16, 2023, 9:45:51 PM6/16/23
to goph...@pubsubhelper.golang.org, Michael Matloob, Bryan Mills, Russ Cox, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com

xie cui restored this change.

View Change

To view, visit change 503397. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: restore

xie cui (Gerrit)

unread,
Jun 20, 2023, 8:13:04 PM6/20/23
to goph...@pubsubhelper.golang.org, Michael Matloob, Bryan Mills, Russ Cox, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com

xie cui abandoned this change.

View Change

Abandoned

To view, visit change 503397. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: abandon
Reply all
Reply to author
Forward
0 new messages