From 27a20507a734c1d2c118950e16461fd240f4131e Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Mon, 20 Feb 2017 22:16:54 +0300 Subject: [PATCH] Eliminate 'condition sizeof(long)>4 is always true' cppcheck style warning * src/atomic_ops_malloc.c (msb): Cast (s >> 32) to unsigned int type before assigning the result to v; check __SIZEOF_SIZE_T__ value (if defined) to decide whether (s >> 32) is needed; move condition (sizeof(size_t) > 4) to SIZEOF_SIZE_T_GT_4 macro (to eliminate cppcheck false report that the condition is always true). --- src/atomic_ops_malloc.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/atomic_ops_malloc.c b/src/atomic_ops_malloc.c index aae21d0..499b145 100644 --- a/src/atomic_ops_malloc.c +++ b/src/atomic_ops_malloc.c @@ -251,14 +251,27 @@ static unsigned msb(size_t s) { unsigned result = 0; if ((s & 0xff) != s) { - unsigned v; - /* The following is a tricky code ought to be equivalent to */ - /* "(v = s >> 32) != 0" but suppresses warnings on 32-bit arch's. */ - if (sizeof(size_t) > 4 && (v = s >> (sizeof(size_t) > 4 ? 32 : 0)) != 0) - { - s = v; - result += 32; - } +# if __SIZEOF_SIZE_T__ == 8 + unsigned v = (unsigned)(s >> 32); + if (v != 0) + { + s = v; + result += 32; + } +# elif __SIZEOF_SIZE_T__ == 4 + /* No op. */ +# else + unsigned v; + /* The following is a tricky code ought to be equivalent to */ + /* "(v = s >> 32) != 0" but suppresses warnings on 32-bit arch's. */ +# define SIZEOF_SIZE_T_GT_4 (sizeof(size_t) > 4) + if (SIZEOF_SIZE_T_GT_4 + && (v = (unsigned)(s >> (SIZEOF_SIZE_T_GT_4 ? 32 : 0))) != 0) + { + s = v; + result += 32; + } +# endif /* !defined(__SIZEOF_SIZE_T__) */ if ((s >> 16) != 0) { s >>= 16; -- 2.40.0