Skip to content

Commit 8e09400

Browse files
committed
Introduce close_all_descriptors helper in proc_open.c
1 parent fe45722 commit 8e09400

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

ext/standard/proc_open.c

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,15 @@ static int set_proc_descriptor_to_pipe(struct php_proc_open_descriptor_item *des
570570
return SUCCESS;
571571
}
572572

573+
static void close_all_descriptors(struct php_proc_open_descriptor_item *descriptors, int ndesc)
574+
{
575+
for (int i = 0; i < ndesc; i++) {
576+
close_descriptor(descriptors[i].childend);
577+
if (descriptors[i].parentend)
578+
close_descriptor(descriptors[i].parentend);
579+
}
580+
}
581+
573582
static void efree_argv(char **argv)
574583
{
575584
if (argv) {
@@ -998,14 +1007,7 @@ PHP_FUNCTION(proc_open)
9981007

9991008
if (FALSE == newprocok) {
10001009
DWORD dw = GetLastError();
1001-
1002-
/* clean up all the descriptors */
1003-
for (i = 0; i < ndesc; i++) {
1004-
CloseHandle(descriptors[i].childend);
1005-
if (descriptors[i].parentend) {
1006-
CloseHandle(descriptors[i].parentend);
1007-
}
1008-
}
1010+
close_all_descriptors(descriptors, ndesc);
10091011
php_error_docref(NULL, E_WARNING, "CreateProcess failed, error code - %u", dw);
10101012
goto exit_fail;
10111013
}
@@ -1076,16 +1078,8 @@ PHP_FUNCTION(proc_open)
10761078

10771079
} else if (child < 0) {
10781080
/* failed to fork() */
1079-
1080-
/* clean up all the descriptors */
1081-
for (i = 0; i < ndesc; i++) {
1082-
close(descriptors[i].childend);
1083-
if (descriptors[i].parentend)
1084-
close(descriptors[i].parentend);
1085-
}
1086-
1081+
close_all_descriptors(descriptors, ndesc);
10871082
php_error_docref(NULL, E_WARNING, "Fork failed - %s", strerror(errno));
1088-
10891083
goto exit_fail;
10901084

10911085
}

0 commit comments

Comments
 (0)