unix/modffi: Fix signed integer cast in return_ffi_value.

Casting an ffi_arg to a signed int may truncate the value. E.g., when the
ffi_arg is 64-bit and the signed int is 32-bit. Also, casting an ffi_arg
to a larger signed type will not sign extend the value. E.g., when the
ffi_arg is 32-bit and the larger signed type is int64_t. If the value is
signed, it should be cast to ffi_sarg, which is the same size as ffi_arg.

Signed-off-by: Michael Sawyer <mjfsawyer@gmail.com>
This commit is contained in:
Michael Sawyer
2024-09-06 19:28:35 -04:00
committed by Damien George
parent 8b35f2c7fa
commit b05983ff6d

View File

@@ -190,7 +190,7 @@ static mp_obj_t return_ffi_value(ffi_union_t *val, char type) {
case 'h': case 'h':
case 'i': case 'i':
case 'l': case 'l':
return mp_obj_new_int((signed)val->ffi); return mp_obj_new_int((ffi_sarg)val->ffi);
case 'B': case 'B':
case 'H': case 'H':
case 'I': case 'I':