Skip to content

Commit ede0f3a

Browse files
committed
py: Add optional code to check bytes constructor values are in range.
Compiled in only if MICROPY_CPYTHON_COMPAT is set. Addresses issue adafruit#1093.
1 parent fd787c5 commit ede0f3a

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

py/objstr.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
222222
mp_obj_t iterable = mp_getiter(args[0]);
223223
mp_obj_t item;
224224
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
225-
vstr_add_byte(&vstr, mp_obj_get_int(item));
225+
mp_int_t val = mp_obj_get_int(item);
226+
#if MICROPY_CPYTHON_COMPAT
227+
if (val < 0 || val > 255) {
228+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bytes value out of range"));
229+
}
230+
#endif
231+
vstr_add_byte(&vstr, val);
226232
}
227233

228234
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);

0 commit comments

Comments
 (0)