Skip to content

Commit caab1e6

Browse files
committed
Fixed #9542 (Better error handling for --addon)
1 parent 68a67a9 commit caab1e6

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

lib/cppcheck.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,49 @@ namespace {
137137

138138
static std::string executeAddon(const AddonInfo &addonInfo, const std::string &dumpFile)
139139
{
140-
const std::string cmd = "python \"" + addonInfo.scriptFile + "\" --cli" + addonInfo.args + " \"" + dumpFile + "\"";
140+
// Can python be executed?
141+
{
142+
const std::string cmd = "python --version 2>&1";
143+
144+
#ifdef _WIN32
145+
std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd.c_str(), "r"), _pclose);
146+
#else
147+
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
148+
#endif
149+
if (!pipe)
150+
throw InternalError(nullptr, "popen failed (command: '" + cmd + "')");
151+
char buffer[1024];
152+
std::string result;
153+
while (fgets(buffer, sizeof(buffer), pipe.get()) != nullptr)
154+
result += buffer;
155+
if (result.compare(0, 7, "Python ", 0, 7) != 0 || result.size() > 50)
156+
throw InternalError(nullptr, "Failed to execute '" + cmd + "' (" + result + ")");
157+
}
158+
159+
const std::string cmd = "python \"" + addonInfo.scriptFile + "\" --cli" + addonInfo.args + " \"" + dumpFile + "\" 2>&1";
141160

142161
#ifdef _WIN32
143162
std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd.c_str(), "r"), _pclose);
144163
#else
145164
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
146165
#endif
147166
if (!pipe)
148-
return "";
167+
throw InternalError(nullptr, "popen failed (command: '" + cmd + "')");
149168
char buffer[1024];
150169
std::string result;
151170
while (fgets(buffer, sizeof(buffer), pipe.get()) != nullptr) {
152171
result += buffer;
153172
}
173+
174+
// Validate output..
175+
std::istringstream istr(result);
176+
std::string line;
177+
while (std::getline(istr, line)) {
178+
if (line.compare(0,9,"Checking ", 0, 9) != 0 && !line.empty() && line[0] != '{')
179+
throw InternalError(nullptr, "Failed to execute '" + cmd + "'. " + result);
180+
}
181+
182+
// Valid results
154183
return result;
155184
}
156185

0 commit comments

Comments
 (0)