Skip to content

Commit df7839e

Browse files
authored
Merge pull request pycom#115 from robert-hh/utime
modutime.c: Ensure proper smallint values on overflow
2 parents 86da809 + 93f32ea commit df7839e

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

esp32/mods/modutime.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,19 @@ STATIC mp_obj_t time_ticks_cpu(void) {
170170
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_cpu_obj, time_ticks_cpu);
171171

172172
STATIC mp_obj_t time_ticks_diff(mp_obj_t start_in, mp_obj_t end_in) {
173-
uint32_t start = mp_obj_get_int(start_in);
174-
uint32_t end = mp_obj_get_int(end_in);
175-
return mp_obj_new_int_from_uint((end - start));
173+
int32_t start = mp_obj_get_int_truncated(start_in);
174+
int32_t end = mp_obj_get_int_truncated(end_in);
175+
return mp_obj_new_int(end - start);
176176
}
177177
STATIC MP_DEFINE_CONST_FUN_OBJ_2(time_ticks_diff_obj, time_ticks_diff);
178178

179+
STATIC mp_obj_t time_ticks_add(mp_obj_t start_in, mp_obj_t delta_in) {
180+
int32_t start = mp_obj_get_int_truncated(start_in);
181+
int32_t delta = mp_obj_get_int_truncated(delta_in);
182+
return mp_obj_new_int(start + delta);
183+
}
184+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(time_ticks_add_obj, time_ticks_add);
185+
179186
/// \function time()
180187
/// Returns the number of seconds, as an integer, since 1/1/1970.
181188
STATIC mp_obj_t time_time(void) {
@@ -208,6 +215,7 @@ STATIC const mp_map_elem_t time_module_globals_table[] = {
208215
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&time_ticks_ms_obj },
209216
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&time_ticks_us_obj },
210217
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_cpu), (mp_obj_t)&time_ticks_cpu_obj },
218+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_add), (mp_obj_t)&time_ticks_add_obj },
211219
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&time_ticks_diff_obj },
212220
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_time_obj },
213221
{ MP_OBJ_NEW_QSTR(MP_QSTR_timezone), (mp_obj_t)&time_timezone_obj },

0 commit comments

Comments
 (0)