From 3e19c3b62e8c3c7c8b9f0b8f7243b4cb583fd5ea Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 6 Jan 2026 16:03:41 +0200 Subject: [PATCH] LibC: Fix timeradd and timersub overflow/underflow --- userspace/libraries/LibC/include/sys/time.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/userspace/libraries/LibC/include/sys/time.h b/userspace/libraries/LibC/include/sys/time.h index 458bde62..8f9523d3 100644 --- a/userspace/libraries/LibC/include/sys/time.h +++ b/userspace/libraries/LibC/include/sys/time.h @@ -37,16 +37,20 @@ int utimes(const char* path, const struct timeval times[2]); do { \ (res)->tv_sec = (a)->tv_sec + (b)->tv_sec; \ (res)->tv_usec = (a)->tv_usec + (b)->tv_usec; \ - (res)->tv_sec += (res)->tv_usec / 1000000; \ - (res)->tv_usec %= 1000000; \ + if ((res)->tv_usec >= 1000000) { \ + (res)->tv_sec++; \ + (res)->tv_usec -= 1000000; \ + } \ } while (0) #define timersub(a, b, res) \ do { \ (res)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ (res)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ - (res)->tv_sec += (res)->tv_usec / 1000000; \ - (res)->tv_usec %= 1000000; \ + if ((res)->tv_usec < 0) { \ + (res)->tv_sec--; \ + (res)->tv_usec += 1000000; \ + } \ } while (0) #define timerclear(tvp) \