From c88cfe165b0ab39c5d9392fb02dd12f22be1a28d Mon Sep 17 00:00:00 2001 From: Damien George <damien.p.george@gmail.com> Date: Thu, 23 Mar 2017 16:17:40 +1100 Subject: [PATCH] py: Use size_t as len argument and return type of mp_get_index. These values are used to compute memory addresses and so size_t is the more appropriate type to use. --- py/obj.c | 6 ++++-- py/obj.h | 2 +- py/objarray.c | 2 +- py/objlist.c | 6 +++--- py/objrange.c | 2 +- py/objstr.c | 4 ++-- py/objstrunicode.c | 2 +- py/objtuple.c | 2 +- py/sequence.c | 6 +++--- 9 files changed, 17 insertions(+), 15 deletions(-) diff --git a/py/obj.c b/py/obj.c index 4534ef1b2..65a35c86f 100644 --- a/py/obj.c +++ b/py/obj.c @@ -351,7 +351,7 @@ void mp_obj_get_array_fixed_n(mp_obj_t o, mp_uint_t len, mp_obj_t **items) { } // is_slice determines whether the index is a slice index -mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, bool is_slice) { +size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice) { mp_int_t i; if (MP_OBJ_IS_SMALL_INT(index)) { i = MP_OBJ_SMALL_INT_VALUE(index); @@ -384,7 +384,9 @@ mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, } } } - return i; + + // By this point 0 <= i <= len and so fits in a size_t + return (size_t)i; } mp_obj_t mp_obj_id(mp_obj_t o_in) { diff --git a/py/obj.h b/py/obj.h index 79a6d56fa..03dadeb07 100644 --- a/py/obj.h +++ b/py/obj.h @@ -674,7 +674,7 @@ void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag); //qstr mp_obj_get_qstr(mp_obj_t arg); void mp_obj_get_array(mp_obj_t o, mp_uint_t *len, mp_obj_t **items); // *items may point inside a GC block void mp_obj_get_array_fixed_n(mp_obj_t o, mp_uint_t len, mp_obj_t **items); // *items may point inside a GC block -mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, bool is_slice); +size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice); mp_obj_t mp_obj_id(mp_obj_t o_in); mp_obj_t mp_obj_len(mp_obj_t o_in); mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); // may return MP_OBJ_NULL diff --git a/py/objarray.c b/py/objarray.c index a84a63151..af75af917 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -471,7 +471,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value return MP_OBJ_FROM_PTR(res); #endif } else { - mp_uint_t index = mp_get_index(o->base.type, o->len, index_in, false); + size_t index = mp_get_index(o->base.type, o->len, index_in, false); #if MICROPY_PY_BUILTINS_MEMORYVIEW if (o->base.type == &mp_type_memoryview) { index += o->free; diff --git a/py/objlist.c b/py/objlist.c index ba7898415..312cef6d3 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -186,7 +186,7 @@ STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { return MP_OBJ_FROM_PTR(res); } #endif - mp_uint_t index_val = mp_get_index(self->base.type, self->len, index, false); + size_t index_val = mp_get_index(self->base.type, self->len, index, false); return self->items[index_val]; } else { #if MICROPY_PY_BUILTINS_SLICE @@ -268,7 +268,7 @@ STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) { if (self->len == 0) { mp_raise_msg(&mp_type_IndexError, "pop from empty list"); } - mp_uint_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false); + size_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false); mp_obj_t ret = self->items[index]; self->len -= 1; memmove(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t)); @@ -490,7 +490,7 @@ void mp_obj_list_set_len(mp_obj_t self_in, size_t len) { void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in); - mp_uint_t i = mp_get_index(self->base.type, self->len, index, false); + size_t i = mp_get_index(self->base.type, self->len, index, false); self->items[i] = value; } diff --git a/py/objrange.c b/py/objrange.c index dd074a98a..5bf0bc7a9 100644 --- a/py/objrange.c +++ b/py/objrange.c @@ -155,7 +155,7 @@ STATIC mp_obj_t range_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { return MP_OBJ_FROM_PTR(o); } #endif - uint index_val = mp_get_index(self->base.type, len, index, false); + size_t index_val = mp_get_index(self->base.type, len, index, false); return MP_OBJ_NEW_SMALL_INT(self->start + index_val * self->step); } else { return MP_OBJ_NULL; // op not supported diff --git a/py/objstr.c b/py/objstr.c index 1e71617bd..4053efb88 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -388,7 +388,7 @@ mp_obj_t mp_obj_str_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { // objstrunicode defines own version const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, size_t self_len, mp_obj_t index, bool is_slice) { - mp_uint_t index_val = mp_get_index(type, self_len, index, is_slice); + size_t index_val = mp_get_index(type, self_len, index, is_slice); return self_data + index_val; } #endif @@ -408,7 +408,7 @@ STATIC mp_obj_t bytes_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { return mp_obj_new_str_of_type(type, self_data + slice.start, slice.stop - slice.start); } #endif - mp_uint_t index_val = mp_get_index(type, self_len, index, false); + size_t index_val = mp_get_index(type, self_len, index, false); // If we have unicode enabled the type will always be bytes, so take the short cut. if (MICROPY_PY_BUILTINS_STR_UNICODE || type == &mp_type_bytes) { return MP_OBJ_NEW_SMALL_INT(self_data[index_val]); diff --git a/py/objstrunicode.c b/py/objstrunicode.c index 441ec293d..9a6ce9b9a 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -120,7 +120,7 @@ const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, s // so it must handle bytes. if (type == &mp_type_bytes) { // Taken from objstr.c:str_index_to_ptr() - mp_uint_t index_val = mp_get_index(type, self_len, index, is_slice); + size_t index_val = mp_get_index(type, self_len, index, is_slice); return self_data + index_val; } diff --git a/py/objtuple.c b/py/objtuple.c index b28807c0d..a8efdc7f9 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -186,7 +186,7 @@ mp_obj_t mp_obj_tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { return MP_OBJ_FROM_PTR(res); } #endif - mp_uint_t index_value = mp_get_index(self->base.type, self->len, index, false); + size_t index_value = mp_get_index(self->base.type, self->len, index, false); return self->items[index_value]; } else { return MP_OBJ_NULL; // op not supported diff --git a/py/sequence.c b/py/sequence.c index bc2cfc077..9aa3b6a89 100644 --- a/py/sequence.c +++ b/py/sequence.c @@ -239,8 +239,8 @@ bool mp_seq_cmp_objs(mp_uint_t op, const mp_obj_t *items1, mp_uint_t len1, const mp_obj_t mp_seq_index_obj(const mp_obj_t *items, mp_uint_t len, mp_uint_t n_args, const mp_obj_t *args) { mp_obj_type_t *type = mp_obj_get_type(args[0]); mp_obj_t value = args[1]; - uint start = 0; - uint stop = len; + size_t start = 0; + size_t stop = len; if (n_args >= 3) { start = mp_get_index(type, len, args[2], true); @@ -249,7 +249,7 @@ mp_obj_t mp_seq_index_obj(const mp_obj_t *items, mp_uint_t len, mp_uint_t n_args } } - for (mp_uint_t i = start; i < stop; i++) { + for (size_t i = start; i < stop; i++) { if (mp_obj_equal(items[i], value)) { // Common sense says this cannot overflow small int return MP_OBJ_NEW_SMALL_INT(i); -- GitLab