diff options
author | Damien George <damien.p.george@gmail.com> | 2017-04-11 15:18:35 +1000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-04-11 15:18:35 +1000 |
commit | e31fbd9b41315963aaca732e7585fc4cb10ccbcd (patch) | |
tree | 6793b4b59fe22071e46781c051cb9898b416a6c7 /py | |
parent | f66df1efc85fe9fc7fd0b2125792d02001de48bb (diff) |
py/objint: Use unsigned arithmetic when formatting an integer.
Otherwise the edge case of the most negative integer value will not convert
correctly.
Diffstat (limited to 'py')
-rw-r--r-- | py/objint.c | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/py/objint.c b/py/objint.c index 719fd4970..99c54a310 100644 --- a/py/objint.c +++ b/py/objint.c @@ -170,8 +170,10 @@ mp_obj_t mp_obj_new_int_from_float(mp_float_t val) { #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG typedef mp_longint_impl_t fmt_int_t; +typedef unsigned long long fmt_uint_t; #else typedef mp_int_t fmt_int_t; +typedef mp_uint_t fmt_uint_t; #endif 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 *(--b) = '0'; } else { do { - int c = num % base; - num /= base; + // The cast to fmt_uint_t is because num is positive and we want unsigned arithmetic + int c = (fmt_uint_t)num % base; + num = (fmt_uint_t)num / base; if (c >= 10) { c += base_char - 10; } else { |