From a2110bd3fca59df8b16a2b5fe4645a4af30b06ed Mon Sep 17 00:00:00 2001 From: David Lechner Date: Fri, 20 Mar 2020 00:10:22 -0500 Subject: [PATCH] all: Fix implicit casts of float/double, and signed comparison. These were found by buiding the unix coverage variant on macOS (so clang compiler). Mostly, these are fixing implicit cast of float/double to mp_float_t which is one of those two and one mp_int_t to size_t fix for good measure. --- extmod/moductypes.c | 4 ++-- ports/unix/modffi.c | 4 ++-- ports/unix/modtime.c | 4 ++-- py/binary.c | 8 ++++---- py/objarray.c | 2 +- py/parsenum.c | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/extmod/moductypes.c b/extmod/moductypes.c index 5446f7b8f8..c846747d50 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -360,9 +360,9 @@ STATIC mp_obj_t get_aligned(uint val_type, void *p, mp_int_t index) { return mp_obj_new_int_from_ll(((int64_t *)p)[index]); #if MICROPY_PY_BUILTINS_FLOAT case FLOAT32: - return mp_obj_new_float(((float *)p)[index]); + return mp_obj_new_float((mp_float_t)((float *)p)[index]); case FLOAT64: - return mp_obj_new_float(((double *)p)[index]); + return mp_obj_new_float((mp_float_t)((double *)p)[index]); #endif default: assert(0); diff --git a/ports/unix/modffi.c b/ports/unix/modffi.c index 1cc10bdd49..dbebec8606 100644 --- a/ports/unix/modffi.c +++ b/ports/unix/modffi.c @@ -167,11 +167,11 @@ STATIC mp_obj_t return_ffi_value(ffi_arg val, char type) { union { ffi_arg ffi; float flt; } val_union = { .ffi = val }; - return mp_obj_new_float(val_union.flt); + return mp_obj_new_float((mp_float_t)val_union.flt); } case 'd': { double *p = (double *)&val; - return mp_obj_new_float(*p); + return mp_obj_new_float((mp_float_t)*p); } #endif case 'O': diff --git a/ports/unix/modtime.c b/ports/unix/modtime.c index aeeece546a..fc9aa50828 100644 --- a/ports/unix/modtime.c +++ b/ports/unix/modtime.c @@ -61,7 +61,7 @@ static inline int msec_sleep_tv(struct timeval *tv) { #endif #if defined(MP_CLOCKS_PER_SEC) -#define CLOCK_DIV (MP_CLOCKS_PER_SEC / 1000.0F) +#define CLOCK_DIV (MP_CLOCKS_PER_SEC / MICROPY_FLOAT_CONST(1000.0)) #else #error Unsupported clock() implementation #endif @@ -84,7 +84,7 @@ STATIC mp_obj_t mod_time_clock(void) { // float cannot represent full range of int32 precisely, so we pre-divide // int to reduce resolution, and then actually do float division hoping // to preserve integer part resolution. - return mp_obj_new_float((float)(clock() / 1000) / CLOCK_DIV); + return mp_obj_new_float((mp_float_t)(clock() / 1000) / CLOCK_DIV); #else return mp_obj_new_int((mp_int_t)clock()); #endif diff --git a/py/binary.c b/py/binary.c index faa413585e..d4898c1436 100644 --- a/py/binary.c +++ b/py/binary.c @@ -176,9 +176,9 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, size_t index) { #endif #if MICROPY_PY_BUILTINS_FLOAT case 'f': - return mp_obj_new_float(((float *)p)[index]); + return mp_obj_new_float((mp_float_t)((float *)p)[index]); case 'd': - return mp_obj_new_float(((double *)p)[index]); + return mp_obj_new_float((mp_float_t)((double *)p)[index]); #endif // Extension to CPython: array of objects case 'O': @@ -244,12 +244,12 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte *p_base, byte * union { uint32_t i; float f; } fpu = {val}; - return mp_obj_new_float(fpu.f); + return mp_obj_new_float((mp_float_t)fpu.f); } else if (val_type == 'd') { union { uint64_t i; double f; } fpu = {val}; - return mp_obj_new_float(fpu.f); + return mp_obj_new_float((mp_float_t)fpu.f); #endif } else if (is_signed(val_type)) { if ((long long)MP_SMALL_INT_MIN <= val && val <= (long long)MP_SMALL_INT_MAX) { diff --git a/py/objarray.c b/py/objarray.c index 7f5cbf0022..f56d605709 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -445,7 +445,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value } #endif if (len_adj > 0) { - if (len_adj > o->free) { + if ((size_t)len_adj > o->free) { // TODO: alloc policy; at the moment we go conservative o->items = m_renew(byte, o->items, (o->len + o->free) * item_sz, (o->len + len_adj) * item_sz); o->free = len_adj; diff --git a/py/parsenum.c b/py/parsenum.c index cdcae8081f..10654115bd 100644 --- a/py/parsenum.c +++ b/py/parsenum.c @@ -218,7 +218,7 @@ mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') { // inf str += 3; - dec_val = INFINITY; + dec_val = (mp_float_t)INFINITY; if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') { // infinity str += 5;