From b05983ff6d684561c8db6257ff816ef2e8114db5 Mon Sep 17 00:00:00 2001 From: Michael Sawyer Date: Fri, 6 Sep 2024 19:28:35 -0400 Subject: [PATCH] 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 --- ports/unix/modffi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/unix/modffi.c b/ports/unix/modffi.c index b3cd3380cc..51beb355fe 100644 --- a/ports/unix/modffi.c +++ b/ports/unix/modffi.c @@ -190,7 +190,7 @@ static mp_obj_t return_ffi_value(ffi_union_t *val, char type) { case 'h': case 'i': case 'l': - return mp_obj_new_int((signed)val->ffi); + return mp_obj_new_int((ffi_sarg)val->ffi); case 'B': case 'H': case 'I':