From caab1e61bb93c74d9c4e71d371d0a94f34f7089f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 17 Dec 2019 12:00:01 +0100 Subject: [PATCH] Fixed #9542 (Better error handling for --addon) --- lib/cppcheck.cpp | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index a362799a1..023ad23f0 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -137,7 +137,26 @@ namespace { static std::string executeAddon(const AddonInfo &addonInfo, const std::string &dumpFile) { - const std::string cmd = "python \"" + addonInfo.scriptFile + "\" --cli" + addonInfo.args + " \"" + dumpFile + "\""; + // Can python be executed? + { + const std::string cmd = "python --version 2>&1"; + +#ifdef _WIN32 + std::unique_ptr pipe(_popen(cmd.c_str(), "r"), _pclose); +#else + std::unique_ptr pipe(popen(cmd.c_str(), "r"), pclose); +#endif + if (!pipe) + throw InternalError(nullptr, "popen failed (command: '" + cmd + "')"); + char buffer[1024]; + std::string result; + while (fgets(buffer, sizeof(buffer), pipe.get()) != nullptr) + result += buffer; + if (result.compare(0, 7, "Python ", 0, 7) != 0 || result.size() > 50) + throw InternalError(nullptr, "Failed to execute '" + cmd + "' (" + result + ")"); + } + + const std::string cmd = "python \"" + addonInfo.scriptFile + "\" --cli" + addonInfo.args + " \"" + dumpFile + "\" 2>&1"; #ifdef _WIN32 std::unique_ptr pipe(_popen(cmd.c_str(), "r"), _pclose); @@ -145,12 +164,22 @@ static std::string executeAddon(const AddonInfo &addonInfo, const std::string &d std::unique_ptr pipe(popen(cmd.c_str(), "r"), pclose); #endif if (!pipe) - return ""; + throw InternalError(nullptr, "popen failed (command: '" + cmd + "')"); char buffer[1024]; std::string result; while (fgets(buffer, sizeof(buffer), pipe.get()) != nullptr) { result += buffer; } + + // Validate output.. + std::istringstream istr(result); + std::string line; + while (std::getline(istr, line)) { + if (line.compare(0,9,"Checking ", 0, 9) != 0 && !line.empty() && line[0] != '{') + throw InternalError(nullptr, "Failed to execute '" + cmd + "'. " + result); + } + + // Valid results return result; }