From a5f08c1744a8a77d2376cdde6ec8e33fb48f3a91 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 18 Jul 2025 06:33:11 -0500 Subject: [PATCH 01/16] ports: Eliminate {U,}INT_FMT where redundant. The default definition in py/mpconfig.h is %u/%d, so these can be removed. Signed-off-by: Jeff Epler --- ports/cc3200/mpconfigport.h | 3 --- ports/esp32/mpconfigport.h | 3 --- ports/esp8266/mpconfigport.h | 3 --- ports/nrf/mpconfigport.h | 2 -- ports/pic16bit/mpconfigport.h | 2 -- ports/renesas-ra/mpconfigport.h | 2 -- ports/stm32/boards/ARDUINO_GIGA/mpconfigboard.h | 2 -- ports/stm32/boards/ARDUINO_NICLA_VISION/mpconfigboard.h | 2 -- ports/stm32/boards/ARDUINO_OPTA/mpconfigboard.h | 2 -- ports/stm32/boards/ARDUINO_PORTENTA_H7/mpconfigboard.h | 2 -- ports/stm32/mpconfigport.h | 2 -- ports/webassembly/mpconfigport.h | 2 -- 12 files changed, 27 deletions(-) diff --git a/ports/cc3200/mpconfigport.h b/ports/cc3200/mpconfigport.h index f1ba4bedd002e..3e8498e380be4 100644 --- a/ports/cc3200/mpconfigport.h +++ b/ports/cc3200/mpconfigport.h @@ -155,9 +155,6 @@ #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1)) #define MP_SSIZE_MAX (0x7FFFFFFF) -#define UINT_FMT "%u" -#define INT_FMT "%d" - typedef int32_t mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size typedef long mp_off_t; diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index a6f103cdef7a5..721f22de11c55 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -326,9 +326,6 @@ void *esp_native_code_commit(void *, size_t, void *); #define MICROPY_WRAP_MP_SCHED_EXCEPTION(f) IRAM_ATTR f #define MICROPY_WRAP_MP_SCHED_KEYBOARD_INTERRUPT(f) IRAM_ATTR f -#define UINT_FMT "%u" -#define INT_FMT "%d" - typedef int32_t mp_int_t; // must be pointer size typedef uint32_t mp_uint_t; // must be pointer size typedef long mp_off_t; diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index 03f3bb643d19b..bc2957190262b 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -150,9 +150,6 @@ #define MP_SSIZE_MAX (0x7fffffff) -#define UINT_FMT "%u" -#define INT_FMT "%d" - typedef int32_t mp_int_t; // must be pointer size typedef uint32_t mp_uint_t; // must be pointer size typedef long mp_off_t; diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index d52b5745d4e8f..963e1e8836db7 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -335,8 +335,6 @@ void *nrf_native_code_commit(void *, unsigned int, void *); #define MP_SSIZE_MAX (0x7fffffff) -#define UINT_FMT "%u" -#define INT_FMT "%d" #define HEX2_FMT "%02x" typedef int mp_int_t; // must be pointer size diff --git a/ports/pic16bit/mpconfigport.h b/ports/pic16bit/mpconfigport.h index d80f7edb9e546..7e6e1c4e02b17 100644 --- a/ports/pic16bit/mpconfigport.h +++ b/ports/pic16bit/mpconfigport.h @@ -77,8 +77,6 @@ #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p))) -#define UINT_FMT "%u" -#define INT_FMT "%d" typedef int mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size diff --git a/ports/renesas-ra/mpconfigport.h b/ports/renesas-ra/mpconfigport.h index 8a116269bb49e..868cdbc7d6ada 100644 --- a/ports/renesas-ra/mpconfigport.h +++ b/ports/renesas-ra/mpconfigport.h @@ -234,8 +234,6 @@ // Assume that if we already defined the obj repr then we also defined these items #ifndef MICROPY_OBJ_REPR -#define UINT_FMT "%u" -#define INT_FMT "%d" typedef int mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size #endif diff --git a/ports/stm32/boards/ARDUINO_GIGA/mpconfigboard.h b/ports/stm32/boards/ARDUINO_GIGA/mpconfigboard.h index 44f6ce66bc495..17d338fdc3340 100644 --- a/ports/stm32/boards/ARDUINO_GIGA/mpconfigboard.h +++ b/ports/stm32/boards/ARDUINO_GIGA/mpconfigboard.h @@ -12,8 +12,6 @@ #define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-giga-r1-wifi" #define MICROPY_OBJ_REPR (MICROPY_OBJ_REPR_C) -#define UINT_FMT "%u" -#define INT_FMT "%d" typedef int mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size diff --git a/ports/stm32/boards/ARDUINO_NICLA_VISION/mpconfigboard.h b/ports/stm32/boards/ARDUINO_NICLA_VISION/mpconfigboard.h index 47bf1be23d460..1be6189548bec 100644 --- a/ports/stm32/boards/ARDUINO_NICLA_VISION/mpconfigboard.h +++ b/ports/stm32/boards/ARDUINO_NICLA_VISION/mpconfigboard.h @@ -12,8 +12,6 @@ #define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-nicla-vision" #define MICROPY_OBJ_REPR (MICROPY_OBJ_REPR_C) -#define UINT_FMT "%u" -#define INT_FMT "%d" typedef int mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size diff --git a/ports/stm32/boards/ARDUINO_OPTA/mpconfigboard.h b/ports/stm32/boards/ARDUINO_OPTA/mpconfigboard.h index f52c8a26a81d0..fc563b2800e25 100644 --- a/ports/stm32/boards/ARDUINO_OPTA/mpconfigboard.h +++ b/ports/stm32/boards/ARDUINO_OPTA/mpconfigboard.h @@ -12,8 +12,6 @@ #define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-opta" #define MICROPY_OBJ_REPR (MICROPY_OBJ_REPR_C) -#define UINT_FMT "%u" -#define INT_FMT "%d" typedef int mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size diff --git a/ports/stm32/boards/ARDUINO_PORTENTA_H7/mpconfigboard.h b/ports/stm32/boards/ARDUINO_PORTENTA_H7/mpconfigboard.h index a9ecf38fbfab4..d8818f257cfdd 100644 --- a/ports/stm32/boards/ARDUINO_PORTENTA_H7/mpconfigboard.h +++ b/ports/stm32/boards/ARDUINO_PORTENTA_H7/mpconfigboard.h @@ -12,8 +12,6 @@ #define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-portenta-h7" #define MICROPY_OBJ_REPR (MICROPY_OBJ_REPR_C) -#define UINT_FMT "%u" -#define INT_FMT "%d" typedef int mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index b910188c5afaf..35deb93c6a0bf 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -230,8 +230,6 @@ extern const struct _mp_obj_type_t network_lan_type; // Assume that if we already defined the obj repr then we also defined these items #ifndef MICROPY_OBJ_REPR -#define UINT_FMT "%u" -#define INT_FMT "%d" typedef int mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size #endif diff --git a/ports/webassembly/mpconfigport.h b/ports/webassembly/mpconfigport.h index ab56162ca2b07..eea6f02a02633 100644 --- a/ports/webassembly/mpconfigport.h +++ b/ports/webassembly/mpconfigport.h @@ -107,8 +107,6 @@ // different targets may be defined in different ways - either as int // or as long. This requires different printf formatting specifiers // to print such value. So, we avoid int32_t and use int directly. -#define UINT_FMT "%u" -#define INT_FMT "%d" typedef int mp_int_t; // must be pointer size typedef unsigned mp_uint_t; // must be pointer size typedef long mp_off_t; From 350041861d33e824c9c76b8c1804f2c678a63f12 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 18 Jul 2025 06:37:26 -0500 Subject: [PATCH 02/16] various: Define HEX_FMT. Signed-off-by: Jeff Epler --- ports/powerpc/mpconfigport.h | 1 + ports/qemu/mpconfigport.h | 1 + ports/stm32/mpconfigport_nanbox.h | 1 + ports/unix/variants/nanbox/mpconfigvariant.h | 1 + py/mpconfig.h | 3 +++ 5 files changed, 7 insertions(+) diff --git a/ports/powerpc/mpconfigport.h b/ports/powerpc/mpconfigport.h index b74f374e7f9f5..25d85c9e61a72 100644 --- a/ports/powerpc/mpconfigport.h +++ b/ports/powerpc/mpconfigport.h @@ -96,6 +96,7 @@ // This port is 64-bit #define UINT_FMT "%lu" #define INT_FMT "%ld" +#define HEX_FMT "%lx" typedef signed long mp_int_t; // must be pointer size typedef unsigned long mp_uint_t; // must be pointer size diff --git a/ports/qemu/mpconfigport.h b/ports/qemu/mpconfigport.h index b02507277323e..9c879f55dfeb5 100644 --- a/ports/qemu/mpconfigport.h +++ b/ports/qemu/mpconfigport.h @@ -72,6 +72,7 @@ #define UINT_FMT "%lu" #define INT_FMT "%ld" +#define HEX_FMT "%lx" typedef int32_t mp_int_t; // must be pointer size typedef uint32_t mp_uint_t; // must be pointer size diff --git a/ports/stm32/mpconfigport_nanbox.h b/ports/stm32/mpconfigport_nanbox.h index f36d55aca9b4c..ffd87ba2f6c60 100644 --- a/ports/stm32/mpconfigport_nanbox.h +++ b/ports/stm32/mpconfigport_nanbox.h @@ -36,6 +36,7 @@ // Types needed for nan-boxing #define UINT_FMT "%llu" #define INT_FMT "%lld" +#define HEX_FMT "%llx" typedef int64_t mp_int_t; typedef uint64_t mp_uint_t; diff --git a/ports/unix/variants/nanbox/mpconfigvariant.h b/ports/unix/variants/nanbox/mpconfigvariant.h index 7b13b7dc6ce4e..8b23b93a8d3c8 100644 --- a/ports/unix/variants/nanbox/mpconfigvariant.h +++ b/ports/unix/variants/nanbox/mpconfigvariant.h @@ -48,3 +48,4 @@ typedef int64_t mp_int_t; typedef uint64_t mp_uint_t; #define UINT_FMT "%llu" #define INT_FMT "%lld" +#define HEX_FMT "%llx" diff --git a/py/mpconfig.h b/py/mpconfig.h index 4c1276275964d..61e4bdd68df00 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -2177,13 +2177,16 @@ typedef time_t mp_timestamp_t; // Archs where mp_int_t == long, long != int #define UINT_FMT "%lu" #define INT_FMT "%ld" +#define HEX_FMT "%lx" #elif defined(_WIN64) #define UINT_FMT "%llu" #define INT_FMT "%lld" +#define HEX_FMT "%llx" #else // Archs where mp_int_t == int #define UINT_FMT "%u" #define INT_FMT "%d" +#define HEX_FMT "%x" #endif #endif // INT_FMT From 9d6291f96896852c132f3cae7a20e93e5f7c4e54 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sat, 21 Jun 2025 15:37:35 +0200 Subject: [PATCH 03/16] core: Cast type names to qstr explicitly. The name field of type objects is of type uint16_t for efficiency, but when the type is passed to mp_printf it must be cast explicitly to type qstr. These locations were found using an experimental gcc plugin for mp_printf error checking, cross-building for x64 windows on Linux. Signed-off-by: Jeff Epler --- py/builtinhelp.c | 2 +- py/obj.c | 2 +- py/objdict.c | 2 +- py/objnamedtuple.c | 2 +- py/objtype.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/py/builtinhelp.c b/py/builtinhelp.c index a3fcc4dfb77c6..c08c2e3b6342b 100644 --- a/py/builtinhelp.c +++ b/py/builtinhelp.c @@ -135,7 +135,7 @@ static void mp_help_print_obj(const mp_obj_t obj) { // try to print something sensible about the given object mp_print_str(MP_PYTHON_PRINTER, "object "); mp_obj_print(obj, PRINT_STR); - mp_printf(MP_PYTHON_PRINTER, " is of type %q\n", type->name); + mp_printf(MP_PYTHON_PRINTER, " is of type %q\n", (qstr)type->name); mp_map_t *map = NULL; if (type == &mp_type_module) { diff --git a/py/obj.c b/py/obj.c index 586759460762b..26a912fc6825e 100644 --- a/py/obj.c +++ b/py/obj.c @@ -128,7 +128,7 @@ void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t if (MP_OBJ_TYPE_HAS_SLOT(type, print)) { MP_OBJ_TYPE_GET_SLOT(type, print)((mp_print_t *)print, o_in, kind); } else { - mp_printf(print, "<%q>", type->name); + mp_printf(print, "<%q>", (qstr)type->name); } } diff --git a/py/objdict.c b/py/objdict.c index cf64fa9555a28..451e87329030c 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -84,7 +84,7 @@ static void dict_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_ #endif } if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict && kind != PRINT_JSON) { - mp_printf(print, "%q(", self->base.type->name); + mp_printf(print, "%q(", (qstr)self->base.type->name); } mp_print_str(print, "{"); size_t cur = 0; diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c index f019604d5257b..e8447ee31ef08 100644 --- a/py/objnamedtuple.c +++ b/py/objnamedtuple.c @@ -63,7 +63,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(namedtuple_asdict_obj, namedtuple_asdict); static void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) { (void)kind; mp_obj_namedtuple_t *o = MP_OBJ_TO_PTR(o_in); - mp_printf(print, "%q", o->tuple.base.type->name); + mp_printf(print, "%q", (qstr)o->tuple.base.type->name); const qstr *fields = ((mp_obj_namedtuple_type_t *)o->tuple.base.type)->fields; mp_obj_attrtuple_print_helper(print, fields, &o->tuple); } diff --git a/py/objtype.c b/py/objtype.c index b9af1008995ef..f2173c79a173e 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -977,7 +977,7 @@ static bool check_for_special_accessors(mp_obj_t key, mp_obj_t value) { static void type_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { (void)kind; mp_obj_type_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "", self->name); + mp_printf(print, "", (qstr)self->name); } static mp_obj_t type_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { From d3f87d6e72fe4a17f5a90c568fe49c6ea4cdf4e8 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sat, 21 Jun 2025 15:40:38 +0200 Subject: [PATCH 04/16] core: Fix mp_printf integer size mismatches. The type of the argument must match the format string. Add casts to ensure that they do. It's possible that casting from `size_t` to `unsigned` loses the correct values by masking off upper bits, but it seems likely that the quantities involved in practice are small enough that the %u formatter (32 bits on most platforms, 16 on pic16bit) will in fact hold the correct value. The alternative, casting to a wider type, adds code size. These locations were found using an experimental gcc plugin for mp_printf error checking, cross-building for x64 windows on Linux. In one case there was already a cast, but it was written incorrectly and did not have the intended effect. Signed-off-by: Jeff Epler --- py/gc.c | 2 +- py/modmicropython.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/py/gc.c b/py/gc.c index eda63187b20a3..de66137f5800d 100644 --- a/py/gc.c +++ b/py/gc.c @@ -1202,7 +1202,7 @@ void gc_dump_alloc_table(const mp_print_t *print) { } if (bl2 - bl >= 2 * DUMP_BYTES_PER_LINE) { // there are at least 2 lines containing only free blocks, so abbreviate their printing - mp_printf(print, "\n (%u lines all free)", (uint)(bl2 - bl) / DUMP_BYTES_PER_LINE); + mp_printf(print, "\n (%u lines all free)", (uint)((bl2 - bl) / DUMP_BYTES_PER_LINE)); bl = bl2 & (~(DUMP_BYTES_PER_LINE - 1)); if (bl >= area->gc_alloc_table_byte_len * BLOCKS_PER_ATB) { // got to end of heap diff --git a/py/modmicropython.c b/py/modmicropython.c index d1a687f10e14e..4d676cb4ab27b 100644 --- a/py/modmicropython.c +++ b/py/modmicropython.c @@ -98,7 +98,7 @@ static mp_obj_t mp_micropython_qstr_info(size_t n_args, const mp_obj_t *args) { size_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes; qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes); mp_printf(&mp_plat_print, "qstr pool: n_pool=%u, n_qstr=%u, n_str_data_bytes=%u, n_total_bytes=%u\n", - n_pool, n_qstr, n_str_data_bytes, n_total_bytes); + (uint)n_pool, (uint)n_qstr, (uint)n_str_data_bytes, (uint)n_total_bytes); if (n_args == 1) { // arg given means dump qstr data qstr_dump_data(); From efab5be232b67316736460e7e36697a2482e23d9 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 24 Jun 2025 10:07:37 +0200 Subject: [PATCH 05/16] coverage: Avoid type checking an invalid string. we still want this not to crash a runtime but the new static checker wouldn't like it. Signed-off-by: Jeff Epler --- ports/unix/coverage.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index 68340d7f239a8..5bd02f6fbbc86 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -230,7 +230,12 @@ static mp_obj_t extra_coverage(void) { mp_printf(&mp_plat_print, "%u\n", 0x80000000); // should print unsigned mp_printf(&mp_plat_print, "%x\n", 0x8000000f); // should print unsigned mp_printf(&mp_plat_print, "%X\n", 0x8000000f); // should print unsigned - mp_printf(&mp_plat_print, "abc\n%"); // string ends in middle of format specifier + // note: storing the string in a variable is enough to prevent the + // format string checker from checking this format string. Otherwise, + // it would be a compile time diagnostic under the format string + // checker. + const char msg[] = "abc\n%"; + mp_printf(&mp_plat_print, msg); // string ends in middle of format specifier mp_printf(&mp_plat_print, "%%\n"); // literal % character mp_printf(&mp_plat_print, ".%-3s.\n", "a"); // left adjust From ae11d5d27c2fcee84b6f39a9c1e3de353bf3124e Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 24 Jun 2025 10:06:58 +0200 Subject: [PATCH 06/16] coverage: Cast type names to qstr explicitly. Signed-off-by: Jeff Epler --- ports/unix/coverage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index 5bd02f6fbbc86..c27198b3b31d5 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -218,7 +218,7 @@ static mp_obj_t extra_coverage(void) { } mp_printf(&mp_plat_print, "%p\n", (void *)0x789f); // pointer mp_printf(&mp_plat_print, "%P\n", (void *)0x789f); // pointer uppercase - mp_printf(&mp_plat_print, "%.2s %.3s '%4.4s' '%5.5q' '%.3q'\n", "abc", "abc", "abc", MP_QSTR_True, MP_QSTR_True); // fixed string precision + mp_printf(&mp_plat_print, "%.2s %.3s '%4.4s' '%5.5q' '%.3q'\n", "abc", "abc", "abc", (qstr)MP_QSTR_True, (qstr)MP_QSTR_True); // fixed string precision mp_printf(&mp_plat_print, "%.*s\n", -1, "abc"); // negative string precision mp_printf(&mp_plat_print, "%b %b\n", 0, 1); // bools #ifndef NDEBUG From 411674b37f810000a5ce210ad96de9daf5b50a87 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 10 Jul 2025 19:39:10 +0100 Subject: [PATCH 07/16] coverage: Cast values to fit. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes the following diagnostic produced by the plugin: ``` error: argument 3: Format ‘%x’ requires a ‘int’ or ‘unsigned int’ (32 bits), not ‘long unsigned int’ [size 64] [-Werror=format=] ``` Signed-off-by: Jeff Epler --- ports/unix/coverage.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index c27198b3b31d5..8b4072b9a37c3 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -528,7 +528,7 @@ static mp_obj_t extra_coverage(void) { // convert a large integer value (stored in a mpz) to mp_uint_t and to ll; mp_obj_t obj_bigint = mp_obj_new_int_from_uint((mp_uint_t)0xdeadbeef); - mp_printf(&mp_plat_print, "%x\n", mp_obj_get_uint(obj_bigint)); + mp_printf(&mp_plat_print, "%x\n", (int)mp_obj_get_uint(obj_bigint)); obj_bigint = mp_obj_new_int_from_ll(0xc0ffee777c0ffeell); long long value_ll = mp_obj_get_ll(obj_bigint); mp_printf(&mp_plat_print, "%x%08x\n", (uint32_t)(value_ll >> 32), (uint32_t)value_ll); @@ -536,13 +536,13 @@ static mp_obj_t extra_coverage(void) { // convert a large integer value (stored via a struct object) to uint and to ll // `deadbeef` global is an uctypes.struct defined by extra_coverage.py obj_bigint = mp_load_global(MP_QSTR_deadbeef); - mp_printf(&mp_plat_print, "%x\n", mp_obj_get_uint(obj_bigint)); + mp_printf(&mp_plat_print, "%x\n", (int)mp_obj_get_uint(obj_bigint)); value_ll = mp_obj_get_ll(obj_bigint); mp_printf(&mp_plat_print, "%x%08x\n", (uint32_t)(value_ll >> 32), (uint32_t)value_ll); // convert a smaller integer value to mp_uint_t and to ll obj_bigint = mp_obj_new_int_from_uint(0xc0ffee); - mp_printf(&mp_plat_print, "%x\n", mp_obj_get_uint(obj_bigint)); + mp_printf(&mp_plat_print, "%x\n", (int)mp_obj_get_uint(obj_bigint)); value_ll = mp_obj_get_ll(obj_bigint); mp_printf(&mp_plat_print, "%x%08x\n", (uint32_t)(value_ll >> 32), (uint32_t)value_ll); } From 2c3f6104f56b901553452b09005e1578478ab780 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 24 Jun 2025 10:08:19 +0200 Subject: [PATCH 08/16] coverage: Cast values to int for printing. During the coverage test, all the values encountered are within the range of %d. These locations were found using an experimental gcc plugin for mp_printf error checking. Signed-off-by: Jeff Epler --- ports/unix/coverage.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index 8b4072b9a37c3..aef123bb6b771 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -171,13 +171,13 @@ static void pairheap_test(size_t nops, int *ops) { if (mp_pairheap_is_empty(pairheap_lt, heap)) { mp_printf(&mp_plat_print, " -"); } else { - mp_printf(&mp_plat_print, " %d", mp_pairheap_peek(pairheap_lt, heap) - &node[0]); + mp_printf(&mp_plat_print, " %d", (int)(mp_pairheap_peek(pairheap_lt, heap) - &node[0])); ; } } mp_printf(&mp_plat_print, "\npop all:"); while (!mp_pairheap_is_empty(pairheap_lt, heap)) { - mp_printf(&mp_plat_print, " %d", mp_pairheap_peek(pairheap_lt, heap) - &node[0]); + mp_printf(&mp_plat_print, " %d", (int)(mp_pairheap_peek(pairheap_lt, heap) - &node[0])); ; heap = mp_pairheap_pop(pairheap_lt, heap); } @@ -274,7 +274,7 @@ static mp_obj_t extra_coverage(void) { mp_printf(&mp_plat_print, "%p\n", gc_realloc(p, 0, false)); // calling gc_nbytes with a non-heap pointer - mp_printf(&mp_plat_print, "%p\n", gc_nbytes(NULL)); + mp_printf(&mp_plat_print, "%d\n", (int)gc_nbytes(NULL)); } // GC initialisation and allocation stress test, to check the logic behind ALLOC_TABLE_GAP_BYTE @@ -335,7 +335,7 @@ static mp_obj_t extra_coverage(void) { } ptrs[i][j] = j; } - mp_printf(&mp_plat_print, "%d %d\n", i, all_zero); + mp_printf(&mp_plat_print, "%d %d\n", (int)i, (int)all_zero); // hide the pointer from the GC and collect ptrs[i] = FLIP_POINTER(ptrs[i]); @@ -351,7 +351,7 @@ static mp_obj_t extra_coverage(void) { break; } } - mp_printf(&mp_plat_print, "%d %d\n", i, correct_contents); + mp_printf(&mp_plat_print, "%d %d\n", (int)i, (int)correct_contents); } // free the memory blocks @@ -449,7 +449,7 @@ static mp_obj_t extra_coverage(void) { // create a bytearray via mp_obj_new_bytearray mp_buffer_info_t bufinfo; mp_get_buffer_raise(mp_obj_new_bytearray(4, "data"), &bufinfo, MP_BUFFER_RW); - mp_printf(&mp_plat_print, "%.*s\n", bufinfo.len, bufinfo.buf); + mp_printf(&mp_plat_print, "%.*s\n", (int)bufinfo.len, bufinfo.buf); } // mpz @@ -516,11 +516,11 @@ static mp_obj_t extra_coverage(void) { // hash the zero mpz integer mpz_set_from_int(&mpz, 0); - mp_printf(&mp_plat_print, "%d\n", mpz_hash(&mpz)); + mp_printf(&mp_plat_print, "%d\n", (int)mpz_hash(&mpz)); // convert the mpz zero integer to int mp_printf(&mp_plat_print, "%d\n", mpz_as_int_checked(&mpz, &value_signed)); - mp_printf(&mp_plat_print, "%d\n", value_signed); + mp_printf(&mp_plat_print, "%d\n", (int)value_signed); // mpz_set_from_float with 0 as argument mpz_set_from_float(&mpz, 0); @@ -562,7 +562,7 @@ static mp_obj_t extra_coverage(void) { mp_call_function_2_protected(MP_OBJ_FROM_PTR(&mp_builtin_divmod_obj), mp_obj_new_str_from_cstr("abc"), mp_obj_new_str_from_cstr("abc")); // mp_obj_int_get_checked with mp_obj_int_t that has a value that is a small integer - mp_printf(&mp_plat_print, "%d\n", mp_obj_int_get_checked(MP_OBJ_FROM_PTR(mp_obj_int_new_mpz()))); + mp_printf(&mp_plat_print, "%d\n", (int)mp_obj_int_get_checked(MP_OBJ_FROM_PTR(mp_obj_int_new_mpz()))); // mp_obj_int_get_uint_checked with non-negative small-int mp_printf(&mp_plat_print, "%d\n", (int)mp_obj_int_get_uint_checked(MP_OBJ_NEW_SMALL_INT(1))); @@ -674,7 +674,7 @@ static mp_obj_t extra_coverage(void) { #endif mp_vm_return_kind_t ret = mp_execute_bytecode(code_state, MP_OBJ_NULL); - mp_printf(&mp_plat_print, "%d %d\n", ret, mp_obj_get_type(code_state->state[0]) == &mp_type_NotImplementedError); + mp_printf(&mp_plat_print, "%d %d\n", (int)ret, mp_obj_get_type(code_state->state[0]) == &mp_type_NotImplementedError); } // scheduler @@ -754,36 +754,36 @@ static mp_obj_t extra_coverage(void) { mp_printf(&mp_plat_print, "# ringbuf\n"); // Single-byte put/get with empty ringbuf. - mp_printf(&mp_plat_print, "%d %d\n", ringbuf_free(&ringbuf), ringbuf_avail(&ringbuf)); + mp_printf(&mp_plat_print, "%d %d\n", (int)ringbuf_free(&ringbuf), (int)ringbuf_avail(&ringbuf)); ringbuf_put(&ringbuf, 22); - mp_printf(&mp_plat_print, "%d %d\n", ringbuf_free(&ringbuf), ringbuf_avail(&ringbuf)); + mp_printf(&mp_plat_print, "%d %d\n", (int)ringbuf_free(&ringbuf), (int)ringbuf_avail(&ringbuf)); mp_printf(&mp_plat_print, "%d\n", ringbuf_get(&ringbuf)); - mp_printf(&mp_plat_print, "%d %d\n", ringbuf_free(&ringbuf), ringbuf_avail(&ringbuf)); + mp_printf(&mp_plat_print, "%d %d\n", (int)ringbuf_free(&ringbuf), (int)ringbuf_avail(&ringbuf)); // Two-byte put/get with empty ringbuf. ringbuf_put16(&ringbuf, 0xaa55); - mp_printf(&mp_plat_print, "%d %d\n", ringbuf_free(&ringbuf), ringbuf_avail(&ringbuf)); + mp_printf(&mp_plat_print, "%d %d\n", (int)ringbuf_free(&ringbuf), (int)ringbuf_avail(&ringbuf)); mp_printf(&mp_plat_print, "%04x\n", ringbuf_get16(&ringbuf)); - mp_printf(&mp_plat_print, "%d %d\n", ringbuf_free(&ringbuf), ringbuf_avail(&ringbuf)); + mp_printf(&mp_plat_print, "%d %d\n", (int)ringbuf_free(&ringbuf), (int)ringbuf_avail(&ringbuf)); // Two-byte put with full ringbuf. for (int i = 0; i < 99; ++i) { ringbuf_put(&ringbuf, i); } - mp_printf(&mp_plat_print, "%d %d\n", ringbuf_free(&ringbuf), ringbuf_avail(&ringbuf)); + mp_printf(&mp_plat_print, "%d %d\n", (int)ringbuf_free(&ringbuf), (int)ringbuf_avail(&ringbuf)); mp_printf(&mp_plat_print, "%d\n", ringbuf_put16(&ringbuf, 0x11bb)); // Two-byte put with one byte free. ringbuf_get(&ringbuf); - mp_printf(&mp_plat_print, "%d %d\n", ringbuf_free(&ringbuf), ringbuf_avail(&ringbuf)); + mp_printf(&mp_plat_print, "%d %d\n", (int)ringbuf_free(&ringbuf), (int)ringbuf_avail(&ringbuf)); mp_printf(&mp_plat_print, "%d\n", ringbuf_put16(&ringbuf, 0x3377)); ringbuf_get(&ringbuf); - mp_printf(&mp_plat_print, "%d %d\n", ringbuf_free(&ringbuf), ringbuf_avail(&ringbuf)); + mp_printf(&mp_plat_print, "%d %d\n", (int)ringbuf_free(&ringbuf), (int)ringbuf_avail(&ringbuf)); mp_printf(&mp_plat_print, "%d\n", ringbuf_put16(&ringbuf, 0xcc99)); for (int i = 0; i < 97; ++i) { ringbuf_get(&ringbuf); } mp_printf(&mp_plat_print, "%04x\n", ringbuf_get16(&ringbuf)); - mp_printf(&mp_plat_print, "%d %d\n", ringbuf_free(&ringbuf), ringbuf_avail(&ringbuf)); + mp_printf(&mp_plat_print, "%d %d\n", (int)ringbuf_free(&ringbuf), (int)ringbuf_avail(&ringbuf)); // Two-byte put with wrap around on first byte: ringbuf.iput = 0; From b23576dfbeed870c8652635daaeefda319c3cbe7 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 24 Jun 2025 10:06:50 +0200 Subject: [PATCH 09/16] coverage: Provide argmuents of expected types. Signed-off-by: Jeff Epler --- ports/unix/coverage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index aef123bb6b771..0b97226f7ca36 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -203,7 +203,7 @@ static mp_obj_t extra_coverage(void) { mp_printf(&mp_plat_print, "# mp_printf\n"); mp_printf(&mp_plat_print, "%d %+d % d\n", -123, 123, 123); // sign mp_printf(&mp_plat_print, "%05d\n", -123); // negative number with zero padding - mp_printf(&mp_plat_print, "%ld\n", 123); // long + mp_printf(&mp_plat_print, "%ld\n", 123l); // long mp_printf(&mp_plat_print, "%lx\n", 0x123fl); // long hex mp_printf(&mp_plat_print, "%lX\n", 0x123fl); // capital long hex if (sizeof(mp_int_t) == 8) { From c13cb3d2749120fca3b3f666c7d9edbcdbe7b35a Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 17 Jul 2025 12:28:18 -0500 Subject: [PATCH 10/16] coverage: Remove unused printf args. Signed-off-by: Jeff Epler --- ports/unix/coverage.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index 0b97226f7ca36..f071049eded50 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -212,9 +212,9 @@ static mp_obj_t extra_coverage(void) { mp_printf(&mp_plat_print, "%llu\n", ULLONG_MAX); // unsigned long long } else { // fake for platforms without narrower mp_int_t - mp_printf(&mp_plat_print, "7fffffffffffffff\n", LLONG_MAX); - mp_printf(&mp_plat_print, "7FFFFFFFFFFFFFFF\n", LLONG_MAX); - mp_printf(&mp_plat_print, "18446744073709551615\n", ULLONG_MAX); + mp_printf(&mp_plat_print, "7fffffffffffffff\n"); + mp_printf(&mp_plat_print, "7FFFFFFFFFFFFFFF\n"); + mp_printf(&mp_plat_print, "18446744073709551615\n"); } mp_printf(&mp_plat_print, "%p\n", (void *)0x789f); // pointer mp_printf(&mp_plat_print, "%P\n", (void *)0x789f); // pointer uppercase From 53cb24875de8b0f5b88190b66942bd80165e59ef Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 24 Jun 2025 10:47:21 +0200 Subject: [PATCH 11/16] examplemodule: Cast arguments to printf. These locations were found using an experimental gcc plugin for mp_printf error checking. Signed-off-by: Jeff Epler --- examples/usercmodule/cexample/examplemodule.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/usercmodule/cexample/examplemodule.c b/examples/usercmodule/cexample/examplemodule.c index 83cc3b27c058e..a2b4d766f109f 100644 --- a/examples/usercmodule/cexample/examplemodule.c +++ b/examples/usercmodule/cexample/examplemodule.c @@ -86,12 +86,12 @@ static void example_AdvancedTimer_print(const mp_print_t *print, mp_obj_t self_i mp_uint_t elapsed = mp_obj_get_int(example_Timer_time(self_in)); // We'll make all representations print at least the class name. - mp_printf(print, "%q()", MP_QSTR_AdvancedTimer); + mp_printf(print, "%q()", (qstr)MP_QSTR_AdvancedTimer); // Decide what else to print based on print kind. if (kind == PRINT_STR) { // For __str__, let's attempt to make it more readable. - mp_printf(print, " # created %d seconds ago", elapsed / 1000); + mp_printf(print, " # created %d seconds ago", (int)(elapsed / 1000)); } } From b80227fe35e1fbdc90cc15b62b6b83b41726db2f Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 6 Jul 2025 08:12:01 +0100 Subject: [PATCH 12/16] modlwip: Print timeout with correct format string. As timeout is of type `mp_int_t`, it must be printed with INT_FMT. Before, the compiler plugin produced an error in the PYBD_SF6 build, which is a nanboxing build with 64-bit ints. Signed-off-by: Jeff Epler --- extmod/modlwip.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extmod/modlwip.c b/extmod/modlwip.c index b53559ed8cfe7..e1627485cf44f 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -907,7 +907,7 @@ static const mp_obj_type_t lwip_socket_type; static void lwip_socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { lwip_socket_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "", self->incoming.tcp.pbuf, self->recv_offset); } else { From 57a87505d5ca6a562755fee0c0425a16af1d6122 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 6 Jul 2025 08:19:21 +0100 Subject: [PATCH 13/16] netutils: Cast the ticks value before printing. Before, the compiler plugin produced an error in the PYBD_SF6 build, which is a nanboxing build with 64-bit ints. I made the decision here to cast the value even though some significant bits might be lost after 49.7 days. However, the format used is "% 8d", which produces a consistent width output for small ticks values (up to about 1.1 days). I judged that it was more valuable to preserve the fixed width display than to accurately represent long time periods. Signed-off-by: Jeff Epler --- shared/netutils/trace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/netutils/trace.c b/shared/netutils/trace.c index a6dfb42c28f00..24af4d5ca315f 100644 --- a/shared/netutils/trace.c +++ b/shared/netutils/trace.c @@ -56,7 +56,7 @@ static const char *ethertype_str(uint16_t type) { } void netutils_ethernet_trace(const mp_print_t *print, size_t len, const uint8_t *buf, unsigned int flags) { - mp_printf(print, "[% 8d] ETH%cX len=%u", mp_hal_ticks_ms(), flags & NETUTILS_TRACE_IS_TX ? 'T' : 'R', len); + mp_printf(print, "[% 8u] ETH%cX len=%u", (unsigned)mp_hal_ticks_ms(), flags & NETUTILS_TRACE_IS_TX ? 'T' : 'R', len); mp_printf(print, " dst=%02x:%02x:%02x:%02x:%02x:%02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); mp_printf(print, " src=%02x:%02x:%02x:%02x:%02x:%02x", buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]); From 47db6d5870eaaa37b57175b252e23359766f06ae Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sat, 5 Jul 2025 09:34:29 +0100 Subject: [PATCH 14/16] objcell: Fix printing cell ID. On the nanbox build, `o->obj` is a 64-bit type but `%p` formats a 32-bit type, leading to undefined behavior. Print the cell's ID as a hex integer instead. This location was found using an experimental gcc plugin for mp_printf error checking. Signed-off-by: Jeff Epler --- py/objcell.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/py/objcell.c b/py/objcell.c index 95966c7917cb7..cadfe0605a98a 100644 --- a/py/objcell.c +++ b/py/objcell.c @@ -30,7 +30,8 @@ static void cell_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) { (void)kind; mp_obj_cell_t *o = MP_OBJ_TO_PTR(o_in); - mp_printf(print, "obj); + MP_STATIC_ASSERT(sizeof(mp_obj_t) == sizeof(mp_uint_t)); + mp_printf(print, "obj); if (o->obj == MP_OBJ_NULL) { mp_print_str(print, "(nil)"); } else { From 716279a25c4d913057528c3202f0c81cbcee1838 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 6 Jul 2025 08:14:31 +0100 Subject: [PATCH 15/16] stm32: Add casts when printing small integers. All these arguments are of type `mp_{u,}int_t`, but the actual value is always a small integer. Cast it so that it can format with the %d/%u formatter. Before, the compiler plugin produced an error in the PYBD_SF6 build, which is a nanboxing build with 64-bit ints. Signed-off-by: Jeff Epler --- ports/stm32/extint.c | 2 +- ports/stm32/led.c | 2 +- ports/stm32/machine_uart.c | 2 +- ports/stm32/pin.c | 2 +- ports/stm32/timer.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ports/stm32/extint.c b/ports/stm32/extint.c index 9c3c24325368c..df0ed6e23e1f6 100644 --- a/ports/stm32/extint.c +++ b/ports/stm32/extint.c @@ -760,7 +760,7 @@ static mp_obj_t extint_make_new(const mp_obj_type_t *type, size_t n_args, size_t static void extint_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { extint_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "", self->line); + mp_printf(print, "", (int)self->line); } static const mp_rom_map_elem_t extint_locals_dict_table[] = { diff --git a/ports/stm32/led.c b/ports/stm32/led.c index 795d8c1109621..2fcb2abb60ddb 100644 --- a/ports/stm32/led.c +++ b/ports/stm32/led.c @@ -305,7 +305,7 @@ void led_debug(int n, int delay) { void led_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { pyb_led_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "LED(%u)", self->led_id); + mp_printf(print, "LED(%u)", (int)self->led_id); } /// \classmethod \constructor(id) diff --git a/ports/stm32/machine_uart.c b/ports/stm32/machine_uart.c index 8f1faea4b69ec..c93eade5d3df6 100644 --- a/ports/stm32/machine_uart.c +++ b/ports/stm32/machine_uart.c @@ -93,7 +93,7 @@ static void mp_machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_ #endif { mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=", - self->uart_id, uart_get_baudrate(self), bits); + self->uart_id, uart_get_baudrate(self), (int)bits); } if (!(cr1 & USART_CR1_PCE)) { mp_print_str(print, "None"); diff --git a/ports/stm32/pin.c b/ports/stm32/pin.c index 7de87f2c7be2f..515437ac8610e 100644 --- a/ports/stm32/pin.c +++ b/ports/stm32/pin.c @@ -232,7 +232,7 @@ static void pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t mp_uint_t af_idx = pin_get_af(self); const pin_af_obj_t *af_obj = pin_find_af_by_index(self, af_idx); if (af_obj == NULL) { - mp_printf(print, ", alt=%d)", af_idx); + mp_printf(print, ", alt=%d)", (int)af_idx); } else { mp_printf(print, ", alt=Pin.%q)", af_obj->name); } diff --git a/ports/stm32/timer.c b/ports/stm32/timer.c index 4ec467d9db53d..8aa0b3a2dda4b 100644 --- a/ports/stm32/timer.c +++ b/ports/stm32/timer.c @@ -499,7 +499,7 @@ static uint32_t compute_dtg_from_ticks(mp_int_t ticks) { // Given the 8-bit value stored in the DTG field of the BDTR register, compute // the number of ticks. -static mp_int_t compute_ticks_from_dtg(uint32_t dtg) { +static unsigned compute_ticks_from_dtg(uint32_t dtg) { if ((dtg & 0x80) == 0) { return dtg & 0x7F; } From 5092b4f756e5711b54974c0fd24a2a8f52ad6fbd Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 18 Jul 2025 06:06:42 -0500 Subject: [PATCH 16/16] mpprint: Fix printing pointers with upper bit set. On a build like nanbox, mp_uint_t is wider than u/intptr_t. Using a signed type for fetching pointer values resulted in erroneous results: like `` instead of ``. Signed-off-by: Jeff Epler --- py/mpprint.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/mpprint.c b/py/mpprint.c index e56b949ddda5f..f1d8bd0c57366 100644 --- a/py/mpprint.c +++ b/py/mpprint.c @@ -530,7 +530,7 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) { char fmt_chr = *fmt; mp_uint_t val; if (fmt_chr == 'p' || fmt_chr == 'P') { - val = va_arg(args, intptr_t); + val = va_arg(args, uintptr_t); } #if SUPPORT_LL_FORMAT else if (long_long_arg) { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy