Skip to content

Commit e31fbd9

Browse files
committed
py/objint: Use unsigned arithmetic when formatting an integer.
Otherwise the edge case of the most negative integer value will not convert correctly.
1 parent f66df1e commit e31fbd9

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

py/objint.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,10 @@ mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
170170

171171
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
172172
typedef mp_longint_impl_t fmt_int_t;
173+
typedef unsigned long long fmt_uint_t;
173174
#else
174175
typedef mp_int_t fmt_int_t;
176+
typedef mp_uint_t fmt_uint_t;
175177
#endif
176178

177179
void mp_obj_int_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
@@ -265,8 +267,9 @@ char *mp_obj_int_formatted(char **buf, size_t *buf_size, size_t *fmt_size, mp_co
265267
*(--b) = '0';
266268
} else {
267269
do {
268-
int c = num % base;
269-
num /= base;
270+
// The cast to fmt_uint_t is because num is positive and we want unsigned arithmetic
271+
int c = (fmt_uint_t)num % base;
272+
num = (fmt_uint_t)num / base;
270273
if (c >= 10) {
271274
c += base_char - 10;
272275
} else {

0 commit comments

Comments
 (0)
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