Closed
Description
Description
The following code:
<?php
$descriptor_spec = [
0=> [ "pipe", "r" ], // stdin is a pipe that the child will read from
1=> [ "pipe", "w" ], // stdout is a pipe that the child will write to
2=> [ "pipe", "w" ], // stderr is a file to write to
];
// NOTE: the array value is iterated by reference!
foreach ( $descriptor_spec as $fd => &$d )
{
// don't do anything, just the fact that we used "&$d" will sink the ship!
;
// my use case was validation of the array and adjusting some default values
}
$proc = proc_open( "/bin/true", $descriptor_spec, $pipes );
echo $proc === false ? "FAILED\n" : "SUCCEEDED\n";
Resulted in this output (v 7.4):
PHP Stack trace:
PHP 1. {main}() /tmp/scratch.php:0
PHP 2. proc_open($command = '/bin/true', $descriptorspec = [0 => [0 => 'pipe', 1 => 'r'], 1 => [0 => 'pipe', 1 => 'w'], 2 => [0 => 'pipe', 1 => 'w']], $pipes = NULL) /tmp/scratch.php:17
FAILED
Resulted in this output (v 8.x):
PHP Fatal error: Uncaught ValueError: proc_open(): Argument #2 ($descriptor_spec) must only contain arrays and streams in /tmp/scratch.php:17
Stack trace:
#0 /tmp/scratch.php(17): proc_open()
#1 {main}
thrown in /tmp/scratch.php on line 17
Process finished with exit code 255
But I expected this output instead:
-
SUCCEEDED
- no error at all, since descriptor_spec is a perfectly valid array containing valid arrays, as the output of v7.4 confirms
- all there was, I iterated over the array by reference!
If you leave away the "by-reference-amphersand", the code will succeed!
Since 3v4l.org does not support proc_open
, I've created a test code which I'll post in the first comment.
PHP Version
PHP 7.4.33 / 8.1.25 / 8.2.12
Operating System
Ubuntu 22.04.3 LTS