Skip to content

Issue #69442: Implemented fix for incorrect filedescriptor closing #1588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions ext/standard/proc_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,12 @@ PHP_FUNCTION(proc_open)
}
#endif

#if PHP_CAN_DO_PTS
if (dev_ptmx >= 0) {
close(dev_ptmx);
close(slave_pty);
}
#endif
/* close those descriptors that we just opened for the parent stuff,
* dup new descriptors into required descriptors and close the original
* cruft */
Expand All @@ -869,13 +875,6 @@ PHP_FUNCTION(proc_open)
close(descriptors[i].childend);
}

#if PHP_CAN_DO_PTS
if (dev_ptmx >= 0) {
close(dev_ptmx);
close(slave_pty);
}
#endif

if (cwd) {
php_ignore_value(chdir(cwd));
}
Expand Down
46 changes: 46 additions & 0 deletions ext/standard/tests/file/bug69442.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
proc_open with PTY closes incorrect file descriptor
--SKIPIF--
<?php

$code = <<< 'EOC'
<?php
$descriptors = array(array("pty"), array("pty"), array("pty"), array("pipe", "w"));
$pipes = array();
$process = proc_open('echo "foo";', $descriptors, $pipes);
EOC;

$tmpFile = tempnam(sys_get_temp_dir(), "bug69442");
file_put_contents($tmpFile, $code);

exec($_SERVER['TEST_PHP_EXECUTABLE']." ".$tmpFile." 2>&1", $output);
$output = join("\n", $output);
unlink($tmpFile);

if (strstr($output, "pty pseudo terminal not supported on this system") !== false) {
die("skip PTY pseudo terminals are not supported");
}
--FILE--
<?php
$cmd = '(echo "foo" ; exit 42;) 3>/dev/null; code=$?; echo $code >&3; exit $code';
$descriptors = array(array("pty"), array("pty"), array("pty"), array("pipe", "w"));
$pipes = array();

$process = proc_open($cmd, $descriptors, $pipes);

foreach ($pipes as $type => $pipe) {
$data = fread($pipe, 999);
echo 'type ' . $type . ' ';
var_dump($data);
fclose($pipe);
}
proc_close($process);
--EXPECT--
type 0 string(5) "foo
"
type 1 string(0) ""
type 2 string(0) ""
type 3 string(3) "42
"