Skip to content

Commit 7d76eef

Browse files
committed
Fix #14749 (cmdFilename: handle more bash special characters)
1 parent 3a9750e commit 7d76eef

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
@@ -318,10 +318,10 @@ namespace {
318318
};
319319
}
320320

321-
static std::string cmdFileName(std::string f)
321+
std::string CppCheck::cmdFileName(std::string f)
322322
{
323323
f = Path::toNativeSeparators(std::move(f));
324-
if (f.find(' ') != std::string::npos)
324+
if (f.find_first_of(" \t;$<>|&`\n") != std::string::npos)
325325
return "\"" + f + "\"";
326326
return f;
327327
}
@@ -456,11 +456,11 @@ static std::vector<picojson::value> executeAddon(const AddonInfo &addonInfo,
456456
std::string pythonExe;
457457

458458
if (!addonInfo.executable.empty())
459-
pythonExe = addonInfo.executable;
459+
pythonExe = CppCheck::cmdFileName(addonInfo.executable);
460460
else if (!addonInfo.python.empty())
461-
pythonExe = cmdFileName(addonInfo.python);
461+
pythonExe = CppCheck::cmdFileName(addonInfo.python);
462462
else if (!defaultPythonExe.empty())
463-
pythonExe = cmdFileName(defaultPythonExe);
463+
pythonExe = CppCheck::cmdFileName(defaultPythonExe);
464464
else {
465465
// store in static variable so we only look this up once - TODO: do not cache globally
466466
static const std::string detectedPythonExe = detectPython(executeCommand);
@@ -471,13 +471,13 @@ static std::vector<picojson::value> executeAddon(const AddonInfo &addonInfo,
471471

472472
std::string args;
473473
if (addonInfo.executable.empty())
474-
args = cmdFileName(addonInfo.runScript) + " " + cmdFileName(addonInfo.scriptFile);
474+
args = CppCheck::cmdFileName(addonInfo.runScript) + " " + CppCheck::cmdFileName(addonInfo.scriptFile);
475475
args += std::string(args.empty() ? "" : " ") + "--cli" + addonInfo.args;
476476
if (!premiumArgs.empty() && !addonInfo.executable.empty())
477477
args += " " + premiumArgs;
478478

479479
const bool is_file_list = (file.find(FILELIST) != std::string::npos);
480-
const std::string fileArg = (is_file_list ? " --file-list " : " ") + cmdFileName(file);
480+
const std::string fileArg = (is_file_list ? " --file-list " : " ") + CppCheck::cmdFileName(file);
481481
args += fileArg;
482482

483483
std::string result;
@@ -672,7 +672,7 @@ static std::string getClangFlags(const Settings& setting, Standards::Language la
672672
flags += getDefinesFlags(setting.userDefines);
673673

674674
for (const std::string &i: setting.userIncludes)
675-
flags += "--include " + cmdFileName(i) + " ";
675+
flags += "--include " + CppCheck::cmdFileName(i) + " ";
676676

677677
return flags;
678678
}

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)