mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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>
This commit is contained in:
parent
2f1e643229
commit
c7aa48eced
2 changed files with 124 additions and 4 deletions
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue