Skip to content

Commit 0524fbe

Browse files
committed
Introduce dup_proc_descriptor helper in proc_open.c
1 parent e0a051b commit 0524fbe

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
@@ -623,6 +623,25 @@ static int set_proc_descriptor_to_file(struct php_proc_open_descriptor_item *des
623623
return SUCCESS;
624624
}
625625

626+
static int dup_proc_descriptor(php_file_descriptor_t from, php_file_descriptor_t *to, zend_ulong nindex)
627+
{
628+
#ifdef PHP_WIN32
629+
*to = dup_handle(from, TRUE, FALSE);
630+
if (*to == NULL) {
631+
php_error_docref(NULL, E_WARNING, "Failed to dup() for descriptor " ZEND_LONG_FMT, nindex);
632+
return FAILURE;
633+
}
634+
#else
635+
*to = dup(from);
636+
if (*to < 0) {
637+
php_error_docref(NULL, E_WARNING,
638+
"Failed to dup() for descriptor " ZEND_LONG_FMT " - %s", nindex, strerror(errno));
639+
return FAILURE;
640+
}
641+
#endif
642+
return SUCCESS;
643+
}
644+
626645
static int close_parent_ends_of_pipes_in_child(struct php_proc_open_descriptor_item *descriptors, int ndesc)
627646
{
628647
/* 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)