diff options
| author | Matt Wozniski <godlygeek+git@gmail.com> | 2019-05-09 22:02:13 -0400 |
|---|---|---|
| committer | Matt Wozniski <godlygeek+git@gmail.com> | 2019-05-09 22:03:12 -0400 |
| commit | 1d0c61642b6fff2251c6b65a9fc18afd0ac255d5 (patch) | |
| tree | 9d6c74e9e87f72d3b59631c73a53565e6af849d7 /py/objint.c | |
| parent | fb7bb405758f12b9c16935001cdc43e57c262b75 (diff) | |
Reverse int to bytes buffer overflow conditionals
Rather than jumping to a label when an overflow is known to have
occurred, return early when one is known not to have.
Diffstat (limited to 'py/objint.c')
| -rw-r--r-- | py/objint.c | 43 |
1 files changed, 16 insertions, 27 deletions
diff --git a/py/objint.c b/py/objint.c index 029402a8d..80f6dfe55 100644 --- a/py/objint.c +++ b/py/objint.c @@ -303,43 +303,32 @@ char *mp_obj_int_formatted(char **buf, size_t *buf_size, size_t *fmt_size, mp_co void mp_obj_int_buffer_overflow_check(mp_obj_t self_in, size_t nbytes, bool is_signed) { if (is_signed) { - // edge = 1 << (nbytes * 8 - 1) + // self must be < 2**(bits - 1) mp_obj_t edge = mp_binary_op(MP_BINARY_OP_INPLACE_LSHIFT, mp_obj_new_int(1), mp_obj_new_int(nbytes * 8 - 1)); - // if self >= edge, we don't fit - if (mp_binary_op(MP_BINARY_OP_MORE_EQUAL, self_in, edge) == mp_const_true) { - goto raise; - } - - // edge = -edge - edge = mp_unary_op(MP_UNARY_OP_NEGATIVE, edge); - - // if self < edge, we don't fit if (mp_binary_op(MP_BINARY_OP_LESS, self_in, edge) == mp_const_true) { - goto raise; + // and >= -2**(bits - 1) + edge = mp_unary_op(MP_UNARY_OP_NEGATIVE, edge); + if (mp_binary_op(MP_BINARY_OP_MORE_EQUAL, self_in, edge) == mp_const_true) { + return; + } } } else { - if (mp_obj_int_sign(self_in) < 0) { - // Negative numbers never fit in an unsigned value - goto raise; - } - - // edge = 1 << (nbytes * 8) - mp_obj_t edge = mp_binary_op(MP_BINARY_OP_INPLACE_LSHIFT, - mp_obj_new_int(1), - mp_obj_new_int(nbytes * 8)); - - // if self >= edge, we don't fit - if (mp_binary_op(MP_BINARY_OP_MORE_EQUAL, self_in, edge) == mp_const_true) { - goto raise; + // self must be >= 0 + if (mp_obj_int_sign(self_in) >= 0) { + // and < 2**(bits) + mp_obj_t edge = mp_binary_op(MP_BINARY_OP_INPLACE_LSHIFT, + mp_obj_new_int(1), + mp_obj_new_int(nbytes * 8)); + + if (mp_binary_op(MP_BINARY_OP_LESS, self_in, edge) == mp_const_true) { + return; + } } } - return; - -raise: mp_raise_OverflowError_varg(translate("value would overflow a %d byte buffer"), nbytes); } |
