From e20dd33f3754da1714411bc89fef6f9715773007 Mon Sep 17 00:00:00 2001 From: zhangyuan29 Date: Fri, 12 Jul 2024 19:03:19 +0800 Subject: [PATCH] clock: Incorrect use of unsigned number Fixed coverity issue. Signed-off-by: zhangyuan29 --- include/nuttx/clock.h | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/include/nuttx/clock.h b/include/nuttx/clock.h index 1463725a6f716..c4f94e41388fc 100644 --- a/include/nuttx/clock.h +++ b/include/nuttx/clock.h @@ -416,24 +416,24 @@ EXTERN volatile clock_t g_system_ticks; ****************************************************************************/ #define clock_timespec_subtract(ts1, ts2, ts3) \ - do \ + do \ + { \ + time_t _sec = (ts1)->tv_sec - (ts2)->tv_sec; \ + long _nsec = (ts1)->tv_nsec - (ts2)->tv_nsec; \ + if (_nsec < 0) \ { \ - time_t _sec = (ts1)->tv_sec - (ts2)->tv_sec; \ - long _nsec = (ts1)->tv_nsec - (ts2)->tv_nsec; \ - if (_nsec < 0) \ - { \ - _nsec += NSEC_PER_SEC; \ - _sec--; \ - } \ - if (_sec < 0) \ - { \ - _sec = 0; \ - _nsec = 0; \ - } \ - (ts3)->tv_sec = _sec; \ - (ts3)->tv_nsec = _nsec; \ - }\ - while (0) + _nsec += NSEC_PER_SEC; \ + _sec--; \ + } \ + if ((int64_t)_sec < 0) \ + { \ + _sec = 0; \ + _nsec = 0; \ + } \ + (ts3)->tv_sec = _sec; \ + (ts3)->tv_nsec = _nsec; \ + }\ + while (0) /**************************************************************************** * Name: clock_timespec_compare @@ -446,9 +446,9 @@ EXTERN volatile clock_t g_system_ticks; ****************************************************************************/ #define clock_timespec_compare(ts1, ts2) \ - (((ts1)->tv_sec < (ts2)->tv_sec) ? -1 : \ - ((ts1)->tv_sec > (ts2)->tv_sec) ? 1 : \ - (ts1)->tv_nsec - (ts2)->tv_nsec) + (((ts1)->tv_sec < (ts2)->tv_sec) ? -1 : \ + ((ts1)->tv_sec > (ts2)->tv_sec) ? 1 : \ + (ts1)->tv_nsec - (ts2)->tv_nsec) /**************************************************************************** * Name: clock_isleapyear