From dd3acd5c3191a7a9a125d79fcae5a43ef3404527 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Wed, 25 Oct 2023 22:27:33 +0200 Subject: [PATCH] Fix #12117 FP integerOverflowCond for shift by 31 bits --- lib/checktype.cpp | 4 ++-- test/testtype.cpp | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/checktype.cpp b/lib/checktype.cpp index d1c95d20e2c..f6e0292903b 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -205,8 +205,8 @@ void CheckType::checkIntegerOverflow() continue; // For left shift, it's common practice to shift into the sign bit - //if (tok->str() == "<<" && value->intvalue > 0 && value->intvalue < (((MathLib::bigint)1) << bits)) - // continue; + if (tok->str() == "<<" && value->intvalue > 0 && value->intvalue < (((MathLib::bigint)1) << bits)) + continue; integerOverflowError(tok, *value); } diff --git a/test/testtype.cpp b/test/testtype.cpp index c2e436876b3..d1536310477 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -295,6 +295,11 @@ class TestType : public TestFixture { " return 123456U * x;\n" "}",settings); ASSERT_EQUALS("", errout.str()); + + check("int f(int i) {\n" // #12117 + " return (i == 31) ? 1 << i : 0;\n" + "}", settings); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (warning) Shifting signed 32-bit value by 31 bits is undefined behaviour. See condition at line 2.\n", errout.str()); } void signConversion() {