detached a02394b

fix: address CodeRabbit findings — []string keepAlive + needsKeepAlive analysis

- Add reflect.Slice to needsKeepAlive check ([]string args need keepAlive)
- Keep individual CString allocations in keepAlive (prevent GC during C call)
- Fix variadic detection: use loop var v instead of args[i]
commit
a02394b1b2afe4553a50f6ed58d590b413491b3a
author
brice · · 2026-03-23 09:51 UTC
parent
d2ade5c
signature
GPG VerifiedCD88F9E564E0102646FB6077A1A07C7C7C3E349D · arch-gpg Verified for the committer email
1 file · +9 -6
M func.go +9 -6
@@ -224,11 +224,12 @@ func RegisterFunc(fptr any, cfn uintptr) {
224224 // and variadic []any args (unknown types at registration time).
225225 needsKeepAlive := false
226226 for i := 0; i < ty.NumIn(); i++ {
227- switch ty.In(i).Kind() {
228- case reflect.String, reflect.Struct, reflect.Array:
227+ inKind := ty.In(i).Kind()
228+ switch inKind {
229+ case reflect.String, reflect.Struct, reflect.Array, reflect.Slice:
230+ // Slice covers []string (CString allocs) and variadic ...any ([]any)
229231 needsKeepAlive = true
230232 case reflect.Interface:
231- // variadic []any — could contain anything
232233 needsKeepAlive = true
233234 }
234235 }
@@ -312,7 +313,7 @@ func RegisterFunc(fptr any, cfn uintptr) {
312313 }
313314 }
314315 for i, v := range args {
315- if variadic, ok := xreflect.TypeAssert[[]any](args[i]); ok {
316+ if variadic, ok := xreflect.TypeAssert[[]any](v); ok {
316317 if i != len(args)-1 {
317318 panic("purego: can only expand last parameter")
318319 }
@@ -342,7 +343,7 @@ func RegisterFunc(fptr any, cfn uintptr) {
342343 defer runtime.KeepAlive(args)
343344
344345 for i, v := range args {
345- if variadic, ok := xreflect.TypeAssert[[]any](args[i]); ok {
346+ if variadic, ok := xreflect.TypeAssert[[]any](v); ok {
346347 if i != len(args)-1 {
347348 panic("purego: can only expand last parameter")
348349 }
@@ -516,7 +517,9 @@ func addValue(v reflect.Value, keepAlive []any, addInt func(x uintptr), addFloat
516517 } else {
517518 res := make([]*byte, len(ss)+1)
518519 for i, s := range ss {
519- res[i] = strings.CString(s)
520+ ptr := strings.CString(s)
521+ keepAlive = append(keepAlive, ptr)
522+ res[i] = ptr
520523 }
521524 // NULL-terminated for C
522525 res[len(ss)] = nil
@@ -224,11 +224,12 @@ func RegisterFunc(fptr any, cfn uintptr) {
224 // and variadic []any args (unknown types at registration time).224 // and variadic []any args (unknown types at registration time).
225 needsKeepAlive := false225 needsKeepAlive := false
226 for i := 0; i < ty.NumIn(); i++ {226 for i := 0; i < ty.NumIn(); i++ {
227- switch ty.In(i).Kind() {227+ inKind := ty.In(i).Kind()
228- case reflect.String, reflect.Struct, reflect.Array:228+ switch inKind {
229+ case reflect.String, reflect.Struct, reflect.Array, reflect.Slice:
230+ // Slice covers []string (CString allocs) and variadic ...any ([]any)
229 needsKeepAlive = true231 needsKeepAlive = true
230 case reflect.Interface:232 case reflect.Interface:
231- // variadic []any — could contain anything
232 needsKeepAlive = true233 needsKeepAlive = true
233 }234 }
234 }235 }
@@ -312,7 +313,7 @@ func RegisterFunc(fptr any, cfn uintptr) {
312 }313 }
313 }314 }
314 for i, v := range args {315 for i, v := range args {
315- if variadic, ok := xreflect.TypeAssert[[]any](args[i]); ok {316+ if variadic, ok := xreflect.TypeAssert[[]any](v); ok {
316 if i != len(args)-1 {317 if i != len(args)-1 {
317 panic("purego: can only expand last parameter")318 panic("purego: can only expand last parameter")
318 }319 }
@@ -342,7 +343,7 @@ func RegisterFunc(fptr any, cfn uintptr) {
342 defer runtime.KeepAlive(args)343 defer runtime.KeepAlive(args)
343 344
344 for i, v := range args {345 for i, v := range args {
345- if variadic, ok := xreflect.TypeAssert[[]any](args[i]); ok {346+ if variadic, ok := xreflect.TypeAssert[[]any](v); ok {
346 if i != len(args)-1 {347 if i != len(args)-1 {
347 panic("purego: can only expand last parameter")348 panic("purego: can only expand last parameter")
348 }349 }
@@ -516,7 +517,9 @@ func addValue(v reflect.Value, keepAlive []any, addInt func(x uintptr), addFloat
516 } else {517 } else {
517 res := make([]*byte, len(ss)+1)518 res := make([]*byte, len(ss)+1)
518 for i, s := range ss {519 for i, s := range ss {
519- res[i] = strings.CString(s)520+ ptr := strings.CString(s)
521+ keepAlive = append(keepAlive, ptr)
522+ res[i] = ptr
520 }523 }
521 // NULL-terminated for C524 // NULL-terminated for C
522 res[len(ss)] = nil525 res[len(ss)] = nil

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