Skip to content

Commit 9f17730

Browse files
committed
runtime: Fix printing failed allocation amounts.
On LP64 and LLP64 systems, size_t is bigger than unsigned. Printing the failed allocation as mp_uint_t allows the correct failed allocation size to be shown. However, there are occasions where mp_uint_t is bigger than size_t (nanbox). In that case, preserve the existing code path to avoid growth in executable size. Example where this affects the failed allocation message (on x86_64 coverage build): ``` >>> "a" * (1 << 54) ``` Before, this would print the size as 1. Now it prints it as 18014398509481985 (2**54 + 1). Signed-off-by: Jeff Epler <jepler@gmail.com>
1 parent 79e6805 commit 9f17730

6 files changed

Lines changed: 9 additions & 5 deletions

File tree

ports/powerpc/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494

9595
// This port is 64-bit
9696
#define UINT_FMT "%lu"
97+
#define SIZE_FMT "%lu"
9798
#define INT_FMT "%ld"
9899
#define HEX_FMT "%lx"
99100
typedef signed long mp_int_t; // must be pointer size

ports/qemu/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ typedef uint32_t mp_uint_t; // must be pointer size
7979
#endif
8080

8181
#define UINT_FMT "%lu"
82+
#define SIZE_FMT "%lu"
8283
#define INT_FMT "%ld"
8384
#define HEX_FMT "%lx"
8485

ports/stm32/mpconfigport_nanbox.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define UINT_FMT "%llu"
3838
#define INT_FMT "%lld"
3939
#define HEX_FMT "%llx"
40+
#define SIZE_FMT "%lu"
4041
typedef int64_t mp_int_t;
4142
typedef uint64_t mp_uint_t;
4243

ports/unix/variants/nanbox/mpconfigvariant.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ typedef uint64_t mp_uint_t;
4949
#define UINT_FMT "%llu"
5050
#define INT_FMT "%lld"
5151
#define HEX_FMT "%llx"
52+
#define SIZE_FMT "%lu"

py/mpconfig.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,18 +2243,18 @@ typedef time_t mp_timestamp_t;
22432243
#define UINT_FMT "%lu"
22442244
#define INT_FMT "%ld"
22452245
#define HEX_FMT "%lx"
2246-
#define SIZE_FMT "%ld"
2246+
#define SIZE_FMT "%lu"
22472247
#elif defined(_WIN64)
22482248
#define UINT_FMT "%llu"
22492249
#define INT_FMT "%lld"
22502250
#define HEX_FMT "%llx"
2251-
#define SIZE_FMT "%lld"
2251+
#define SIZE_FMT "%llu"
22522252
#else
22532253
// Archs where mp_int_t == int
22542254
#define UINT_FMT "%u"
22552255
#define INT_FMT "%d"
22562256
#define HEX_FMT "%x"
2257-
#define SIZE_FMT "%d"
2257+
#define SIZE_FMT "%u"
22582258
#endif
22592259
#endif // INT_FMT
22602260

py/runtime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,14 +1684,14 @@ mp_obj_t mp_parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t parse_i
16841684
#endif // MICROPY_ENABLE_COMPILER
16851685

16861686
MP_NORETURN void m_malloc_fail(size_t num_bytes) {
1687-
DEBUG_printf("memory allocation failed, allocating %u bytes\n", (uint)num_bytes);
1687+
DEBUG_printf("memory allocation failed, allocating " SIZE_FMT " bytes\n", num_bytes);
16881688
#if MICROPY_ENABLE_GC
16891689
if (gc_is_locked()) {
16901690
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("memory allocation failed, heap is locked"));
16911691
}
16921692
#endif
16931693
mp_raise_msg_varg(&mp_type_MemoryError,
1694-
MP_ERROR_TEXT("memory allocation failed, allocating %u bytes"), (uint)num_bytes);
1694+
MP_ERROR_TEXT("memory allocation failed, allocating " SIZE_FMT " bytes"), (mp_uint_t)num_bytes);
16951695
}
16961696

16971697
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE

0 commit comments

Comments
 (0)