diff --git a/lib/path.cpp b/lib/path.cpp index ec1e8e0c6..363ae6a14 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -63,8 +63,7 @@ std::string Path::simplifyPath(std::string originalPath) const bool isUnc = originalPath.size() > 2 && originalPath[0] == '/' && originalPath[1] == '/'; // Remove ./, .//, ./// etc. at the beginning - if (originalPath.size() > 2 && originalPath[0] == '.' && - originalPath[1] == '/') { + while (originalPath.size() > 2 && originalPath[0] == '.' && originalPath[1] == '/') { // remove "./././" size_t toErase = 2; for (std::size_t i = 2; i < originalPath.size(); i++) { if (originalPath[i] == '/') diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 506af7f46..480abfe18 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -485,7 +485,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri errmsg.str(""); errmsg << "The code contains unhandled characters " << info << ". Checking continues, but do not expect valid results.\n" << "The code contains characters that are unhandled " << info << ". Neither unicode nor extended ASCII are supported. Checking continues, but do not expect valid results."; - writeError(filename, lineno, _errorLogger, "unhandledCharacters", errmsg.str()); + writeError(Path::simplifyPath(filename), lineno, _errorLogger, "unhandledCharacters", errmsg.str()); } if (_settings.terminated()) @@ -1914,7 +1914,7 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string // #error => return "" if (match && line.compare(0, 6, "#error") == 0) { if (!_settings.userDefines.empty() && !_settings._force) { - error(filenames.top(), lineno, line); + error(Path::simplifyPath(filenames.top()), lineno, line); } return ""; } @@ -3044,7 +3044,7 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file ++pos; if (pos >= line.size()) { - writeError(filename, + writeError(Path::simplifyPath(filename), linenr + tmpLinenr, errorLogger, "noQuoteCharPair", diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 2c97092c3..5ee9a6d88 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -300,6 +300,9 @@ private: TEST_CASE(invalid_ifs); // #5909 TEST_CASE(garbage); + + TEST_CASE(wrongPathOnUnicodeError); // see #6773 + TEST_CASE(wrongPathOnErrorDirective); } std::string preprocessorRead(const char* code) { @@ -3711,6 +3714,27 @@ private: std::map actual; preprocess(filedata, actual); } + + void wrongPathOnUnicodeError() { + const char filedata[] = "#file ././test.c\n" + "extern int 🌷;\n"; + preprocessorRead(filedata); + ASSERT_EQUALS("[test.c:2]: (error) The code contains unhandled characters (character code = 0xf0). Checking continues, but do not expect valid results.\n" + "[test.c:2]: (error) The code contains unhandled characters (character code = 0x9f). Checking continues, but do not expect valid results.\n" + "[test.c:2]: (error) The code contains unhandled characters (character code = 0x8c). Checking continues, but do not expect valid results.\n" + "[test.c:2]: (error) The code contains unhandled characters (character code = 0xb7). Checking continues, but do not expect valid results.\n", errout.str()); + } + + void wrongPathOnErrorDirective() { + errout.str(""); + Settings settings; + settings.userDefines = "foo"; + Preprocessor preprocessor(settings, this); + const std::string code("#error hello world!\n"); + preprocessor.getcode(code, "X", "./././test.c"); + ASSERT_EQUALS("[test.c:1]: (error) #error hello world!\n", errout.str()); + } + }; REGISTER_TEST(TestPreprocessor)