str plus char: Added check and error message for str + ch

This commit is contained in:
Daniel Marjamäki 2009-01-15 16:57:51 +00:00
parent a01c5a6099
commit 8bbd4b9401
6 changed files with 76 additions and 2 deletions

View File

@ -958,3 +958,28 @@ void CheckOther::functionVariableUsage()
}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// str plus char
//---------------------------------------------------------------------------
void CheckOther::strPlusChar()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next() )
{
if ( Token::Match(tok, "%str% + %any%") )
{
const char *s = tok->strAt(2);
// char constant..
if ( *s == '\'' )
_errorLogger->reportErr(ErrorMessage::strPlusChar(_tokenizer, tok));
}
}
}

View File

@ -71,6 +71,9 @@ public:
/** Unused function variables */
void functionVariableUsage();
/** str plus char */
void strPlusChar();
#ifndef UNIT_TESTING
private:
#endif

View File

@ -336,6 +336,10 @@ void CppCheck::checkFile(const std::string &code, const char FileName[])
// mean that an ';' has been added by accident
if (ErrorMessage::constStatement(_settings))
checkOther.CheckIncompleteStatement();
// Unusual pointer arithmetic
if (ErrorMessage::strPlusChar(_settings))
checkOther.strPlusChar();
}
//---------------------------------------------------------------------------

View File

@ -327,5 +327,14 @@ public:
return s._checkCodingStyle;
}
static std::string strPlusChar(const Tokenizer *tokenizer, const Token *Location)
{
return msg1(tokenizer, Location) + "Unusual pointer arithmetic";
}
static bool strPlusChar(const Settings &s)
{
return true;
}
};
#endif

View File

@ -37,10 +37,12 @@ private:
TEST_CASE(delete1);
TEST_CASE(delete2);
TEST_CASE(sprintf1); // Dangerous usage of sprintf
TEST_CASE(sprintf1); // Dangerous usage of sprintf
TEST_CASE(sprintf2);
TEST_CASE(sprintf3);
TEST_CASE(sprintf4); // struct member
TEST_CASE(sprintf4); // struct member
TEST_CASE(strPlusChar1); // "/usr" + '/'
}
void check(const char code[])
@ -157,6 +159,36 @@ private:
ASSERT_EQUALS(std::string(""), errout.str());
}
void strPlusChar(const char code[])
{
// Tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
// Clear the error buffer..
errout.str("");
// Check for redundant code..
CheckOther checkOther(&tokenizer, Settings(), this);
checkOther.strPlusChar();
}
void strPlusChar1()
{
// Strange looking pointer arithmetic..
strPlusChar("void foo()\n"
"{\n"
" const char *p = \"/usr\" + '/';\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:3]: Unusual pointer arithmetic\n"), errout.str());
}
};
REGISTER_TEST(TestOther)

View File

@ -94,6 +94,7 @@ int main()
err.push_back(Message("variableScope", Message::never, "The scope of the variable %1 can be limited", "varname"));
err.push_back(Message("ifAssignment", Message::style, "Assignment in if-condition"));
err.push_back(Message("conditionAlwaysTrueFalse", Message::style, "Condition is always %1", "truefalse"));
err.push_back(Message("strPlusChar", Message::std, "Unusual pointer arithmetic"));
// Generate code..
std::cout << "Generate code.." << std::endl;