Skip to content

Commit

Permalink
Fix argument parsing bug on "Run Command" dialog, PR #757.
Browse files Browse the repository at this point in the history
Current code fails when environment variable expanded result contains space (e.g. `%ProgramFiles%`).
  • Loading branch information
lifenjoiner authored and zufuliu committed Jan 24, 2024
1 parent 73cadae commit ca52161
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
7 changes: 4 additions & 3 deletions metapath/src/Dialogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,9 @@ INT_PTR CALLBACK RunDlgProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam)
WCHAR szFile[MAX_PATH * 2];

GetDlgItemText(hwnd, IDC_COMMANDLINE, szArgs, COUNTOF(szArgs));
ExpandEnvironmentStringsEx(szArgs, COUNTOF(szArgs));
ExtractFirstArgument(szArgs, szFile, szArg2);
ExpandEnvironmentStringsEx(szFile, COUNTOF(szFile));
ExpandEnvironmentStringsEx(szArg2, COUNTOF(szArg2));

WCHAR szTitle[32];
WCHAR szFilter[256];
Expand Down Expand Up @@ -302,13 +303,13 @@ INT_PTR CALLBACK RunDlgProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam)
ExtractFirstArgument(arg1 + 1, arg1, arg2);
DisplayPath(arg1, IDS_ERR_CMDLINE);
} else {
ExpandEnvironmentStringsEx(arg1, COUNTOF(arg1));
ExtractFirstArgument(arg1, arg1, arg2);
ExpandEnvironmentStringsEx(arg2, COUNTOF(arg2));

SHELLEXECUTEINFO sei;
memset(&sei, 0, sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = 0;
sei.fMask = SEE_MASK_DOENVSUBST;
sei.hwnd = hwnd;
sei.lpVerb = NULL;
sei.lpFile = arg1;
Expand Down
7 changes: 4 additions & 3 deletions src/Dialogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,9 @@ static INT_PTR CALLBACK RunDlgProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM l
WCHAR szFile[MAX_PATH * 2];

GetDlgItemText(hwnd, IDC_COMMANDLINE, szArgs, COUNTOF(szArgs));
ExpandEnvironmentStringsEx(szArgs, COUNTOF(szArgs));
ExtractFirstArgument(szArgs, szFile, szArg2);
ExpandEnvironmentStringsEx(szFile, COUNTOF(szFile));
ExpandEnvironmentStringsEx(szArg2, COUNTOF(szArg2));

WCHAR szFilter[256];
GetString(IDS_FILTER_EXE, szFilter, COUNTOF(szFilter));
Expand Down Expand Up @@ -501,8 +502,8 @@ static INT_PTR CALLBACK RunDlgProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM l
bool bQuickExit = false;
WCHAR arg2[MAX_PATH];

ExpandEnvironmentStringsEx(arg1, COUNTOF(arg1));
ExtractFirstArgument(arg1, arg1, arg2);
ExpandEnvironmentStringsEx(arg2, COUNTOF(arg2));

if (StrCaseEqual(arg1, L"notepad2") || StrCaseEqual(arg1, L"notepad2.exe")) {
GetModuleFileName(NULL, arg1, COUNTOF(arg1));
Expand All @@ -518,7 +519,7 @@ static INT_PTR CALLBACK RunDlgProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM l
SHELLEXECUTEINFO sei;
memset(&sei, 0, sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = 0;
sei.fMask = SEE_MASK_DOENVSUBST;
sei.hwnd = hwnd;
sei.lpVerb = NULL;
sei.lpFile = arg1;
Expand Down
4 changes: 2 additions & 2 deletions src/Edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -6930,10 +6930,10 @@ void EditSelectionAction(int action) {
LPWSTR lpszCommand = (LPWSTR)NP2HeapAlloc(sizeof(WCHAR) * (cchEscapedW + COUNTOF(szCmdTemplate) + MAX_PATH + 32));
const size_t cbCommand = NP2HeapSize(lpszCommand);
wsprintf(lpszCommand, szCmdTemplate, pszEscapedW);
ExpandEnvironmentStringsEx(lpszCommand, (DWORD)(cbCommand / sizeof(WCHAR)));

LPWSTR lpszArgs = (LPWSTR)NP2HeapAlloc(cbCommand);
ExtractFirstArgument(lpszCommand, lpszCommand, lpszArgs);
ExpandEnvironmentStringsEx(lpszArgs, (DWORD)(cbCommand / sizeof(WCHAR)));

WCHAR wchDirectory[MAX_PATH] = L"";
if (StrNotEmpty(szCurFile)) {
Expand All @@ -6944,7 +6944,7 @@ void EditSelectionAction(int action) {
SHELLEXECUTEINFO sei;
memset(&sei, 0, sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = SEE_MASK_NOZONECHECKS;
sei.fMask = SEE_MASK_DOENVSUBST | SEE_MASK_NOZONECHECKS;
sei.hwnd = NULL;
sei.lpVerb = NULL;
sei.lpFile = lpszCommand;
Expand Down

0 comments on commit ca52161

Please sign in to comment.