Skip to content

Commit 64ce567

Browse files
trunghieulenxpnashif
authored andcommitted
include: zephyr: sys: util: Add type check for macro DIV_ROUND_CLOSEST
Add type check to avoid comparing unsigned values with 0 which is always true or false and can cause coverity issue. Signed-off-by: Trung Hieu Le <[email protected]>
1 parent 27227c5 commit 64ce567

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

include/zephyr/sys/util.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,10 @@ extern "C" {
366366
*
367367
* @return The result of @p n / @p d, rounded to the nearest integer.
368368
*/
369-
#define DIV_ROUND_CLOSEST(n, d) \
370-
((((n) < 0) ^ ((d) < 0)) ? ((n) - ((d) / 2)) / (d) : \
371-
((n) + ((d) / 2)) / (d))
369+
#define DIV_ROUND_CLOSEST(n, d) \
370+
(((((__typeof__(n))-1) < 0) && (((__typeof__(d))-1) < 0) && ((n) < 0) ^ ((d) < 0)) \
371+
? ((n) - ((d) / 2)) / (d) \
372+
: ((n) + ((d) / 2)) / (d))
372373

373374
#ifndef MAX
374375
/**

0 commit comments

Comments
 (0)