aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/cgo/internal
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2023-05-08 12:45:42 -0700
committerGopher Robot <gobot@golang.org>2023-05-22 18:34:47 +0000
commitc7aa48ecedf1db2bfad4f50ee2e21d8daa8aa692 (patch)
tree03cd5c10d29f57060f4800eb27f05d54152fd7c5 /src/cmd/cgo/internal
parent2f1e643229d19f40a5f80dc3784daaff83d5cc02 (diff)
downloadgo-c7aa48ecedf1db2bfad4f50ee2e21d8daa8aa692.tar.xz
cmd/cgo: recognize unsafe.{StringData,SliceData}
A simple call to unsafe.StringData can't contain any pointers. When looking for field references, a call to unsafe.StringData or unsafe.SliceData can be treated as a type conversion. In order to make unsafe.SliceData useful, recognize slice expressions when calling C functions. Fixes #59954 Change-Id: I08a3ace7882073284c1d46a5210582a2521b0b4e Reviewed-on: https://go-review.googlesource.com/c/go/+/493556 Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com>
Diffstat (limited to 'src/cmd/cgo/internal')
-rw-r--r--src/cmd/cgo/internal/testerrors/ptr_test.go24
1 files changed, 23 insertions, 1 deletions
diff --git a/src/cmd/cgo/internal/testerrors/ptr_test.go b/src/cmd/cgo/internal/testerrors/ptr_test.go
index eb923eaa5b..33126f40ae 100644
--- a/src/cmd/cgo/internal/testerrors/ptr_test.go
+++ b/src/cmd/cgo/internal/testerrors/ptr_test.go
@@ -444,6 +444,28 @@ var ptrTests = []ptrTest{
body: `s := &S40{p: new(int)}; C.f40((*C.struct_S40i)(&s.a))`,
fail: false,
},
+ {
+ // Test that we handle unsafe.StringData.
+ name: "stringdata",
+ c: `void f41(void* p) {}`,
+ imports: []string{"unsafe"},
+ body: `s := struct { a [4]byte; p *int }{p: new(int)}; str := unsafe.String(&s.a[0], 4); C.f41(unsafe.Pointer(unsafe.StringData(str)))`,
+ fail: false,
+ },
+ {
+ name: "slicedata",
+ c: `void f42(void* p) {}`,
+ imports: []string{"unsafe"},
+ body: `s := []*byte{nil, new(byte)}; C.f42(unsafe.Pointer(unsafe.SliceData(s)))`,
+ fail: true,
+ },
+ {
+ name: "slicedata2",
+ c: `void f43(void* p) {}`,
+ imports: []string{"unsafe"},
+ body: `s := struct { a [4]byte; p *int }{p: new(int)}; C.f43(unsafe.Pointer(unsafe.SliceData(s.a[:])))`,
+ fail: false,
+ },
}
func TestPointerChecks(t *testing.T) {
@@ -497,7 +519,7 @@ func buildPtrTests(t *testing.T, gopath string, cgocheck2 bool) (exe string) {
if err := os.MkdirAll(src, 0777); err != nil {
t.Fatal(err)
}
- if err := os.WriteFile(filepath.Join(src, "go.mod"), []byte("module ptrtest"), 0666); err != nil {
+ if err := os.WriteFile(filepath.Join(src, "go.mod"), []byte("module ptrtest\ngo 1.20"), 0666); err != nil {
t.Fatal(err)
}