Skip to content

Commit 76f3208

Browse files
ianlancetaylorgopherbot
authored andcommitted
runtime: support cgo index into pointer-to-array
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]>
1 parent 4646556 commit 76f3208

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/cmd/cgo/internal/testerrors/ptr_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,23 @@ var ptrTests = []ptrTest{
472472
body: `s := struct { a [4]byte; p *int }{p: new(int)}; C.f43(unsafe.Pointer(unsafe.SliceData(s.a[:])))`,
473473
fail: false,
474474
},
475+
{
476+
// Passing the address of an element of a pointer-to-array.
477+
name: "arraypointer",
478+
c: `void f44(void* p) {}`,
479+
imports: []string{"unsafe"},
480+
body: `a := new([10]byte); C.f44(unsafe.Pointer(&a[0]))`,
481+
fail: false,
482+
},
483+
{
484+
// Passing the address of an element of a pointer-to-array
485+
// that contains a Go pointer.
486+
name: "arraypointer2",
487+
c: `void f45(void** p) {}`,
488+
imports: []string{"unsafe"},
489+
body: `i := 0; a := &[2]unsafe.Pointer{nil, unsafe.Pointer(&i)}; C.f45(&a[0])`,
490+
fail: true,
491+
},
475492
}
476493

477494
func TestPointerChecks(t *testing.T) {

src/runtime/cgocall.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,17 @@ func cgoCheckPointer(ptr any, arg any) {
563563
ep = aep
564564
t = ep._type
565565
top = false
566+
case abi.Pointer:
567+
// The Go code is indexing into a pointer to an array,
568+
// and we have been passed the pointer-to-array.
569+
// Check the array rather than the pointer.
570+
pt := (*abi.PtrType)(unsafe.Pointer(aep._type))
571+
t = pt.Elem
572+
if t.Kind_&abi.KindMask != abi.Array {
573+
throw("can't happen")
574+
}
575+
ep = aep
576+
top = false
566577
default:
567578
throw("can't happen")
568579
}

0 commit comments

Comments
 (0)