Merge pull request #4947 from microDev1/input-validation

Fix bytes() and bytearray() input validation
This commit is contained in:
Dan Halbert 2021-06-30 22:50:46 -04:00 committed by GitHub
commit 98002cf0ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 4 deletions

View File

@ -195,6 +195,9 @@ STATIC mp_obj_t bytearray_make_new(const mp_obj_type_t *type_in, size_t n_args,
// no args: construct an empty bytearray
return MP_OBJ_FROM_PTR(array_new(BYTEARRAY_TYPECODE, 0));
} else if (mp_obj_is_int(args[0])) {
if (n_args > 1) {
mp_raise_TypeError(MP_ERROR_TEXT("wrong number of arguments"));
}
// 1 arg, an integer: construct a blank bytearray of that length
mp_uint_t len = mp_obj_get_int(args[0]);
mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);

View File

@ -233,6 +233,10 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, cons
return MP_OBJ_FROM_PTR(o);
}
if (n_args > 1) {
goto wrong_args;
}
if (mp_obj_is_small_int(args[0])) {
mp_int_t len = MP_OBJ_SMALL_INT_VALUE(args[0]);
if (len < 0) {
@ -244,10 +248,6 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, cons
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
if (n_args > 1) {
goto wrong_args;
}
// check if __bytes__ exists, and if so delegate to it
mp_obj_t dest[2];
mp_load_method_maybe(args[0], MP_QSTR___bytes__, dest);