Skip to content

Commit 58bb73e

Browse files
committed
py/objint: In int.from_bytes, only create big-int if really needed.
This patch ensures that int.from_bytes only creates a big-int if necessary, by checking the value for a small-int overflow as it's being parsed.
1 parent 288ea06 commit 58bb73e

1 file changed

Lines changed: 16 additions & 17 deletions

File tree

py/objint.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -407,26 +407,25 @@ STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
407407
mp_buffer_info_t bufinfo;
408408
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
409409

410-
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
411-
// If result guaranteedly fits in small int, use that
412-
if (bufinfo.len >= sizeof(mp_uint_t) || !MP_SMALL_INT_FITS(1 << (bufinfo.len * 8 - 1))) {
413-
return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
414-
} else
415-
#endif
416-
{
417-
const byte* buf = (const byte*)bufinfo.buf;
418-
int delta = 1;
419-
if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
420-
buf += bufinfo.len - 1;
421-
delta = -1;
422-
}
410+
const byte* buf = (const byte*)bufinfo.buf;
411+
int delta = 1;
412+
if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
413+
buf += bufinfo.len - 1;
414+
delta = -1;
415+
}
423416

424-
mp_uint_t value = 0;
425-
for (; bufinfo.len--; buf += delta) {
426-
value = (value << 8) | *buf;
417+
mp_uint_t value = 0;
418+
size_t len = bufinfo.len;
419+
for (; len--; buf += delta) {
420+
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
421+
if (value > (MP_SMALL_INT_MAX >> 8)) {
422+
// Result will overflow a small-int so construct a big-int
423+
return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
427424
}
428-
return mp_obj_new_int_from_uint(value);
425+
#endif
426+
value = (value << 8) | *buf;
429427
}
428+
return mp_obj_new_int_from_uint(value);
430429
}
431430

432431
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_from_bytes);

0 commit comments

Comments
 (0)