Skip to content

Commit b0f9b8f

Browse files
committed
Introduce dup_proc_descriptor helper in proc_open.c
1 parent ddc2f39 commit b0f9b8f

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

ext/standard/proc_open.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,25 @@ static int set_proc_descriptor_to_file(struct php_proc_open_descriptor_item *des
622622
return SUCCESS;
623623
}
624624

625+
static int dup_proc_descriptor(php_file_descriptor_t from, php_file_descriptor_t *to, zend_ulong nindex)
626+
{
627+
#ifdef PHP_WIN32
628+
*to = dup_handle(from, TRUE, FALSE);
629+
if (*to == NULL) {
630+
php_error_docref(NULL, E_WARNING, "Failed to dup() for descriptor " ZEND_LONG_FMT, nindex);
631+
return FAILURE;
632+
}
633+
#else
634+
*to = dup(from);
635+
if (*to < 0) {
636+
php_error_docref(NULL, E_WARNING,
637+
"Failed to dup() for descriptor " ZEND_LONG_FMT " - %s", nindex, strerror(errno));
638+
return FAILURE;
639+
}
640+
#endif
641+
return SUCCESS;
642+
}
643+
625644
static int close_parent_ends_of_pipes_in_child(struct php_proc_open_descriptor_item *descriptors, int ndesc)
626645
{
627646
/* we are running in child process
@@ -909,22 +928,9 @@ PHP_FUNCTION(proc_open)
909928
#endif
910929
}
911930

912-
#ifdef PHP_WIN32
913-
descriptors[ndesc].childend = dup_handle(childend, TRUE, FALSE);
914-
if (descriptors[ndesc].childend == NULL) {
915-
php_error_docref(NULL, E_WARNING,
916-
"Failed to dup() for descriptor " ZEND_LONG_FMT, nindex);
931+
if (dup_proc_descriptor(childend, &descriptors[ndesc].childend, nindex) == FAILURE) {
917932
goto exit_fail;
918933
}
919-
#else
920-
descriptors[ndesc].childend = dup(childend);
921-
if (descriptors[ndesc].childend < 0) {
922-
php_error_docref(NULL, E_WARNING,
923-
"Failed to dup() for descriptor " ZEND_LONG_FMT " - %s",
924-
nindex, strerror(errno));
925-
goto exit_fail;
926-
}
927-
#endif
928934
} else if (strcmp(Z_STRVAL_P(ztype), "null") == 0) {
929935
if (set_proc_descriptor_to_blackhole(&descriptors[ndesc]) == FAILURE) {
930936
goto exit_fail;

0 commit comments

Comments
 (0)