From a3ff9557c6dfa6477d979028e716b331ad6ff2d7 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 13 Feb 2025 18:32:48 +0000 Subject: ffi: big endian: broken int8 and uint8 return values. * ffi.c (ffi_i8_rput, ffi_i8_rget, ffi_u8_rput, ffi_u8_rget): These functions are not doing the correct job; they are just casting the pointer to the target type, like on little endian. The big endian rget must fetch the entire 64 bit word (ffi_arg) and convert its value to the target type. If it's a character value, the actual bits are found at *(src + 7) not at *src. The rput function must do the reverse; convert the value to the 64 bit ffi_arg and store that. --- ffi.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ffi.c b/ffi.c index 9d72a61e..d5769e21 100644 --- a/ffi.c +++ b/ffi.c @@ -1885,14 +1885,15 @@ static void ffi_i8_rput(struct txr_ffi_type *tft, val n, mem_t *dst, val self) { i8_t v = c_i8(n, self); (void) tft; - *coerce(i8_t *, dst) = v; + *coerce(ffi_arg *, dst) = v; } static val ffi_i8_rget(struct txr_ffi_type *tft, mem_t *src, val self) { + i8_t n = *coerce(ffi_arg *, src); (void) tft; (void) self; - return num_fast(*src); + return num_fast(n); } static void ffi_u8_rput(struct txr_ffi_type *tft, val n, mem_t *dst, val self) @@ -1900,14 +1901,15 @@ static void ffi_u8_rput(struct txr_ffi_type *tft, val n, mem_t *dst, val self) u8_t v = c_u8(n, self); (void) tft; (void) self; - *coerce(u8_t *, dst) = v; + *coerce(ffi_arg *, dst) = v; } static val ffi_u8_rget(struct txr_ffi_type *tft, mem_t *src, val self) { + u8_t n = *coerce(ffi_arg *, src); (void) tft; (void) self; - return num_fast(*coerce(u8_t *, src)); + return num_fast(n); } #if HAVE_I16 -- cgit v1.2.3