summaryrefslogtreecommitdiffstatshomepage
path: root/py/objint.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-05-05 21:42:39 +1000
committerDamien George <damien.p.george@gmail.com>2017-05-06 10:29:09 +1000
commit58bb73e010301235b32c3b81575af21cca55f2a7 (patch)
treed01586977dd71bc9a533bceb64dd7e899b2d5fc2 /py/objint.c
parent288ea06e7ce80c24c242ebd065be39ab3ab64c12 (diff)
downloadmicropython-58bb73e010301235b32c3b81575af21cca55f2a7.tar.gz
micropython-58bb73e010301235b32c3b81575af21cca55f2a7.zip
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.
Diffstat (limited to 'py/objint.c')
-rw-r--r--py/objint.c33
1 files changed, 16 insertions, 17 deletions
diff --git a/py/objint.c b/py/objint.c
index 99c54a3101..bda9c46cf0 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -407,26 +407,25 @@ STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
- #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
- // If result guaranteedly fits in small int, use that
- if (bufinfo.len >= sizeof(mp_uint_t) || !MP_SMALL_INT_FITS(1 << (bufinfo.len * 8 - 1))) {
- return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
- } else
- #endif
- {
- const byte* buf = (const byte*)bufinfo.buf;
- int delta = 1;
- if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
- buf += bufinfo.len - 1;
- delta = -1;
- }
+ const byte* buf = (const byte*)bufinfo.buf;
+ int delta = 1;
+ if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
+ buf += bufinfo.len - 1;
+ delta = -1;
+ }
- mp_uint_t value = 0;
- for (; bufinfo.len--; buf += delta) {
- value = (value << 8) | *buf;
+ mp_uint_t value = 0;
+ size_t len = bufinfo.len;
+ for (; len--; buf += delta) {
+ #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
+ if (value > (MP_SMALL_INT_MAX >> 8)) {
+ // Result will overflow a small-int so construct a big-int
+ return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
}
- return mp_obj_new_int_from_uint(value);
+ #endif
+ value = (value << 8) | *buf;
}
+ return mp_obj_new_int_from_uint(value);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_from_bytes);