From 596f635b28cdecbee6f7a97b162b13f3640e88a8 Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Mon, 19 Aug 2024 10:46:26 +0800 Subject: [PATCH] fix(newlib): fixed potential overflow in usleep If trying to usleep for 0xFFFF FFFF us the calculation of delay ticks would overflow resulting in the system not sleeping at all. Closes https://github.com/espressif/esp-idf/issues/14390 --- components/newlib/time.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/components/newlib/time.c b/components/newlib/time.c index 4b9fc6ca8e..ce993e8f71 100644 --- a/components/newlib/time.c +++ b/components/newlib/time.c @@ -200,7 +200,10 @@ WEAK_UNLESS_TIMEFUNC_IMPL int settimeofday(const struct timeval *tv, const struc int usleep(useconds_t us) { - const int us_per_tick = portTICK_PERIOD_MS * 1000; + /* Even at max tick rate, vTaskDelay can still delay for the max of the us argument, + we just need to make sure the tick calculation does not overflow + */ + const int64_t us_per_tick = portTICK_PERIOD_MS * 1000; if (us < us_per_tick) { esp_rom_delay_us((uint32_t) us); } else {