diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 8fac158ec51..c75ee3a2ff0 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -326,8 +326,27 @@ namespace { }; } -static std::string cmdFileName(std::string f) +/** throw if filename contains characters that potentially has a special meaning for the shell */ +static void checkCmdFileName(const std::string& f) { + const auto badpos = f.find_first_of("\t\n\r;$<>|&`"); + if (badpos != std::string::npos) { + std::string c; + if (f[badpos] == '\n') + c = ""; + else if (f[badpos] == '\r') + c = ""; + else if (f[badpos] == '\t') + c = ""; + else + c += f[badpos]; + throw std::runtime_error("Cppcheck does not allow character " + c + " in filename " + f); + } +} + +std::string CppCheck::cmdFileName(std::string f) +{ + checkCmdFileName(f); f = Path::toNativeSeparators(std::move(f)); if (f.find(' ') != std::string::npos) return "\"" + f + "\""; @@ -463,12 +482,14 @@ static std::vector executeAddon(const AddonInfo &addonInfo, std::string pythonExe; - if (!addonInfo.executable.empty()) + if (!addonInfo.executable.empty()) { + // no quoting here, executeCommand quotes the executable if needed + checkCmdFileName(addonInfo.executable); pythonExe = addonInfo.executable; - else if (!addonInfo.python.empty()) - pythonExe = cmdFileName(addonInfo.python); + } else if (!addonInfo.python.empty()) + pythonExe = CppCheck::cmdFileName(addonInfo.python); else if (!defaultPythonExe.empty()) - pythonExe = cmdFileName(defaultPythonExe); + pythonExe = CppCheck::cmdFileName(defaultPythonExe); else { // store in static variable so we only look this up once - TODO: do not cache globally static const std::string detectedPythonExe = detectPython(executeCommand); @@ -479,13 +500,13 @@ static std::vector executeAddon(const AddonInfo &addonInfo, std::string args; if (addonInfo.executable.empty()) - args = cmdFileName(addonInfo.runScript) + " " + cmdFileName(addonInfo.scriptFile); + args = CppCheck::cmdFileName(addonInfo.runScript) + " " + CppCheck::cmdFileName(addonInfo.scriptFile); args += std::string(args.empty() ? "" : " ") + "--cli" + addonInfo.args; if (!premiumArgs.empty() && !addonInfo.executable.empty()) args += " " + premiumArgs; const bool is_file_list = (file.find(FILELIST) != std::string::npos); - const std::string fileArg = (is_file_list ? " --file-list " : " ") + cmdFileName(file); + const std::string fileArg = (is_file_list ? " --file-list " : " ") + CppCheck::cmdFileName(file); args += fileArg; std::string result; @@ -680,7 +701,7 @@ static std::string getClangFlags(const Settings& setting, Standards::Language la flags += getDefinesFlags(setting.userDefines); for (const std::string &i: setting.userIncludes) - flags += "--include " + cmdFileName(i) + " "; + flags += "--include " + CppCheck::cmdFileName(i) + " "; return flags; } diff --git a/lib/cppcheck.h b/lib/cppcheck.h index 4ccd68193ea..d524d668a51 100644 --- a/lib/cppcheck.h +++ b/lib/cppcheck.h @@ -144,6 +144,11 @@ class CPPCHECKLIB CppCheck { /** analyse whole program use .analyzeinfo files or ctuinfo string */ unsigned int analyseWholeProgram(const std::string &buildDir, const std::list &files, const std::list& fileSettings, const std::string& ctuInfo); + /** + * Return quoted filename string if input filename contains spaces or various other shell meta characters. + */ + static std::string cmdFileName(std::string f); + private: void purgedConfigurationMessage(const std::string &file, const std::string& configuration); diff --git a/test/testcppcheck.cpp b/test/testcppcheck.cpp index d88daceb0d9..031ecad4c5f 100644 --- a/test/testcppcheck.cpp +++ b/test/testcppcheck.cpp @@ -102,6 +102,7 @@ class TestCppcheck : public TestFixture { TEST_CASE(premiumResultsCache); TEST_CASE(purgedConfiguration); TEST_CASE(recheckInclude); + TEST_CASE(cmdFileName); } void getErrorMessages() const { @@ -757,6 +758,20 @@ class TestCppcheck : public TestFixture { } } + void cmdFileName() const { + ASSERT_EQUALS("x", CppCheck::cmdFileName("x")); + ASSERT_EQUALS("\" \"", CppCheck::cmdFileName(" ")); + ASSERT_THROW_EQUALS(CppCheck::cmdFileName("\t"), std::runtime_error, "Cppcheck does not allow character in filename \t"); + ASSERT_THROW_EQUALS(CppCheck::cmdFileName("\r"), std::runtime_error, "Cppcheck does not allow character in filename \r"); + ASSERT_THROW_EQUALS(CppCheck::cmdFileName("\n"), std::runtime_error, "Cppcheck does not allow character in filename \n"); + ASSERT_THROW_EQUALS(CppCheck::cmdFileName(";"), std::runtime_error, "Cppcheck does not allow character ; in filename ;"); + ASSERT_THROW_EQUALS(CppCheck::cmdFileName(">"), std::runtime_error, "Cppcheck does not allow character > in filename >"); + ASSERT_THROW_EQUALS(CppCheck::cmdFileName("<"), std::runtime_error, "Cppcheck does not allow character < in filename <"); + ASSERT_THROW_EQUALS(CppCheck::cmdFileName("|"), std::runtime_error, "Cppcheck does not allow character | in filename |"); + ASSERT_THROW_EQUALS(CppCheck::cmdFileName("`"), std::runtime_error, "Cppcheck does not allow character ` in filename `"); + ASSERT_THROW_EQUALS(CppCheck::cmdFileName("$"), std::runtime_error, "Cppcheck does not allow character $ in filename $"); + } + // TODO: test suppressions // TODO: test all with FS };