extmod/modubinascii: Add check for empty buffer passed to hexlify.

Previous to this patch hexlify(b'', b':') would lead to a bad crash due to
the computed length of the result being -1=0xffffffff.
pull/1/head
Damien George 2017-07-03 14:52:00 +10:00
parent 45b127e7ac
commit b86c65d31c
3 changed files with 10 additions and 0 deletions

View File

@ -42,6 +42,12 @@ mp_obj_t mod_binascii_hexlify(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
// Code below assumes non-zero buffer length when computing size with
// separator, so handle the zero-length case here.
if (bufinfo.len == 0) {
return mp_const_empty_bytes;
}
vstr_t vstr;
size_t out_len = bufinfo.len * 2;
if (n_args > 1) {

View File

@ -10,3 +10,6 @@ except ImportError:
# two arguments supported in uPy but not CPython
a = binascii.hexlify(b'123', ':')
print(a)
# zero length buffer
print(binascii.hexlify(b'', b':'))

View File

@ -1 +1,2 @@
b'31:32:33'
b''