stringLiteralWrite: Updated error message

This commit is contained in:
Daniel Marjamäki 2015-06-05 14:34:13 +02:00
parent 5fd9dd04fa
commit e36cc9f6d9
3 changed files with 22 additions and 9 deletions

View File

@ -45,17 +45,30 @@ void CheckString::stringLiteralWrite()
if (!str) if (!str)
continue; continue;
if (Token::Match(tok, "%var% [") && Token::simpleMatch(tok->linkAt(1), "] =")) if (Token::Match(tok, "%var% [") && Token::simpleMatch(tok->linkAt(1), "] ="))
stringLiteralWriteError(tok); stringLiteralWriteError(tok, str);
else if (Token::Match(tok->previous(), "* %var% =")) else if (Token::Match(tok->previous(), "* %var% ="))
stringLiteralWriteError(tok); stringLiteralWriteError(tok, str);
} }
} }
} }
void CheckString::stringLiteralWriteError(const Token *tok) void CheckString::stringLiteralWriteError(const Token *tok, const Token *strValue)
{ {
reportError(tok, Severity::error, "stringLiteralWrite", std::list<const Token *> callstack;
"Modifying string literal directly or indirectly is undefined behaviour"); callstack.push_back(tok);
if (strValue)
callstack.push_back(strValue);
std::string errmsg("Modifying string literal");
if (strValue) {
std::string s = strValue->strValue();
if (s.size() > 15U)
s = s.substr(0,13) + "..";
errmsg += " \"" + s + "\"";
}
errmsg += " directly or indirectly is undefined behaviour";
reportError(callstack, Severity::error, "stringLiteralWrite", errmsg);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -81,7 +81,7 @@ public:
void sprintfOverlappingData(); void sprintfOverlappingData();
private: private:
void stringLiteralWriteError(const Token *tok); void stringLiteralWriteError(const Token *tok, const Token *strValue);
void sprintfOverlappingDataError(const Token *tok, const std::string &varname); void sprintfOverlappingDataError(const Token *tok, const std::string &varname);
void strPlusCharError(const Token *tok); void strPlusCharError(const Token *tok);
void incorrectStringCompareError(const Token *tok, const std::string& func, const std::string &string); void incorrectStringCompareError(const Token *tok, const std::string& func, const std::string &string);
@ -94,7 +94,7 @@ private:
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const { void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
CheckString c(0, settings, errorLogger); CheckString c(0, settings, errorLogger);
c.stringLiteralWriteError(0); c.stringLiteralWriteError(0,0);
c.sprintfOverlappingDataError(0, "varname"); c.sprintfOverlappingDataError(0, "varname");
c.strPlusCharError(0); c.strPlusCharError(0);
c.incorrectStringCompareError(0, "substr", "\"Hello World\""); c.incorrectStringCompareError(0, "substr", "\"Hello World\"");

View File

@ -102,13 +102,13 @@ private:
" char *abc = \"abc\";\n" " char *abc = \"abc\";\n"
" abc[0] = 'a';\n" " abc[0] = 'a';\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Modifying string literal directly or indirectly is undefined behaviour\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (error) Modifying string literal \"abc\" directly or indirectly is undefined behaviour\n", errout.str());
check("void f() {\n" check("void f() {\n"
" char *abc = \"abc\";\n" " char *abc = \"abc\";\n"
" *abc = 'a';\n" " *abc = 'a';\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Modifying string literal directly or indirectly is undefined behaviour\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (error) Modifying string literal \"abc\" directly or indirectly is undefined behaviour\n", errout.str());
check("void f() {\n" check("void f() {\n"
" QString abc = \"abc\";\n" " QString abc = \"abc\";\n"