-
Notifications
You must be signed in to change notification settings - Fork 17.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/cgo: check function argument/return types for bad C pointer types
We need to determine whether arguments to and return values from C functions are "bad" typedef'd pointer types which need to be uintptr on the Go side. The type of those arguments are not specified explicitly. As a result, we never look through the C declarations for the GetTypeID functions associated with that type, and never realize that they are bad. However, in another function in the same package there might be an explicit reference. Then we end up with the declaration being uintptr in one file and *struct{...} in another file. Badness ensues. Fix this by doing a 2-pass algorithm. In the first pass, we run as normal, but record all the argument and result types we see. In the second pass, we include those argument types also when reading the C types. Fixes #24161 Change-Id: I8d727e73a2fbc88cb9d9899f8719ae405f59f753 Reviewed-on: https://go-review.googlesource.com/122575 Run-TryBot: Keith Randall <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
- Loading branch information
Showing
6 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2018 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package cgotest | ||
|
||
import ( | ||
"testing" | ||
|
||
"./issue24161arg" | ||
"./issue24161res" | ||
) | ||
|
||
func Test24161Arg(t *testing.T) { | ||
issue24161arg.Test(t) | ||
} | ||
func Test24161Res(t *testing.T) { | ||
issue24161res.Test(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2018 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build darwin | ||
|
||
package issue24161arg | ||
|
||
/* | ||
#cgo LDFLAGS: -framework CoreFoundation | ||
#include <CoreFoundation/CoreFoundation.h> | ||
*/ | ||
import "C" | ||
|
||
func test24161array() C.CFArrayRef { | ||
return C.CFArrayCreate(0, nil, 0, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2018 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build darwin | ||
|
||
package issue24161arg | ||
|
||
/* | ||
#cgo LDFLAGS: -framework CoreFoundation | ||
#include <CoreFoundation/CoreFoundation.h> | ||
*/ | ||
import "C" | ||
import "testing" | ||
|
||
func Test(t *testing.T) { | ||
a := test24161array() | ||
C.CFArrayCreateCopy(0, a) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2018 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build darwin | ||
|
||
package issue24161res | ||
|
||
/* | ||
#cgo LDFLAGS: -framework CoreFoundation | ||
#include <CoreFoundation/CoreFoundation.h> | ||
*/ | ||
import "C" | ||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test(t *testing.T) { | ||
if k := reflect.TypeOf(C.CFArrayCreate(0, nil, 0, nil)).Kind(); k != reflect.Uintptr { | ||
t.Fatalf("bad kind %s\n", k) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters