Skip to content

Commit e7f23e1

Browse files
committed
Implemented fix for incorrect filedescriptor closing
1 parent ccc95bf commit e7f23e1

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
@@ -854,6 +854,12 @@ PHP_FUNCTION(proc_open)
854854
}
855855
#endif
856856

857+
#if PHP_CAN_DO_PTS
858+
if (dev_ptmx >= 0) {
859+
close(dev_ptmx);
860+
close(slave_pty);
861+
}
862+
#endif
857863
/* close those descriptors that we just opened for the parent stuff,
858864
* dup new descriptors into required descriptors and close the original
859865
* cruft */
@@ -869,13 +875,6 @@ PHP_FUNCTION(proc_open)
869875
close(descriptors[i].childend);
870876
}
871877

872-
#if PHP_CAN_DO_PTS
873-
if (dev_ptmx >= 0) {
874-
close(dev_ptmx);
875-
close(slave_pty);
876-
}
877-
#endif
878-
879878
if (cwd) {
880879
php_ignore_value(chdir(cwd));
881880
}

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)