From f28c40aee0192282c0be8c8ee3a61e5a59003750 Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Sat, 22 Nov 2014 23:04:33 +0000 Subject: [PATCH] Fix the following -Werror warnings from clang 3.5.0, while building usr.sbin/rtadvd: usr.sbin/rtadvd/rtadvd.c:1291:7: error: taking the absolute value of unsigned type 'unsigned int' has no effect [-Werror,-Wabsolute-value] abs(preferred_time - pfx->pfx_pltimeexpire) > rai->rai_clockskew) { ^ usr.sbin/rtadvd/rtadvd.c:1291:7: note: remove the call to 'abs' since unsigned values cannot be negative abs(preferred_time - pfx->pfx_pltimeexpire) > rai->rai_clockskew) { ^~~ usr.sbin/rtadvd/rtadvd.c:1324:7: error: taking the absolute value of unsigned type 'unsigned int' has no effect [-Werror,-Wabsolute-value] abs(valid_time - pfx->pfx_vltimeexpire) > rai->rai_clockskew) { ^ usr.sbin/rtadvd/rtadvd.c:1324:7: note: remove the call to 'abs' since unsigned values cannot be negative abs(valid_time - pfx->pfx_vltimeexpire) > rai->rai_clockskew) { ^~~ 2 errors generated. These warnings occur because both preferred_time and pfx_pltimeexpire are uint32_t's, so the subtraction expression is also unsigned, and calling abs() is a no-op. However, the intention was to look at the absolute difference between the two unsigned quantities. Introduce a small static function to clarify what we're doing, and call that instead. Reviewed by: hrs MFC after: 3 days Differential Revision: https://reviews.freebsd.org/D1197 --- usr.sbin/rtadvd/rtadvd.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/usr.sbin/rtadvd/rtadvd.c b/usr.sbin/rtadvd/rtadvd.c index ba71954085e9..6554b61495a4 100644 --- a/usr.sbin/rtadvd/rtadvd.c +++ b/usr.sbin/rtadvd/rtadvd.c @@ -1230,6 +1230,12 @@ ra_input(int len, struct nd_router_advert *nra, return; } +static uint32_t +udiff(uint32_t u, uint32_t v) +{ + return (u >= v ? u - v : v - u); +} + /* return a non-zero value if the received prefix is inconsitent with ours */ static int prefix_check(struct nd_opt_prefix_info *pinfo, @@ -1288,7 +1294,7 @@ prefix_check(struct nd_opt_prefix_info *pinfo, preferred_time += now.tv_sec; if (!pfx->pfx_timer && rai->rai_clockskew && - abs(preferred_time - pfx->pfx_pltimeexpire) > rai->rai_clockskew) { + udiff(preferred_time, pfx->pfx_pltimeexpire) > rai->rai_clockskew) { syslog(LOG_INFO, "<%s> preferred lifetime for %s/%d" " (decr. in real time) inconsistent on %s:" @@ -1321,7 +1327,7 @@ prefix_check(struct nd_opt_prefix_info *pinfo, valid_time += now.tv_sec; if (!pfx->pfx_timer && rai->rai_clockskew && - abs(valid_time - pfx->pfx_vltimeexpire) > rai->rai_clockskew) { + udiff(valid_time, pfx->pfx_vltimeexpire) > rai->rai_clockskew) { syslog(LOG_INFO, "<%s> valid lifetime for %s/%d" " (decr. in real time) inconsistent on %s:"