Skip to content

Commit 79ffaae

Browse files
committed
Introduce convert_command_to_use_shell helper in proc_open.c
1 parent 2ffaeee commit 79ffaae

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

ext/standard/proc_open.c

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,28 @@ static void init_startup_info(STARTUPINFOW *si, struct php_proc_open_descriptor_
507507
}
508508
}
509509
}
510+
511+
static int convert_command_to_use_shell(wchar_t **cmdw, size_t cmdw_len)
512+
{
513+
size_t len = sizeof(COMSPEC_NT) + sizeof(" /s /c ") + cmdw_len + 3;
514+
wchar_t *cmdw_shell = (wchar_t *)malloc(len * sizeof(wchar_t));
515+
516+
if (cmdw_shell == NULL) {
517+
php_error_docref(NULL, E_WARNING, "Command conversion failed");
518+
return FAILURE;
519+
}
520+
521+
if (_snwprintf(cmdw_shell, len, L"%hs /s /c \"%s\"", COMSPEC_NT, *cmdw) == -1) {
522+
free(cmdw_shell);
523+
php_error_docref(NULL, E_WARNING, "Command conversion failed");
524+
return FAILURE;
525+
}
526+
527+
free(*cmdw);
528+
*cmdw = cmdw_shell;
529+
530+
return SUCCESS;
531+
}
510532
#endif
511533

512534
static struct php_proc_open_descriptor_item* alloc_descriptor_array(zval *descriptorspec)
@@ -650,7 +672,7 @@ PHP_FUNCTION(proc_open)
650672
UINT old_error_mode;
651673
char cur_cwd[MAXPATHLEN];
652674
wchar_t *cmdw = NULL, *cwdw = NULL, *envpw = NULL;
653-
size_t tmp_len;
675+
size_t cmdw_len;
654676
int suppress_errors = 0;
655677
int bypass_shell = 0;
656678
int blocking_pipes = 0;
@@ -968,39 +990,19 @@ PHP_FUNCTION(proc_open)
968990
}
969991
}
970992

971-
cmdw = php_win32_cp_conv_any_to_w(command, strlen(command), &tmp_len);
993+
cmdw = php_win32_cp_conv_any_to_w(command, strlen(command), &cmdw_len);
972994
if (!cmdw) {
973995
php_error_docref(NULL, E_WARNING, "Command conversion failed");
974996
goto exit_fail;
975997
}
976998

977-
if (bypass_shell) {
978-
newprocok = CreateProcessW(NULL, cmdw, &php_proc_open_security, &php_proc_open_security,
979-
TRUE, dwCreateFlags, envpw, cwdw, &si, &pi);
980-
} else {
981-
int ret;
982-
size_t len;
983-
wchar_t *cmdw2;
984-
985-
986-
len = (sizeof(COMSPEC_NT) + sizeof(" /s /c ") + tmp_len + 3);
987-
cmdw2 = (wchar_t *)malloc(len * sizeof(wchar_t));
988-
if (!cmdw2) {
989-
php_error_docref(NULL, E_WARNING, "Command conversion failed");
999+
if (!bypass_shell) {
1000+
if (convert_command_to_use_shell(&cmdw, cmdw_len) == FAILURE) {
9901001
goto exit_fail;
9911002
}
992-
ret = _snwprintf(cmdw2, len, L"%hs /s /c \"%s\"", COMSPEC_NT, cmdw);
993-
994-
if (-1 == ret) {
995-
free(cmdw2);
996-
php_error_docref(NULL, E_WARNING, "Command conversion failed");
997-
goto exit_fail;
998-
}
999-
1000-
newprocok = CreateProcessW(NULL, cmdw2, &php_proc_open_security, &php_proc_open_security,
1001-
TRUE, dwCreateFlags, envpw, cwdw, &si, &pi);
1002-
free(cmdw2);
10031003
}
1004+
newprocok = CreateProcessW(NULL, cmdw, &php_proc_open_security, &php_proc_open_security,
1005+
TRUE, dwCreateFlags, envpw, cwdw, &si, &pi);
10041006

10051007
free(cwdw);
10061008
cwdw = NULL;

0 commit comments

Comments
 (0)