Skip to content

Commit b86c65d

Browse files
committed
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.
1 parent 45b127e commit b86c65d

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

extmod/modubinascii.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ mp_obj_t mod_binascii_hexlify(size_t n_args, const mp_obj_t *args) {
4242
mp_buffer_info_t bufinfo;
4343
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
4444

45+
// Code below assumes non-zero buffer length when computing size with
46+
// separator, so handle the zero-length case here.
47+
if (bufinfo.len == 0) {
48+
return mp_const_empty_bytes;
49+
}
50+
4551
vstr_t vstr;
4652
size_t out_len = bufinfo.len * 2;
4753
if (n_args > 1) {

tests/extmod/ubinascii_micropython.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
# two arguments supported in uPy but not CPython
1111
a = binascii.hexlify(b'123', ':')
1212
print(a)
13+
14+
# zero length buffer
15+
print(binascii.hexlify(b'', b':'))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
b'31:32:33'
2+
b''

0 commit comments

Comments
 (0)