Skip to content

Commit

Permalink
Fix the issue where the following string containing double quotes is …
Browse files Browse the repository at this point in the history
…not interpreted correctly when specified to the /unpacker command line argument.

~~~
/unpacker "Replace ""a"" ""b"""
~~~~
  • Loading branch information
sdottaka committed Jan 3, 2024
1 parent f8a3342 commit a5d3a4b
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
32 changes: 30 additions & 2 deletions Src/MergeCmdLineInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,36 @@ const tchar_t *MergeCmdLineInfo::EatParam(const tchar_t *p, String &param, bool
param = strutils::makelower(param);
}
// Strip any leading or trailing whitespace or quotes
param.erase(0, param.find_first_not_of(_T(" \t\r\n\"")));
param.erase(param.find_last_not_of(_T(" \t\r\n\"")) + 1);
param.erase(0, param.find_first_not_of(_T(" \t\r\n")));
param.erase(param.find_last_not_of(_T(" \t\r\n")) + 1);
if (!param.empty() && param.front() == '"')
{
param = ([](const String& param) -> String
{
String result;
bool inquotes = false;
for (int i = 0; i < param.length(); ++i)
{
if (param[i] == '"')
{
if (inquotes && i < param.length() - 1 && param[i + 1] == '"')
{
result += '"';
++i;
}
else
{
inquotes = !inquotes;
}
}
else
{
result += param[i];
}
}
return result;
})(param);
}
return q;
}

Expand Down
68 changes: 68 additions & 0 deletions Testing/GoogleTest/CmdLine/MergeCmdLine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2048,6 +2048,74 @@ namespace
EXPECT_EQ(_T(""), cmdInfo.m_sReportFile);
EXPECT_EQ(_T(""), cmdInfo.m_sIniFilepath);
}
TEST_F(MergeCmdLineInfoTest, Unpacker2)
{
MergeCmdLineInfo cmdInfo(_T("C:\\WinMerge\\WinMerge.exe /unpacker \"Replace -e ',\\s*\\n\\s*}' '}' -e ',\\s*\\n\\s*\\]' ']' | QueryJSON '.Parameters^|=sort_by(.HandleId)'\""));
EXPECT_EQ(0, cmdInfo.m_Files.GetSize());
EXPECT_EQ(MergeCmdLineInfo::SHOWNORMAL, cmdInfo.m_nCmdShow);
EXPECT_FALSE(cmdInfo.m_bEscShutdown);
EXPECT_FALSE(cmdInfo.m_bExitIfNoDiff);
EXPECT_FALSE(cmdInfo.m_bRecurse);
EXPECT_FALSE(cmdInfo.m_bNonInteractive);
EXPECT_FALSE(cmdInfo.m_nSingleInstance.has_value());
EXPECT_FALSE(cmdInfo.m_bShowUsage);
EXPECT_FALSE(cmdInfo.m_bSelfCompare);
EXPECT_FALSE(cmdInfo.m_bNewCompare);
EXPECT_EQ(MergeCmdLineInfo::AUTOMATIC, cmdInfo.m_nWindowType);
EXPECT_FALSE(cmdInfo.m_nCompMethod.has_value());
EXPECT_FALSE(cmdInfo.m_cTableDelimiter.has_value());
EXPECT_FALSE(cmdInfo.m_cTableQuote.has_value());
EXPECT_FALSE(cmdInfo.m_bTableAllowNewlinesInQuotes.has_value());
EXPECT_EQ(0,cmdInfo.m_nCodepage);
EXPECT_EQ(-1, cmdInfo.m_nLineIndex);
EXPECT_EQ(FFILEOPEN_NONE, cmdInfo.m_dwLeftFlags);
EXPECT_EQ(FFILEOPEN_NONE, cmdInfo.m_dwMiddleFlags);
EXPECT_EQ(FFILEOPEN_NONE, cmdInfo.m_dwRightFlags);
EXPECT_EQ(_T(""), cmdInfo.m_sLeftDesc);
EXPECT_EQ(_T(""), cmdInfo.m_sMiddleDesc);
EXPECT_EQ(_T(""), cmdInfo.m_sRightDesc);
EXPECT_EQ(_T(""), cmdInfo.m_sFileFilter);
EXPECT_EQ(_T(""), cmdInfo.m_sPreDiffer);
EXPECT_EQ(_T("Replace -e ',\\s*\\n\\s*}' '}' -e ',\\s*\\n\\s*\\]' ']' | QueryJSON '.Parameters^|=sort_by(.HandleId)'"), cmdInfo.m_sUnpacker);
EXPECT_EQ(_T(""), cmdInfo.m_sFileExt);
EXPECT_EQ(_T(""), cmdInfo.m_sOutputpath);
EXPECT_EQ(_T(""), cmdInfo.m_sReportFile);
EXPECT_EQ(_T(""), cmdInfo.m_sIniFilepath);
}
TEST_F(MergeCmdLineInfoTest, Unpacker3)
{
MergeCmdLineInfo cmdInfo(_T("C:\\WinMerge\\WinMerge.exe /unpacker \"Replace -e \"\",\\s*\\n\\s*}\"\" \"\"}\"\" -e \"\",\\s*\\n\\s*\\]\"\" \"\"]\"\" | QueryJSON \"\".Parameters|=sort_by(.HandleId)\"\"\""));
EXPECT_EQ(0, cmdInfo.m_Files.GetSize());
EXPECT_EQ(MergeCmdLineInfo::SHOWNORMAL, cmdInfo.m_nCmdShow);
EXPECT_FALSE(cmdInfo.m_bEscShutdown);
EXPECT_FALSE(cmdInfo.m_bExitIfNoDiff);
EXPECT_FALSE(cmdInfo.m_bRecurse);
EXPECT_FALSE(cmdInfo.m_bNonInteractive);
EXPECT_FALSE(cmdInfo.m_nSingleInstance.has_value());
EXPECT_FALSE(cmdInfo.m_bShowUsage);
EXPECT_FALSE(cmdInfo.m_bSelfCompare);
EXPECT_FALSE(cmdInfo.m_bNewCompare);
EXPECT_EQ(MergeCmdLineInfo::AUTOMATIC, cmdInfo.m_nWindowType);
EXPECT_FALSE(cmdInfo.m_nCompMethod.has_value());
EXPECT_FALSE(cmdInfo.m_cTableDelimiter.has_value());
EXPECT_FALSE(cmdInfo.m_cTableQuote.has_value());
EXPECT_FALSE(cmdInfo.m_bTableAllowNewlinesInQuotes.has_value());
EXPECT_EQ(0,cmdInfo.m_nCodepage);
EXPECT_EQ(-1, cmdInfo.m_nLineIndex);
EXPECT_EQ(FFILEOPEN_NONE, cmdInfo.m_dwLeftFlags);
EXPECT_EQ(FFILEOPEN_NONE, cmdInfo.m_dwMiddleFlags);
EXPECT_EQ(FFILEOPEN_NONE, cmdInfo.m_dwRightFlags);
EXPECT_EQ(_T(""), cmdInfo.m_sLeftDesc);
EXPECT_EQ(_T(""), cmdInfo.m_sMiddleDesc);
EXPECT_EQ(_T(""), cmdInfo.m_sRightDesc);
EXPECT_EQ(_T(""), cmdInfo.m_sFileFilter);
EXPECT_EQ(_T(""), cmdInfo.m_sPreDiffer);
EXPECT_EQ(_T("Replace -e \",\\s*\\n\\s*}\" \"}\" -e \",\\s*\\n\\s*\\]\" \"]\" | QueryJSON \".Parameters|=sort_by(.HandleId)\""), cmdInfo.m_sUnpacker);
EXPECT_EQ(_T(""), cmdInfo.m_sFileExt);
EXPECT_EQ(_T(""), cmdInfo.m_sOutputpath);
EXPECT_EQ(_T(""), cmdInfo.m_sReportFile);
EXPECT_EQ(_T(""), cmdInfo.m_sIniFilepath);
}

// Prediffer
TEST_F(MergeCmdLineInfoTest, Prediffer1)
Expand Down

0 comments on commit a5d3a4b

Please sign in to comment.