Skip to content

Commit 5879141

Browse files
Vicente Olivert Rierapfalcon
authored andcommitted
unix/modffi.c: cast first to intptr_t when casting from/to pointer
This fixes errors like these ones: modffi.c: In function 'return_ffi_value': modffi.c:143:29: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast] const char *s = (const char *)val; ^ modffi.c:162:20: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast] return (mp_obj_t)val; ^ modffi.c: In function 'ffifunc_call': modffi.c:358:25: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast] values[i] = (ffi_arg)a; ^ modffi.c:373:25: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast] values[i] = (ffi_arg)s; ^ modffi.c:381:25: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast] values[i] = (ffi_arg)bufinfo.buf; ^ modffi.c:384:25: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast] values[i] = (ffi_arg)p->func; ^ These errors can be highlighted when building micropython from MIPS64 n32 because ffi_arg is 64-bit wide and the pointers on MIPS64 n32 are 32-bit wide, so it's trying to case an integer to a pointer (or vice-versa) of a different size. We should cast first the pointer (or the integer) to a pointer sized integer (intptr_t) to fix that problem. Signed-off-by: Vicente Olivert Riera <Vincent.Riera@imgtec.com>
1 parent ed22e9b commit 5879141

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

unix/modffi.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <errno.h>
3131
#include <dlfcn.h>
3232
#include <ffi.h>
33+
#include <stdint.h>
3334

3435
#include "py/nlr.h"
3536
#include "py/runtime.h"
@@ -140,7 +141,7 @@ STATIC mp_obj_t return_ffi_value(ffi_arg val, char type)
140141
{
141142
switch (type) {
142143
case 's': {
143-
const char *s = (const char *)val;
144+
const char *s = (const char *)(intptr_t)val;
144145
if (!s) {
145146
return mp_const_none;
146147
}
@@ -159,7 +160,7 @@ STATIC mp_obj_t return_ffi_value(ffi_arg val, char type)
159160
}
160161
#endif
161162
case 'O':
162-
return (mp_obj_t)val;
163+
return (mp_obj_t)(intptr_t)val;
163164
default:
164165
return mp_obj_new_int(val);
165166
}
@@ -355,7 +356,7 @@ STATIC mp_obj_t ffifunc_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw,
355356
for (uint i = 0; i < n_args; i++, argtype++) {
356357
mp_obj_t a = args[i];
357358
if (*argtype == 'O') {
358-
values[i] = (ffi_arg)a;
359+
values[i] = (ffi_arg)(intptr_t)a;
359360
#if MICROPY_PY_BUILTINS_FLOAT
360361
} else if (*argtype == 'f') {
361362
float *p = (float*)&values[i];
@@ -370,18 +371,18 @@ STATIC mp_obj_t ffifunc_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw,
370371
values[i] = mp_obj_int_get_truncated(a);
371372
} else if (MP_OBJ_IS_STR(a)) {
372373
const char *s = mp_obj_str_get_str(a);
373-
values[i] = (ffi_arg)s;
374+
values[i] = (ffi_arg)(intptr_t)s;
374375
} else if (((mp_obj_base_t*)a)->type->buffer_p.get_buffer != NULL) {
375376
mp_obj_base_t *o = (mp_obj_base_t*)a;
376377
mp_buffer_info_t bufinfo;
377378
int ret = o->type->buffer_p.get_buffer(o, &bufinfo, MP_BUFFER_READ); // TODO: MP_BUFFER_READ?
378379
if (ret != 0) {
379380
goto error;
380381
}
381-
values[i] = (ffi_arg)bufinfo.buf;
382+
values[i] = (ffi_arg)(intptr_t)bufinfo.buf;
382383
} else if (MP_OBJ_IS_TYPE(a, &fficallback_type)) {
383384
mp_obj_fficallback_t *p = a;
384-
values[i] = (ffi_arg)p->func;
385+
values[i] = (ffi_arg)(intptr_t)p->func;
385386
} else {
386387
goto error;
387388
}

0 commit comments

Comments
 (0)