Skip to content

Commit

Permalink
runtime: support cgo index into pointer-to-array
Browse files Browse the repository at this point in the history
We were missing a case for calling a C function with an index
into a pointer-to-array.

Fixes #70016

Change-Id: I9c74d629e58722813c1aaa0f0dc225a5a64d111b
Reviewed-on: https://go-review.googlesource.com/c/go/+/621576
Reviewed-by: Keith Randall <[email protected]>
Reviewed-by: Michael Knyszek <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Auto-Submit: Ian Lance Taylor <[email protected]>
Reviewed-by: Keith Randall <[email protected]>
  • Loading branch information
ianlancetaylor authored and gopherbot committed Oct 24, 2024
1 parent 4646556 commit 76f3208
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/cmd/cgo/internal/testerrors/ptr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,23 @@ var ptrTests = []ptrTest{
body: `s := struct { a [4]byte; p *int }{p: new(int)}; C.f43(unsafe.Pointer(unsafe.SliceData(s.a[:])))`,
fail: false,
},
{
// Passing the address of an element of a pointer-to-array.
name: "arraypointer",
c: `void f44(void* p) {}`,
imports: []string{"unsafe"},
body: `a := new([10]byte); C.f44(unsafe.Pointer(&a[0]))`,
fail: false,
},
{
// Passing the address of an element of a pointer-to-array
// that contains a Go pointer.
name: "arraypointer2",
c: `void f45(void** p) {}`,
imports: []string{"unsafe"},
body: `i := 0; a := &[2]unsafe.Pointer{nil, unsafe.Pointer(&i)}; C.f45(&a[0])`,
fail: true,
},
}

func TestPointerChecks(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions src/runtime/cgocall.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,17 @@ func cgoCheckPointer(ptr any, arg any) {
ep = aep
t = ep._type
top = false
case abi.Pointer:
// The Go code is indexing into a pointer to an array,
// and we have been passed the pointer-to-array.
// Check the array rather than the pointer.
pt := (*abi.PtrType)(unsafe.Pointer(aep._type))
t = pt.Elem
if t.Kind_&abi.KindMask != abi.Array {
throw("can't happen")
}
ep = aep
top = false
default:
throw("can't happen")
}
Expand Down

0 comments on commit 76f3208

Please sign in to comment.