diff --git a/lib/checkstring.cpp b/lib/checkstring.cpp index 994138efc..ab3f96046 100644 --- a/lib/checkstring.cpp +++ b/lib/checkstring.cpp @@ -45,17 +45,30 @@ void CheckString::stringLiteralWrite() if (!str) continue; if (Token::Match(tok, "%var% [") && Token::simpleMatch(tok->linkAt(1), "] =")) - stringLiteralWriteError(tok); + stringLiteralWriteError(tok, str); 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", - "Modifying string literal directly or indirectly is undefined behaviour"); + std::list callstack; + 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); } //--------------------------------------------------------------------------- diff --git a/lib/checkstring.h b/lib/checkstring.h index 9a3596ca2..073bc8c50 100644 --- a/lib/checkstring.h +++ b/lib/checkstring.h @@ -81,7 +81,7 @@ public: void sprintfOverlappingData(); private: - void stringLiteralWriteError(const Token *tok); + void stringLiteralWriteError(const Token *tok, const Token *strValue); void sprintfOverlappingDataError(const Token *tok, const std::string &varname); void strPlusCharError(const Token *tok); 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 { CheckString c(0, settings, errorLogger); - c.stringLiteralWriteError(0); + c.stringLiteralWriteError(0,0); c.sprintfOverlappingDataError(0, "varname"); c.strPlusCharError(0); c.incorrectStringCompareError(0, "substr", "\"Hello World\""); diff --git a/test/teststring.cpp b/test/teststring.cpp index 2ee9da3f9..938ec101b 100644 --- a/test/teststring.cpp +++ b/test/teststring.cpp @@ -102,13 +102,13 @@ private: " char *abc = \"abc\";\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" " char *abc = \"abc\";\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" " QString abc = \"abc\";\n"