Skip to content

Commit

Permalink
purego: check for nil callback (#126)
Browse files Browse the repository at this point in the history
It was possible to create a C callback for a nil function but this shouldn't be possible. This commit makes it so that NewCallback will panic on a nil function.
  • Loading branch information
TotallyGamerJet authored Apr 10, 2023
1 parent e145907 commit c3a82d7
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions syscall_sysv.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ type syscall9Args struct {

//go:nosplit
func syscall_syscall9X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2, err uintptr) {
args := syscall9Args{fn, a1, a2, a3, a4, a5, a6, a7, a8, a9,
args := syscall9Args{
fn, a1, a2, a3, a4, a5, a6, a7, a8, a9,
a1, a2, a3, a4, a5, a6, a7, a8,
r1, r2, err}
r1, r2, err,
}
runtime_cgocall(syscall9XABI0, unsafe.Pointer(&args))
return args.r1, args.r2, args.err
}
Expand Down Expand Up @@ -73,7 +75,10 @@ type callbackArgs struct {
func compileCallback(fn interface{}) uintptr {
val := reflect.ValueOf(fn)
if val.Kind() != reflect.Func {
panic("purego: type is not a function")
panic("purego: the type must be a function but was not")
}
if val.IsNil() {
panic("purego: function must not be nil")
}
ty := val.Type()
for i := 0; i < ty.NumIn(); i++ {
Expand Down Expand Up @@ -134,7 +139,7 @@ func callbackWrap(a *callbackArgs) {
var intsN int // intsN represents the number of integer arguments processed
// stack points to the index into frame of the current stack element.
// The stack begins after the float and integer registers.
var stack = numOfIntegerRegisters() + numOfFloats
stack := numOfIntegerRegisters() + numOfFloats
for i := range args {
var pos int
switch fnType.In(i).Kind() {
Expand Down

0 comments on commit c3a82d7

Please sign in to comment.