Skip to content

Commit cae27c5

Browse files
Fix #12117 FP integerOverflowCond for shift by 31 bits (cppcheck-opensource#5596)
1 parent f6fb333 commit cae27c5

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

lib/checktype.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ void CheckType::checkIntegerOverflow()
205205
continue;
206206

207207
// For left shift, it's common practice to shift into the sign bit
208-
//if (tok->str() == "<<" && value->intvalue > 0 && value->intvalue < (((MathLib::bigint)1) << bits))
209-
// continue;
208+
if (tok->str() == "<<" && value->intvalue > 0 && value->intvalue < (((MathLib::bigint)1) << bits))
209+
continue;
210210

211211
integerOverflowError(tok, *value);
212212
}

test/testtype.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ class TestType : public TestFixture {
295295
" return 123456U * x;\n"
296296
"}",settings);
297297
ASSERT_EQUALS("", errout.str());
298+
299+
check("int f(int i) {\n" // #12117
300+
" return (i == 31) ? 1 << i : 0;\n"
301+
"}", settings);
302+
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());
298303
}
299304

300305
void signConversion() {

0 commit comments

Comments
 (0)