From cae27c5ec770890edf788cbbf82d881b862301ba Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 28 Oct 2023 16:47:57 +0200 Subject: [PATCH] Fix #12117 FP integerOverflowCond for shift by 31 bits (#5596) --- 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 d1c95d20e..f6e029290 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 c2e436876..d15363104 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -295,6 +295,11 @@ private: " 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() {