detached c3a82d7

purego: check for nil callback (#126)

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.
commit
c3a82d74994b67409e9fd97be97c819977b894e9
author
TotallyGamerJet · · 2023-04-10 13:44 UTC
committer
GitHub · · 2023-04-10 13:44 UTC
parent
e145907
signature
GPG Unverified4AEE18F83AFDEB23 The key is unknown
1 file · +9 -4
M syscall_sysv.go +9 -4
@@ -22,9 +22,11 @@ type syscall9Args struct {
2222
2323 //go:nosplit
2424 func syscall_syscall9X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2, err uintptr) {
25- args := syscall9Args{fn, a1, a2, a3, a4, a5, a6, a7, a8, a9,
25+ args := syscall9Args{
26+ fn, a1, a2, a3, a4, a5, a6, a7, a8, a9,
2627 a1, a2, a3, a4, a5, a6, a7, a8,
27- r1, r2, err}
28+ r1, r2, err,
29+ }
2830 runtime_cgocall(syscall9XABI0, unsafe.Pointer(&args))
2931 return args.r1, args.r2, args.err
3032 }
@@ -73,7 +75,10 @@ type callbackArgs struct {
7375 func compileCallback(fn interface{}) uintptr {
7476 val := reflect.ValueOf(fn)
7577 if val.Kind() != reflect.Func {
76- panic("purego: type is not a function")
78+ panic("purego: the type must be a function but was not")
79+ }
80+ if val.IsNil() {
81+ panic("purego: function must not be nil")
7782 }
7883 ty := val.Type()
7984 for i := 0; i < ty.NumIn(); i++ {
@@ -134,7 +139,7 @@ func callbackWrap(a *callbackArgs) {
134139 var intsN int // intsN represents the number of integer arguments processed
135140 // stack points to the index into frame of the current stack element.
136141 // The stack begins after the float and integer registers.
137- var stack = numOfIntegerRegisters() + numOfFloats
142+ stack := numOfIntegerRegisters() + numOfFloats
138143 for i := range args {
139144 var pos int
140145 switch fnType.In(i).Kind() {
@@ -22,9 +22,11 @@ type syscall9Args struct {
22 22
23 //go:nosplit23 //go:nosplit
24 func syscall_syscall9X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2, err uintptr) {24 func syscall_syscall9X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2, err uintptr) {
25- args := syscall9Args{fn, a1, a2, a3, a4, a5, a6, a7, a8, a9,25+ args := syscall9Args{
26+ fn, a1, a2, a3, a4, a5, a6, a7, a8, a9,
26 a1, a2, a3, a4, a5, a6, a7, a8,27 a1, a2, a3, a4, a5, a6, a7, a8,
27- r1, r2, err}28+ r1, r2, err,
29+ }
28 runtime_cgocall(syscall9XABI0, unsafe.Pointer(&args))30 runtime_cgocall(syscall9XABI0, unsafe.Pointer(&args))
29 return args.r1, args.r2, args.err31 return args.r1, args.r2, args.err
30 }32 }
@@ -73,7 +75,10 @@ type callbackArgs struct {
73 func compileCallback(fn interface{}) uintptr {75 func compileCallback(fn interface{}) uintptr {
74 val := reflect.ValueOf(fn)76 val := reflect.ValueOf(fn)
75 if val.Kind() != reflect.Func {77 if val.Kind() != reflect.Func {
76- panic("purego: type is not a function")78+ panic("purego: the type must be a function but was not")
79+ }
80+ if val.IsNil() {
81+ panic("purego: function must not be nil")
77 }82 }
78 ty := val.Type()83 ty := val.Type()
79 for i := 0; i < ty.NumIn(); i++ {84 for i := 0; i < ty.NumIn(); i++ {
@@ -134,7 +139,7 @@ func callbackWrap(a *callbackArgs) {
134 var intsN int // intsN represents the number of integer arguments processed139 var intsN int // intsN represents the number of integer arguments processed
135 // stack points to the index into frame of the current stack element.140 // stack points to the index into frame of the current stack element.
136 // The stack begins after the float and integer registers.141 // The stack begins after the float and integer registers.
137- var stack = numOfIntegerRegisters() + numOfFloats142+ stack := numOfIntegerRegisters() + numOfFloats
138 for i := range args {143 for i := range args {
139 var pos int144 var pos int
140 switch fnType.In(i).Kind() {145 switch fnType.In(i).Kind() {

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 102

↑↓ moveenter openesc close