Fix ticket #318 ('..' in include will cause conflicting slashes in messages)

This commit is contained in:
Daniel Marjamäki 2009-05-31 10:12:19 +02:00
parent 375dfeab06
commit 8b76301ee2
3 changed files with 54 additions and 3 deletions

View File

@ -46,7 +46,7 @@ std::string ErrorLogger::ErrorMessage::serialize() const
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = _callStack.begin(); tok != _callStack.end(); ++tok) for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = _callStack.begin(); tok != _callStack.end(); ++tok)
{ {
std::ostringstream smallStream; std::ostringstream smallStream;
smallStream << (*tok).line << ":" << (*tok).file; smallStream << (*tok).line << ":" << (*tok).getfile();
oss << smallStream.str().length() << " " << smallStream.str(); oss << smallStream.str().length() << " " << smallStream.str();
} }
return oss.str(); return oss.str();
@ -119,7 +119,7 @@ std::string ErrorLogger::ErrorMessage::toXML() const
xml << "<error"; xml << "<error";
if (!_callStack.empty()) if (!_callStack.empty())
{ {
xml << " file=\"" << _callStack.back().file << "\""; xml << " file=\"" << _callStack.back().getfile() << "\"";
xml << " line=\"" << _callStack.back().line << "\""; xml << " line=\"" << _callStack.back().line << "\"";
} }
xml << " id=\"" << _id << "\""; xml << " id=\"" << _id << "\"";
@ -191,6 +191,41 @@ std::string ErrorLogger::callStackToString(const std::list<ErrorLogger::ErrorMes
{ {
std::ostringstream ostr; std::ostringstream ostr;
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok) for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok)
ostr << (tok == callStack.begin() ? "" : " -> ") << "[" << (*tok).file << ":" << (*tok).line << "]"; ostr << (tok == callStack.begin() ? "" : " -> ") << "[" << (*tok).getfile() << ":" << (*tok).line << "]";
return ostr.str(); return ostr.str();
} }
std::string ErrorLogger::ErrorMessage::FileLocation::getfile() const
{
std::string f(file);
// replace "/ab/.." with "/"..
std::string::size_type pos = 0;
while ((pos = f.find("..", pos + 1)) != std::string::npos)
{
// position must be at least 4..
if (pos < 4)
continue;
// Previous char must be a separator..
if (f[pos-1] != '/' && f[pos-2] != '\\')
continue;
// Next char must be a separator..
if (f[pos+2] != '/' && f[pos+2] != '\\')
continue;
// Locate previous separator..
std::string::size_type sep = f.find_last_of("/\\", pos - 2);
if (sep == std::string::npos)
continue;
// Delete substring..
f.erase(sep, pos + 2 - sep);
pos = sep;
}
return f;
}

View File

@ -50,6 +50,8 @@ public:
{ {
line = 0; line = 0;
} }
std::string getfile() const;
std::string file; std::string file;
unsigned int line; unsigned int line;
}; };

View File

@ -51,6 +51,8 @@ private:
// TEST_CASE(linenumbers2); // TEST_CASE(linenumbers2);
TEST_CASE(xml); TEST_CASE(xml);
TEST_CASE(include);
} }
void linenumbers() void linenumbers()
@ -90,6 +92,18 @@ private:
errmsg._msg = "ab<cd>ef"; errmsg._msg = "ab<cd>ef";
ASSERT_EQUALS("<error id=\"\" severity=\"\" msg=\"ab&lt;cd&gt;ef\"/>", errmsg.toXML()); ASSERT_EQUALS("<error id=\"\" severity=\"\" msg=\"ab&lt;cd&gt;ef\"/>", errmsg.toXML());
} }
void include()
{
ErrorLogger::ErrorMessage errmsg;
ErrorLogger::ErrorMessage::FileLocation loc;
loc.file = "ab/cd/../ef.h";
errmsg._callStack.push_back(loc);
ASSERT_EQUALS("<error file=\"ab/ef.h\" line=\"0\" id=\"\" severity=\"\" msg=\"\"/>", errmsg.toXML());
ASSERT_EQUALS("[ab/ef.h:0]: ", errmsg.toText());
}
}; };
REGISTER_TEST(TestCppcheck) REGISTER_TEST(TestCppcheck)