Skip to content

Commit 837984f

Browse files
cmd/cgo: use function arg type for _cgoCheckPointerN function
When cgo writes a _cgoCheckPointerN function to handle unsafe.Pointer, use the function's argument type rather than interface{}. This permits type errors to be detected at build time rather than run time. Fixes #13830. Change-Id: Ic7090905e16b977e2379670e0f83640dc192b565 Reviewed-on: https://go-review.googlesource.com/23675 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent 894803c commit 837984f

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

misc/cgo/errors/issue13830.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// cgo converts C void* to Go unsafe.Pointer, so despite appearances C
6+
// void** is Go *unsafe.Pointer. This test verifies that we detect the
7+
// problem at build time.
8+
9+
package main
10+
11+
// typedef void v;
12+
// void F(v** p) {}
13+
import "C"
14+
15+
import "unsafe"
16+
17+
type v [0]byte
18+
19+
func f(p **v) {
20+
C.F((**C.v)(unsafe.Pointer(p))) // ERROR HERE
21+
}
22+
23+
func main() {
24+
var p *v
25+
f(&p)
26+
}

misc/cgo/errors/test.bash

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ expect() {
1818
file=$1
1919
shift
2020
if go build $file >errs 2>&1; then
21-
echo 1>&2 misc/cgo/errors/test.bash: BUG: expected cgo to fail but it succeeded
21+
echo 1>&2 misc/cgo/errors/test.bash: BUG: expected cgo to fail on $file but it succeeded
2222
exit 1
2323
fi
2424
if ! test -s errs; then
25-
echo 1>&2 misc/cgo/errors/test.bash: BUG: expected error output but saw none
25+
echo 1>&2 misc/cgo/errors/test.bash: BUG: expected error output for $file but saw none
2626
exit 1
2727
fi
2828
for error; do
2929
if ! fgrep $error errs >/dev/null 2>&1; then
30-
echo 1>&2 misc/cgo/errors/test.bash: BUG: expected error output to contain \"$error\" but saw:
30+
echo 1>&2 misc/cgo/errors/test.bash: BUG: expected error output for $file to contain \"$error\" but saw:
3131
cat 1>&2 errs
3232
exit 1
3333
fi
@@ -44,6 +44,7 @@ check issue11097b.go
4444
expect issue13129.go C.ushort
4545
check issue13423.go
4646
expect issue13635.go C.uchar C.schar C.ushort C.uint C.ulong C.longlong C.ulonglong C.complexfloat C.complexdouble
47+
check issue13830.go
4748

4849
if ! go build issue14669.go; then
4950
exit 1

src/cmd/cgo/out.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (p *Package) writeDefs() {
113113

114114
for i, t := range p.CgoChecks {
115115
n := p.unsafeCheckPointerNameIndex(i)
116-
fmt.Fprintf(fgo2, "\nfunc %s(p interface{}, args ...interface{}) %s {\n", n, t)
116+
fmt.Fprintf(fgo2, "\nfunc %s(p %s, args ...interface{}) %s {\n", n, t, t)
117117
fmt.Fprintf(fgo2, "\treturn _cgoCheckPointer(p, args...).(%s)\n", t)
118118
fmt.Fprintf(fgo2, "}\n")
119119
}

0 commit comments

Comments
 (0)