diff options
author | Damien George <damien.p.george@gmail.com> | 2015-04-23 15:28:18 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-04-23 15:28:18 +0100 |
commit | ede0f3ab3dfcc4ab1a5799b312395703bf942517 (patch) | |
tree | e3ab86562db9326cfd3424937a474b0e5060df57 /py | |
parent | fd787c5e4ed369ddf8a178a62b6faf8a421e2bbc (diff) | |
download | micropython-ede0f3ab3dfcc4ab1a5799b312395703bf942517.tar.gz micropython-ede0f3ab3dfcc4ab1a5799b312395703bf942517.zip |
py: Add optional code to check bytes constructor values are in range.
Compiled in only if MICROPY_CPYTHON_COMPAT is set.
Addresses issue #1093.
Diffstat (limited to 'py')
-rw-r--r-- | py/objstr.c | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/py/objstr.c b/py/objstr.c index 50fe10378b..7d307f5885 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -222,7 +222,13 @@ STATIC mp_obj_t bytes_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k mp_obj_t iterable = mp_getiter(args[0]); mp_obj_t item; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { - vstr_add_byte(&vstr, mp_obj_get_int(item)); + mp_int_t val = mp_obj_get_int(item); + #if MICROPY_CPYTHON_COMPAT + if (val < 0 || val > 255) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bytes value out of range")); + } + #endif + vstr_add_byte(&vstr, val); } return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); |