Skip to content

Commit 472cdac

Browse files
committed
Initialize SECURITY_ATTRIBUTES only once in proc_open.c
Every time proc_open() is called on Windows, an identical SECURITY_ATTRIBUTES struct is initialized. It is never modified, just used to tell CreateProcessW() that subprocesses are allowed to inherit file handles from the parent. Just allocate one of them statically and be done with it.
1 parent e490bcf commit 472cdac

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

ext/standard/proc_open.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,15 @@ PHP_FUNCTION(proc_get_status)
353353

354354
/* {{{ handy definitions for portability/readability */
355355
#ifdef PHP_WIN32
356-
# define pipe(pair) (CreatePipe(&pair[0], &pair[1], &security, 0) ? 0 : -1)
356+
357+
/* we use this to allow child processes to inherit handles */
358+
SECURITY_ATTRIBUTES php_proc_open_security = {
359+
.nLength = sizeof(SECURITY_ATTRIBUTES),
360+
.bInheritHandle = TRUE,
361+
.lpSecurityDescriptor = NULL
362+
};
363+
364+
# define pipe(pair) (CreatePipe(&pair[0], &pair[1], &php_proc_open_security, 0) ? 0 : -1)
357365

358366
# define COMSPEC_NT "cmd.exe"
359367

@@ -539,7 +547,6 @@ PHP_FUNCTION(proc_open)
539547
HANDLE childHandle;
540548
STARTUPINFOW si;
541549
BOOL newprocok;
542-
SECURITY_ATTRIBUTES security;
543550
DWORD dwCreateFlags = 0;
544551
UINT old_error_mode;
545552
char cur_cwd[MAXPATHLEN];
@@ -631,14 +638,6 @@ PHP_FUNCTION(proc_open)
631638

632639
descriptors = alloc_descriptor_array(descriptorspec);
633640

634-
#ifdef PHP_WIN32
635-
/* we use this to allow the child to inherit handles */
636-
memset(&security, 0, sizeof(security));
637-
security.nLength = sizeof(security);
638-
security.bInheritHandle = TRUE;
639-
security.lpSecurityDescriptor = NULL;
640-
#endif
641-
642641
/* walk the descriptor spec and set up files/pipes */
643642
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(descriptorspec), nindex, str_index, descitem) {
644643
zval *ztype;
@@ -945,7 +944,8 @@ PHP_FUNCTION(proc_open)
945944
}
946945

947946
if (bypass_shell) {
948-
newprocok = CreateProcessW(NULL, cmdw, &security, &security, TRUE, dwCreateFlags, envpw, cwdw, &si, &pi);
947+
newprocok = CreateProcessW(NULL, cmdw, &php_proc_open_security, &php_proc_open_security,
948+
TRUE, dwCreateFlags, envpw, cwdw, &si, &pi);
949949
} else {
950950
int ret;
951951
size_t len;
@@ -966,7 +966,8 @@ PHP_FUNCTION(proc_open)
966966
goto exit_fail;
967967
}
968968

969-
newprocok = CreateProcessW(NULL, cmdw2, &security, &security, TRUE, dwCreateFlags, envpw, cwdw, &si, &pi);
969+
newprocok = CreateProcessW(NULL, cmdw2, &php_proc_open_security, &php_proc_open_security,
970+
TRUE, dwCreateFlags, envpw, cwdw, &si, &pi);
970971
free(cmdw2);
971972
}
972973

0 commit comments

Comments
 (0)