[PATCH 07/14] Cleanup: avoid undefined shift operation

Dirk Hohndel dirk at hohndel.org
Thu Dec 28 16:35:35 PST 2017


Coverity CID 207769

Signed-off-by: Dirk Hohndel <dirk at hohndel.org>
---
 src/uwatec_smart_parser.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/uwatec_smart_parser.c b/src/uwatec_smart_parser.c
index a70acb7b91c6..acbb6eb8d983 100644
--- a/src/uwatec_smart_parser.c
+++ b/src/uwatec_smart_parser.c
@@ -886,7 +886,13 @@ uwatec_smart_fixsignbit (unsigned int x, unsigned int n)
 		return 0;
 
 	unsigned int signbit = (1 << (n - 1));
-	unsigned int mask = (0xFFFFFFFF << n);
+	unsigned int mask;
+
+	// shifting a 32bit constant by more than 31 bits has undefined behavior
+	if (n == 32)
+		mask = 0;
+	else
+		mask = (0xFFFFFFFF << n);
 
 	// When turning a two's-complement number with a certain number
 	// of bits into one with more bits, the sign bit must be repeated
-- 
2.15.1



More information about the devel mailing list