Fixed #1523 (false negative:: buffer access out of bounds when using shift operator)

This commit is contained in:
Robert Reif 2010-03-27 07:21:08 +01:00 committed by Daniel Marjamäki
parent 0cc84bd2be
commit c50469dba6
2 changed files with 30 additions and 0 deletions

View File

@ -5217,6 +5217,25 @@ bool Tokenizer::simplifyCalculations()
tok->deleteNext(); tok->deleteNext();
} }
} }
if (Token::Match(tok, "%num% <<|>> %num%"))
{
const int op1(MathLib::toLongNumber(tok->str()));
const int op2(MathLib::toLongNumber(tok->tokAt(2)->str()));
int result;
if (tok->next()->str() == "<<")
result = op1 << op2;
else
result = op1 >> op2;
std::stringstream ss;
ss << result;
tok->str(ss.str());
tok->deleteNext();
tok->deleteNext();
}
} }
return ret; return ret;
} }

View File

@ -92,6 +92,7 @@ private:
TEST_CASE(array_index_20); TEST_CASE(array_index_20);
TEST_CASE(array_index_21); TEST_CASE(array_index_21);
TEST_CASE(array_index_22); TEST_CASE(array_index_22);
TEST_CASE(array_index_23);
TEST_CASE(array_index_multidim); TEST_CASE(array_index_multidim);
TEST_CASE(array_index_switch_in_for); TEST_CASE(array_index_switch_in_for);
TEST_CASE(array_index_calculation); TEST_CASE(array_index_calculation);
@ -746,6 +747,16 @@ private:
ASSERT_EQUALS("[test.cpp:4]: (error) Array 'indices[2]' index 2 out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Array 'indices[2]' index 2 out of bounds\n", errout.str());
} }
void array_index_23()
{
check("void foo()\n"
"{\n"
" char c[10];\n"
" c[1<<23]='a';\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Array 'c[10]' index 8388608 out of bounds\n", errout.str());
}
void array_index_multidim() void array_index_multidim()
{ {
check("void f()\n" check("void f()\n"