Fix #945 (Array index out of bounds not detected, because of "int const")

http://sourceforge.net/apps/trac/cppcheck/ticket/945
This commit is contained in:
Reijo Tomperi 2009-11-12 23:49:39 +02:00
parent 3d5760b149
commit c4244ac9e8
3 changed files with 41 additions and 0 deletions

View File

@ -585,6 +585,8 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[])
* - try to change "for" loop to a "while" loop instead
*/
simplifyConst();
// Split up variable declarations.
simplifyVarDecl();
@ -4634,6 +4636,18 @@ void Tokenizer::simplifyComparisonOrder()
}
}
void Tokenizer::simplifyConst()
{
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "[;{}(,] %type% const"))
{
tok->tokAt(2)->str(tok->tokAt(1)->str());
tok->tokAt(1)->str("const");
}
}
}
void Tokenizer::getErrorMessages()
{
syntaxError(0, ' ');

View File

@ -315,6 +315,11 @@ private:
*/
void simplifyComparisonOrder();
/**
* Change "int const x;" into "const int x;"
*/
void simplifyConst();
/**
* Remove exception specifications. This function calls itself recursively.
* @param tok First token in scope to cleanup

View File

@ -164,6 +164,7 @@ private:
TEST_CASE(gt); // use "<" comparisons instead of ">"
TEST_CASE(simplifyString);
TEST_CASE(simplifyConst);
}
@ -2617,6 +2618,27 @@ private:
ASSERT_EQUALS("\"a3\"", tokenizer->simplifyString("\"\\x333\""));
}
void simplifyConst()
{
ASSERT_EQUALS("void foo ( ) { const int x ; }",
tokenizeAndStringify("void foo(){ int const x;}"));
ASSERT_EQUALS("void foo ( ) { { } const long x ; }",
tokenizeAndStringify("void foo(){ {} long const x;}"));
ASSERT_EQUALS("void foo ( ) { bar ( ) ; const char x ; }",
tokenizeAndStringify("void foo(){ bar(); char const x;}"));
ASSERT_EQUALS("void foo ( const char x ) { }",
tokenizeAndStringify("void foo(char const x){}"));
ASSERT_EQUALS("void foo ( int b , const char x ) { }",
tokenizeAndStringify("void foo(int b,char const x){}"));
ASSERT_EQUALS("void foo ( ) { int * const x ; }",
tokenizeAndStringify("void foo(){ int * const x;}"));
}
};
REGISTER_TEST(TestTokenizer)