Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

modutime.c: Ensure proper smallint values on overflow #115

Merged
merged 3 commits into from
Jan 23, 2018
Merged

modutime.c: Ensure proper smallint values on overflow #115

merged 3 commits into from
Jan 23, 2018

Conversation

robert-hh
Copy link
Contributor

This fixes ensure, that the return values of ticks_ms(), ticks_us() and ticks_cpu() fit into a MP_SMALL_INT. That way, they can be fed into ticks_diff() without the risk of an OverflowError (see issue #113). As a side effect, the result of ticks_diff(a, b) is always defined as a reasonable value even if a is a time after b (a > b). In that case, the value is negative. The implementation just copies the mechanism from py/extmod/mphal_utime.c.
Second change: Add the method ticks_add().

This fixes ensure, that the return values of ticks_ms(), ticks_us() and ticks_cpu() fit into a MP_SMALL_INT. That way, they can be fed into ticks_diff() without the risk of an OverflowError (see issue #113). As a side effect, the result of ticks_diff(a, b) is always defined as a reasonable value even if a is a time after b (a > b). In that case, the value is negative. The implementation just copies the mechanism from py/extmod/mphal_utime.c.
Second change: Add the method ticks_add().
@robert-hh
Copy link
Contributor Author

robert-hh commented Jan 5, 2018

Letting ticks_ms() and ticks_us() return 64 bit values would not change that behavior, but then ticks_diff() would be obsolete, because if can re replaced by calculating the difference in Python. The code for that could be:

STATIC mp_obj_t time_ticks_ms(void) {
    return mp_obj_new_int_from_ll(system_get_rtc_time() / 1000);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_ms_obj, time_ticks_ms);

STATIC mp_obj_t time_ticks_us(void) {
    return mp_obj_new_int_from_ll(system_get_rtc_time());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_us_obj, time_ticks_us);

Edit: Changing in addition ticks_diff into:

STATIC mp_obj_t time_ticks_diff(mp_obj_t start_in, mp_obj_t end_in) {
    int32_t start = mp_obj_get_int_truncated(start_in);
    int32_t end = mp_obj_get_int_truncated(end_in);
    return mp_obj_new_int((end - start));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(time_ticks_diff_obj, time_ticks_diff);

avoids the overflow exception and gives a reasonable behavior for diffs <= 2**31, and maybe it's the only change needed.

Revert changes to ticks_diff() and the addition of ticks_add()
… ticks_xx() in ticks_diff()

Make ticks_add consistent to ticks_diff
And also still avoid the overrun condition when using results from ticks_xx() in ticks_diff() and ticks_add()
@husigeza husigeza merged commit df7839e into pycom:master Jan 23, 2018
@robert-hh robert-hh deleted the utime branch January 25, 2018 06:53
peter-pycom added a commit that referenced this pull request Jun 10, 2020
X-Ryl669 pushed a commit to X-Ryl669/pycom-micropython-sigfox that referenced this pull request May 12, 2023
modutime.c: Ensure proper smallint values on overflow
X-Ryl669 pushed a commit to X-Ryl669/pycom-micropython-sigfox that referenced this pull request May 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants