From 0ab1fb3e4885b120307d4be4801e695a93715a79 Mon Sep 17 00:00:00 2001 From: jdfiguer Date: Thu, 13 Jun 2024 13:07:50 -0400 Subject: [PATCH] Fix #1458, Adds integer overflow protection to osapi-clock multiplication --- src/os/inc/osapi-clock.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/os/inc/osapi-clock.h b/src/os/inc/osapi-clock.h index 50cc82c6d..93432aad3 100644 --- a/src/os/inc/osapi-clock.h +++ b/src/os/inc/osapi-clock.h @@ -215,7 +215,17 @@ static inline int64 OS_TimeGetTotalMicroseconds(OS_time_t tm) */ static inline OS_time_t OS_TimeFromTotalMicroseconds(int64 tm) { - OS_time_t ostm = {tm * OS_TIME_TICKS_PER_USEC}; + OS_time_t ostm; + + // Check for overflow before performing multiplication + if (tm > INT64_MAX / OS_TIME_TICKS_PER_USEC) { + // If multiplication would overflow + ostm.ticks = INT64_MAX; // Using INT64_MAX to indicate overflow + } else { + // Perform multiplication if it is safe + ostm.ticks = tm * OS_TIME_TICKS_PER_USEC; + } + return ostm; }