Commit a364bfe8 authored by Damien George's avatar Damien George

py/objarray: Protect against creating bytearray(n) with n negative.

It should already be protected by MemoryError but this check here is to be
extra safe, and comply with CPython behaviour.
parent 888bd75c
......@@ -182,7 +182,10 @@ STATIC mp_obj_t bytearray_make_new(const mp_obj_type_t *type_in, size_t n_args,
return MP_OBJ_FROM_PTR(array_new(BYTEARRAY_TYPECODE, 0));
} else if (MP_OBJ_IS_INT(args[0])) {
// 1 arg, an integer: construct a blank bytearray of that length
mp_uint_t len = mp_obj_get_int(args[0]);
mp_int_t len = mp_obj_get_int(args[0]);
if (len < 0) {
mp_raise_ValueError(NULL);
}
mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);
memset(o->items, 0, len);
return MP_OBJ_FROM_PTR(o);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment