Skip to content

Calling a C function that returns a function pointer panics #501

Description

@NotAFlightRisk

PureGo Version

v0.10.2, and main as of today

Operating System

  • Windows
  • macOS
  • Linux
  • FreeBSD
  • NetBSD
  • Android
  • iOS

Go Version (go version)

go1.26.5 linux/amd64

What steps will reproduce the problem?

Declare a C function that returns a function pointer, so the Go side has a func return type:

var fn func() func(a, b int32) int32
purego.RegisterLibFunc(&fn, lib, "return_func_ptr")
add := fn()

Registering it is fine, its the call that goes, and it goes for every signature I tried.

What is the expected result?

fn() hands back a callable func(int32, int32) int32.

What happens instead?

panic: reflect.MakeFunc: value of type *func(int32, int32) int32 is not assignable to type func(int32, int32) int32

Anything else you feel useful to add?

The RegisterFunc type table has func <=> C function in it, but that only seems to hold in the argument direction. Every other case in that return switch leaves the result as the declared type - the reflect.Func case swaps in a *func, registers into that, and hands the pointer back to MakeFunc, which wont take it.

Been that way since #49 in 2022 as far as I can tell. Fix and a test are in #500.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions