mp_hal_delay_ms: avoid overflow when scaling ticks

This patch casts the delay argument to mp_hal_delay_ms()
from mp_uint_t to uint64_t when scaling from milliseconds
to ticks to avoid 32-bit integer overflow when time.sleep()
is called with a duration of greater than 70 minutes.

Signed-off-by: Trammell Hudson <hudson@trmm.net>
This commit is contained in:
Trammell Hudson 2021-09-17 16:56:12 +00:00
parent f5ef2559fe
commit 4ab00d7125
1 changed files with 4 additions and 4 deletions

View File

@ -127,12 +127,12 @@ void PLACE_IN_ITCM(supervisor_run_background_tasks_if_tick)() {
background_callback_run_all();
}
void mp_hal_delay_ms(mp_uint_t delay) {
void mp_hal_delay_ms(mp_uint_t delay_ms) {
uint64_t start_tick = port_get_raw_ticks(NULL);
// Adjust the delay to ticks vs ms.
delay = delay * 1024 / 1000;
uint64_t end_tick = start_tick + delay;
int64_t remaining = delay;
uint64_t delay_ticks = (delay_ms * (uint64_t)1024) / 1000;
uint64_t end_tick = start_tick + delay_ticks;
int64_t remaining = delay_ticks;
// Loop until we've waited long enough or we've been CTRL-Ced by autoreload
// or the user.