Skip to content

Commit 69b1526

Browse files
committed
Merge branch 'PHP-7.0' into PHP-7.1
* PHP-7.0: Fixed #69442 closing of fd incorrect when PTS enabled
2 parents 811dfaa + ff6b309 commit 69b1526

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

ext/standard/proc_open.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,12 @@ PHP_FUNCTION(proc_open)
865865
}
866866
#endif
867867

868+
#if PHP_CAN_DO_PTS
869+
if (dev_ptmx >= 0) {
870+
close(dev_ptmx);
871+
close(slave_pty);
872+
}
873+
#endif
868874
/* close those descriptors that we just opened for the parent stuff,
869875
* dup new descriptors into required descriptors and close the original
870876
* cruft */
@@ -880,13 +886,6 @@ PHP_FUNCTION(proc_open)
880886
close(descriptors[i].childend);
881887
}
882888

883-
#if PHP_CAN_DO_PTS
884-
if (dev_ptmx >= 0) {
885-
close(dev_ptmx);
886-
close(slave_pty);
887-
}
888-
#endif
889-
890889
if (cwd) {
891890
php_ignore_value(chdir(cwd));
892891
}

ext/standard/tests/file/bug69442.phpt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
proc_open with PTY closes incorrect file descriptor
3+
--SKIPIF--
4+
<?php
5+
6+
$code = <<< 'EOC'
7+
<?php
8+
$descriptors = array(array("pty"), array("pty"), array("pty"), array("pipe", "w"));
9+
$pipes = array();
10+
$process = proc_open('echo "foo";', $descriptors, $pipes);
11+
EOC;
12+
13+
$tmpFile = tempnam(sys_get_temp_dir(), "bug69442");
14+
file_put_contents($tmpFile, $code);
15+
16+
exec($_SERVER['TEST_PHP_EXECUTABLE']." ".$tmpFile." 2>&1", $output);
17+
$output = join("\n", $output);
18+
unlink($tmpFile);
19+
20+
if (strstr($output, "pty pseudo terminal not supported on this system") !== false) {
21+
die("skip PTY pseudo terminals are not supported");
22+
}
23+
--FILE--
24+
<?php
25+
$cmd = '(echo "foo" ; exit 42;) 3>/dev/null; code=$?; echo $code >&3; exit $code';
26+
$descriptors = array(array("pty"), array("pty"), array("pty"), array("pipe", "w"));
27+
$pipes = array();
28+
29+
$process = proc_open($cmd, $descriptors, $pipes);
30+
31+
foreach ($pipes as $type => $pipe) {
32+
$data = fread($pipe, 999);
33+
echo 'type ' . $type . ' ';
34+
var_dump($data);
35+
fclose($pipe);
36+
}
37+
proc_close($process);
38+
--EXPECT--
39+
type 0 string(5) "foo
40+
"
41+
type 1 string(0) ""
42+
type 2 string(0) ""
43+
type 3 string(3) "42
44+
"
45+
46+

0 commit comments

Comments
 (0)