Skip to content

Commit 3d2daa2

Browse files
committed
py: Change exception traceback data to use size_t instead of mp_uint_t.
The traceback array stores qstrs and line numbers. qstrs are typed as size_t, and line numbers should safely fit in size_t as well.
1 parent ae4865e commit 3d2daa2

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

py/obj.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
8686
// helper function to print an exception with traceback
8787
void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
8888
if (mp_obj_is_exception_instance(exc)) {
89-
mp_uint_t n, *values;
89+
size_t n, *values;
9090
mp_obj_exception_get_traceback(exc, &n, &values);
9191
if (n > 0) {
9292
assert(n % 3 == 0);

py/obj.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,8 @@ bool mp_obj_is_exception_type(mp_obj_t self_in);
680680
bool mp_obj_is_exception_instance(mp_obj_t self_in);
681681
bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type);
682682
void mp_obj_exception_clear_traceback(mp_obj_t self_in);
683-
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, mp_uint_t line, qstr block);
684-
void mp_obj_exception_get_traceback(mp_obj_t self_in, mp_uint_t *n, mp_uint_t **values);
683+
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block);
684+
void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values);
685685
mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
686686
mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args);
687687
mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in);

py/objexcept.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char
346346
offset += sizeof(void *) - 1;
347347
offset &= ~(sizeof(void *) - 1);
348348

349-
if ((mp_emergency_exception_buf_size - offset) > (sizeof(mp_uint_t) * 3)) {
349+
if ((mp_emergency_exception_buf_size - offset) > (sizeof(o->traceback_data[0]) * 3)) {
350350
// We have room to store some traceback.
351-
o->traceback_data = (mp_uint_t*)((byte *)MP_STATE_VM(mp_emergency_exception_buf) + offset);
351+
o->traceback_data = (size_t*)((byte *)MP_STATE_VM(mp_emergency_exception_buf) + offset);
352352
o->traceback_alloc = (MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size - (byte *)o->traceback_data) / sizeof(o->traceback_data[0]);
353353
o->traceback_len = 0;
354354
}
@@ -429,37 +429,37 @@ void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
429429
self->traceback_data = NULL;
430430
}
431431

432-
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, mp_uint_t line, qstr block) {
432+
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block) {
433433
GET_NATIVE_EXCEPTION(self, self_in);
434434

435435
// append this traceback info to traceback data
436436
// if memory allocation fails (eg because gc is locked), just return
437437

438438
if (self->traceback_data == NULL) {
439-
self->traceback_data = m_new_maybe(mp_uint_t, 3);
439+
self->traceback_data = m_new_maybe(size_t, 3);
440440
if (self->traceback_data == NULL) {
441441
return;
442442
}
443443
self->traceback_alloc = 3;
444444
self->traceback_len = 0;
445445
} else if (self->traceback_len + 3 > self->traceback_alloc) {
446446
// be conservative with growing traceback data
447-
mp_uint_t *tb_data = m_renew_maybe(mp_uint_t, self->traceback_data, self->traceback_alloc, self->traceback_alloc + 3, true);
447+
size_t *tb_data = m_renew_maybe(size_t, self->traceback_data, self->traceback_alloc, self->traceback_alloc + 3, true);
448448
if (tb_data == NULL) {
449449
return;
450450
}
451451
self->traceback_data = tb_data;
452452
self->traceback_alloc += 3;
453453
}
454454

455-
mp_uint_t *tb_data = &self->traceback_data[self->traceback_len];
455+
size_t *tb_data = &self->traceback_data[self->traceback_len];
456456
self->traceback_len += 3;
457-
tb_data[0] = (mp_uint_t)file;
458-
tb_data[1] = (mp_uint_t)line;
459-
tb_data[2] = (mp_uint_t)block;
457+
tb_data[0] = file;
458+
tb_data[1] = line;
459+
tb_data[2] = block;
460460
}
461461

462-
void mp_obj_exception_get_traceback(mp_obj_t self_in, mp_uint_t *n, mp_uint_t **values) {
462+
void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values) {
463463
GET_NATIVE_EXCEPTION(self, self_in);
464464

465465
if (self->traceback_data == NULL) {

py/objexcept.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ typedef struct _mp_obj_exception_t {
3333
mp_obj_base_t base;
3434
mp_uint_t traceback_alloc : (BITS_PER_WORD / 2);
3535
mp_uint_t traceback_len : (BITS_PER_WORD / 2);
36-
mp_uint_t *traceback_data;
36+
size_t *traceback_data;
3737
mp_obj_tuple_t *args;
3838
} mp_obj_exception_t;
3939

py/vm.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,9 +1311,9 @@ unwind_jump:;
13111311
qstr block_name = mp_decode_uint(&ip);
13121312
qstr source_file = mp_decode_uint(&ip);
13131313
#endif
1314-
mp_uint_t bc = code_state->ip - code_state->code_info - code_info_size;
1315-
mp_uint_t source_line = 1;
1316-
mp_uint_t c;
1314+
size_t bc = code_state->ip - code_state->code_info - code_info_size;
1315+
size_t source_line = 1;
1316+
size_t c;
13171317
while ((c = *ip)) {
13181318
mp_uint_t b, l;
13191319
if ((c & 0x80) == 0) {

0 commit comments

Comments
 (0)