Skip to content

Commit 22bb464

Browse files
committed
Fix #14749 (cmdFilename: handle more bash special characters)
1 parent ff799b5 commit 22bb464

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
@@ -304,10 +304,10 @@ namespace {
304304
};
305305
}
306306

307-
static std::string cmdFileName(std::string f)
307+
std::string CppCheck::cmdFileName(std::string f)
308308
{
309309
f = Path::toNativeSeparators(std::move(f));
310-
if (f.find(' ') != std::string::npos)
310+
if (f.find_first_of(" \t;$<>|&`\n") != std::string::npos)
311311
return "\"" + f + "\"";
312312
return f;
313313
}
@@ -442,11 +442,11 @@ static std::vector<picojson::value> executeAddon(const AddonInfo &addonInfo,
442442
std::string pythonExe;
443443

444444
if (!addonInfo.executable.empty())
445-
pythonExe = addonInfo.executable;
445+
pythonExe = CppCheck::cmdFileName(addonInfo.executable);
446446
else if (!addonInfo.python.empty())
447-
pythonExe = cmdFileName(addonInfo.python);
447+
pythonExe = CppCheck::cmdFileName(addonInfo.python);
448448
else if (!defaultPythonExe.empty())
449-
pythonExe = cmdFileName(defaultPythonExe);
449+
pythonExe = CppCheck::cmdFileName(defaultPythonExe);
450450
else {
451451
// store in static variable so we only look this up once - TODO: do not cache globally
452452
static const std::string detectedPythonExe = detectPython(executeCommand);
@@ -457,13 +457,13 @@ static std::vector<picojson::value> executeAddon(const AddonInfo &addonInfo,
457457

458458
std::string args;
459459
if (addonInfo.executable.empty())
460-
args = cmdFileName(addonInfo.runScript) + " " + cmdFileName(addonInfo.scriptFile);
460+
args = CppCheck::cmdFileName(addonInfo.runScript) + " " + CppCheck::cmdFileName(addonInfo.scriptFile);
461461
args += std::string(args.empty() ? "" : " ") + "--cli" + addonInfo.args;
462462
if (!premiumArgs.empty() && !addonInfo.executable.empty())
463463
args += " " + premiumArgs;
464464

465465
const bool is_file_list = (file.find(FILELIST) != std::string::npos);
466-
const std::string fileArg = (is_file_list ? " --file-list " : " ") + cmdFileName(file);
466+
const std::string fileArg = (is_file_list ? " --file-list " : " ") + CppCheck::cmdFileName(file);
467467
args += fileArg;
468468

469469
std::string result;
@@ -658,7 +658,7 @@ static std::string getClangFlags(const Settings& setting, Standards::Language la
658658
flags += getDefinesFlags(setting.userDefines);
659659

660660
for (const std::string &i: setting.userIncludes)
661-
flags += "--include " + cmdFileName(i) + " ";
661+
flags += "--include " + CppCheck::cmdFileName(i) + " ";
662662

663663
return flags;
664664
}

lib/cppcheck.h

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

145+
/**
146+
*
147+
*/
148+
static std::string cmdFileName(std::string f);
149+
145150
private:
146151
void purgedConfigurationMessage(const std::string &file, const std::string& configuration);
147152

test/testcppcheck.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class TestCppcheck : public TestFixture {
8484
TEST_CASE(checkPlistOutput);
8585
TEST_CASE(premiumResultsCache);
8686
TEST_CASE(purgedConfiguration);
87+
TEST_CASE(cmdFileName);
8788
}
8889

8990
void getErrorMessages() const {
@@ -622,6 +623,18 @@ class TestCppcheck : public TestFixture {
622623
it->toString(false, templateFormat, ""));
623624
}
624625

626+
void cmdFileName() const {
627+
ASSERT_EQUALS("x", CppCheck::cmdFileName("x"));
628+
ASSERT_EQUALS("\" \"", CppCheck::cmdFileName(" "));
629+
ASSERT_EQUALS("\"\t\"", CppCheck::cmdFileName("\t"));
630+
ASSERT_EQUALS("\";\"", CppCheck::cmdFileName(";"));
631+
ASSERT_EQUALS("\">\"", CppCheck::cmdFileName(">"));
632+
ASSERT_EQUALS("\"<\"", CppCheck::cmdFileName("<"));
633+
ASSERT_EQUALS("\"|\"", CppCheck::cmdFileName("|"));
634+
ASSERT_EQUALS("\"`\"", CppCheck::cmdFileName("`"));
635+
ASSERT_EQUALS("\"$\"", CppCheck::cmdFileName("$"));
636+
}
637+
625638
// TODO: test suppressions
626639
// TODO: test all with FS
627640
};

0 commit comments

Comments
 (0)