File tree 2 files changed +28
-0
lines changed
cmd/cgo/internal/testerrors 2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -472,6 +472,23 @@ var ptrTests = []ptrTest{
472
472
body : `s := struct { a [4]byte; p *int }{p: new(int)}; C.f43(unsafe.Pointer(unsafe.SliceData(s.a[:])))` ,
473
473
fail : false ,
474
474
},
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
+ },
475
492
}
476
493
477
494
func TestPointerChecks (t * testing.T ) {
Original file line number Diff line number Diff line change @@ -563,6 +563,17 @@ func cgoCheckPointer(ptr any, arg any) {
563
563
ep = aep
564
564
t = ep ._type
565
565
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
566
577
default :
567
578
throw ("can't happen" )
568
579
}
You can’t perform that action at this time.
0 commit comments