fix(cgo): support byte slices in Call argument conversion

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 19:36:57 +00:00
parent 375d522b1a
commit 64b4003bb4
3 changed files with 27 additions and 5 deletions

View file

@ -186,6 +186,15 @@ func TestCallSupportsByteSliceArgument(t *testing.T) {
}
}
func TestCallSupportsEmptyByteSliceArgument(t *testing.T) {
t.Parallel()
payload := []byte{}
if err := Call(callZeroByteSliceArgumentFunction(), payload); err != nil {
t.Fatalf("expected success for empty byte slice, got %v", err)
}
}
func TestErrnoMapping(t *testing.T) {
t.Parallel()

View file

@ -75,6 +75,10 @@ int call_buffer_argument(uintptr_t value) {
return value == 0 ? 13 : 0;
}
int call_zero_byte_slice_argument(uintptr_t value) {
return value == 0 ? 0 : 13;
}
int call_c_types_arguments(long a0, unsigned long a1, long long a2, unsigned long long a3) {
if (a0 == 10 && a1 == 11 && a2 == 12 && a3 == 13) {
return 0;
@ -130,6 +134,10 @@ uintptr_t call_buffer_argument_ptr(void) {
return (uintptr_t)&call_buffer_argument;
}
uintptr_t call_zero_byte_slice_argument_ptr(void) {
return (uintptr_t)&call_zero_byte_slice_argument;
}
uintptr_t call_c_types_arguments_ptr(void) {
return (uintptr_t)&call_c_types_arguments;
}
@ -198,6 +206,11 @@ func callBufferArgumentFunction() unsafe.Pointer {
return *(*unsafe.Pointer)(unsafe.Pointer(&function))
}
func callZeroByteSliceArgumentFunction() unsafe.Pointer {
function := C.call_zero_byte_slice_argument_ptr()
return *(*unsafe.Pointer)(unsafe.Pointer(&function))
}
func callCTypeArgumentFunction() unsafe.Pointer {
function := C.call_c_types_arguments_ptr()
return *(*unsafe.Pointer)(unsafe.Pointer(&function))

View file

@ -805,6 +805,11 @@ func toSyscallArg(value interface{}) (uintptr, bool) {
return uintptr(typed), true
case uint64:
return uintptr(typed), true
case []byte:
if len(typed) == 0 {
return 0, true
}
return uintptr(unsafe.Pointer(&typed[0])), true
default:
reflected := reflect.ValueOf(value)
switch reflected.Kind() {
@ -819,11 +824,6 @@ func toSyscallArg(value interface{}) (uintptr, bool) {
return 0, true
}
return 0, false
case []byte:
if len(typed) == 0 {
return 0, true
}
return uintptr(unsafe.Pointer(&typed[0])), true
}
}