Fix UB on right-shift. (#3235)

This commit is contained in:
keinflue 2021-04-28 09:57:28 +00:00 committed by GitHub
parent e1bfd369db
commit d2184ac6a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 1 deletions

View File

@ -534,7 +534,7 @@ void execute(const Token *expr,
*result = result1 << result2;
}
} else if (expr->str() == ">>") {
if (result2 < 0) { // don't perform UB
if (result2 < 0 || result2 >= MathLib::bigint_bits) { // don't perform UB
*error=true;
} else {
*result = result1 >> result2;

View File

@ -143,6 +143,9 @@ private:
ASSERT_EQUALS("", errout.str());
}
check("void f() { int x; x = 1 >> 64; }", &settings);
ASSERT_EQUALS("[test.cpp:1]: (error) Shifting 32-bit value by 64 bits is undefined behaviour\n", errout.str());
check("void foo() {\n"
" QList<int> someList;\n"
" someList << 300;\n"