Skip to content

Commit b398558

Browse files
danmardmcppcheck
authored andcommitted
Fix #14749 (cmdFilename: handle more bash special characters)
1 parent 64cee51 commit b398558

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

‎lib/cppcheck.cpp‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,10 @@ namespace {
326326
};
327327
}
328328

329-
static std::string cmdFileName(std::string f)
329+
std::string CppCheck::cmdFileName(std::string f)
330330
{
331331
f = Path::toNativeSeparators(std::move(f));
332-
if (f.find(' ') != std::string::npos)
332+
if (f.find_first_of(" \t;$<>|&`\n") != std::string::npos)
333333
return "\"" + f + "\"";
334334
return f;
335335
}
@@ -464,11 +464,11 @@ static std::vector<picojson::value> executeAddon(const AddonInfo &addonInfo,
464464
std::string pythonExe;
465465

466466
if (!addonInfo.executable.empty())
467-
pythonExe = addonInfo.executable;
467+
pythonExe = CppCheck::cmdFileName(addonInfo.executable);
468468
else if (!addonInfo.python.empty())
469-
pythonExe = cmdFileName(addonInfo.python);
469+
pythonExe = CppCheck::cmdFileName(addonInfo.python);
470470
else if (!defaultPythonExe.empty())
471-
pythonExe = cmdFileName(defaultPythonExe);
471+
pythonExe = CppCheck::cmdFileName(defaultPythonExe);
472472
else {
473473
// store in static variable so we only look this up once - TODO: do not cache globally
474474
static const std::string detectedPythonExe = detectPython(executeCommand);
@@ -479,13 +479,13 @@ static std::vector<picojson::value> executeAddon(const AddonInfo &addonInfo,
479479

480480
std::string args;
481481
if (addonInfo.executable.empty())
482-
args = cmdFileName(addonInfo.runScript) + " " + cmdFileName(addonInfo.scriptFile);
482+
args = CppCheck::cmdFileName(addonInfo.runScript) + " " + CppCheck::cmdFileName(addonInfo.scriptFile);
483483
args += std::string(args.empty() ? "" : " ") + "--cli" + addonInfo.args;
484484
if (!premiumArgs.empty() && !addonInfo.executable.empty())
485485
args += " " + premiumArgs;
486486

487487
const bool is_file_list = (file.find(FILELIST) != std::string::npos);
488-
const std::string fileArg = (is_file_list ? " --file-list " : " ") + cmdFileName(file);
488+
const std::string fileArg = (is_file_list ? " --file-list " : " ") + CppCheck::cmdFileName(file);
489489
args += fileArg;
490490

491491
std::string result;
@@ -680,7 +680,7 @@ static std::string getClangFlags(const Settings& setting, Standards::Language la
680680
flags += getDefinesFlags(setting.userDefines);
681681

682682
for (const std::string &i: setting.userIncludes)
683-
flags += "--include " + cmdFileName(i) + " ";
683+
flags += "--include " + CppCheck::cmdFileName(i) + " ";
684684

685685
return flags;
686686
}

‎lib/cppcheck.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ class CPPCHECKLIB CppCheck {
144144
/** analyse whole program use .analyzeinfo files or ctuinfo string */
145145
unsigned int analyseWholeProgram(const std::string &buildDir, const std::list<FileWithDetails> &files, const std::list<FileSettings>& fileSettings, const std::string& ctuInfo);
146146

147+
/**
148+
*
149+
*/
150+
static std::string cmdFileName(std::string f);
151+
147152
private:
148153
void purgedConfigurationMessage(const std::string &file, const std::string& configuration);
149154

‎test/testcppcheck.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class TestCppcheck : public TestFixture {
102102
TEST_CASE(premiumResultsCache);
103103
TEST_CASE(purgedConfiguration);
104104
TEST_CASE(recheckInclude);
105+
TEST_CASE(cmdFileName);
105106
}
106107

107108
void getErrorMessages() const {
@@ -757,6 +758,18 @@ class TestCppcheck : public TestFixture {
757758
}
758759
}
759760

761+
void cmdFileName() const {
762+
ASSERT_EQUALS("x", CppCheck::cmdFileName("x"));
763+
ASSERT_EQUALS("\" \"", CppCheck::cmdFileName(" "));
764+
ASSERT_EQUALS("\"\t\"", CppCheck::cmdFileName("\t"));
765+
ASSERT_EQUALS("\";\"", CppCheck::cmdFileName(";"));
766+
ASSERT_EQUALS("\">\"", CppCheck::cmdFileName(">"));
767+
ASSERT_EQUALS("\"<\"", CppCheck::cmdFileName("<"));
768+
ASSERT_EQUALS("\"|\"", CppCheck::cmdFileName("|"));
769+
ASSERT_EQUALS("\"`\"", CppCheck::cmdFileName("`"));
770+
ASSERT_EQUALS("\"$\"", CppCheck::cmdFileName("$"));
771+
}
772+
760773
// TODO: test suppressions
761774
// TODO: test all with FS
762775
};

0 commit comments

Comments
 (0)