Skip to content

Commit f964393

Browse files
zyfeierGUIDINGLI
authored andcommitted
clock: Incorrect use of unsigned number
https: //coverity.pt.xiaomi.com/#/project-view/10098/10010?selectedIssue=1275849 Signed-off-by: zhangyuan29 <zhangyuan29@xiaomi.com> Signed-off-by: ligd <liguiding1@xiaomi.com>
1 parent a90843e commit f964393

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

include/nuttx/clock.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -416,24 +416,24 @@ EXTERN volatile clock_t g_system_ticks;
416416
****************************************************************************/
417417

418418
#define clock_timespec_subtract(ts1, ts2, ts3) \
419-
do \
419+
do \
420+
{ \
421+
time_t _sec = (ts1)->tv_sec - (ts2)->tv_sec; \
422+
long _nsec = (ts1)->tv_nsec - (ts2)->tv_nsec; \
423+
if (_nsec < 0) \
420424
{ \
421-
time_t _sec = (ts1)->tv_sec - (ts2)->tv_sec; \
422-
long _nsec = (ts1)->tv_nsec - (ts2)->tv_nsec; \
423-
if (_nsec < 0) \
424-
{ \
425-
_nsec += NSEC_PER_SEC; \
426-
_sec--; \
427-
} \
428-
if (_sec < 0) \
429-
{ \
430-
_sec = 0; \
431-
_nsec = 0; \
432-
} \
433-
(ts3)->tv_sec = _sec; \
434-
(ts3)->tv_nsec = _nsec; \
435-
}\
436-
while (0)
425+
_nsec += NSEC_PER_SEC; \
426+
_sec--; \
427+
} \
428+
if ((int64_t)_sec < 0) \
429+
{ \
430+
_sec = 0; \
431+
_nsec = 0; \
432+
} \
433+
(ts3)->tv_sec = _sec; \
434+
(ts3)->tv_nsec = _nsec; \
435+
}\
436+
while (0)
437437

438438
/****************************************************************************
439439
* Name: clock_timespec_compare
@@ -446,9 +446,9 @@ EXTERN volatile clock_t g_system_ticks;
446446
****************************************************************************/
447447

448448
#define clock_timespec_compare(ts1, ts2) \
449-
(((ts1)->tv_sec < (ts2)->tv_sec) ? -1 : \
450-
((ts1)->tv_sec > (ts2)->tv_sec) ? 1 : \
451-
(ts1)->tv_nsec - (ts2)->tv_nsec)
449+
(((ts1)->tv_sec < (ts2)->tv_sec) ? -1 : \
450+
((ts1)->tv_sec > (ts2)->tv_sec) ? 1 : \
451+
(ts1)->tv_nsec - (ts2)->tv_nsec)
452452

453453
/****************************************************************************
454454
* Name: clock_abstime2ticks

0 commit comments

Comments
 (0)