Skip to content

Commit 68ebfc8

Browse files
Sean-Derweltling
authored andcommitted
Fix bug #71624, PHP_MODE_PROCESS_STDIN (CLI SAPI called with '-R') did not properly set $argi and $argn
1 parent 84651b3 commit 68ebfc8

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

sapi/cli/php_cli.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -1018,16 +1018,15 @@ static int do_cli(int argc, char **argv) /* {{{ */
10181018
if (exec_begin && zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1) == FAILURE) {
10191019
exit_status=254;
10201020
}
1021-
ZVAL_LONG(&argi, index);
1022-
zend_hash_str_update(&EG(symbol_table), "argi", sizeof("argi")-1, &argi);
10231021
while (exit_status == SUCCESS && (input=php_stream_gets(s_in_process, NULL, 0)) != NULL) {
10241022
len = strlen(input);
10251023
while (len > 0 && len-- && (input[len]=='\n' || input[len]=='\r')) {
10261024
input[len] = '\0';
10271025
}
1028-
ZVAL_STRINGL(&argn, input, len);
1026+
ZVAL_STRINGL(&argn, input, len + 1);
10291027
zend_hash_str_update(&EG(symbol_table), "argn", sizeof("argn")-1, &argn);
1030-
Z_LVAL(argi) = ++index;
1028+
ZVAL_LONG(&argi, ++index);
1029+
zend_hash_str_update(&EG(symbol_table), "argi", sizeof("argi")-1, &argi);
10311030
if (exec_run) {
10321031
if (zend_eval_string_ex(exec_run, NULL, "Command line run code", 1) == FAILURE) {
10331032
exit_status=254;

sapi/cli/tests/bug71624.phpt

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
Bug #61977 Test that -R properly sets argi and argn
3+
--SKIPIF--
4+
<?php
5+
include "skipif.inc";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
$php = getenv('TEST_PHP_EXECUTABLE');
11+
12+
$filename_txt = dirname(__FILE__) . DIRECTORY_SEPARATOR . "bug71624.test.txt";
13+
14+
$txt = 'foo
15+
test
16+
hello
17+
';
18+
19+
file_put_contents($filename_txt, $txt);
20+
21+
$test_args = ['$argi', '$argn'];
22+
foreach ($test_args as $test_arg) {
23+
if (substr(PHP_OS, 0, 3) == 'WIN') {
24+
var_dump(`type "$filename_txt" | "$php" -n -R "echo $test_arg . PHP_EOL;"`);
25+
} else {
26+
var_dump(`cat "$filename_txt" | "$php" -n -R 'echo $test_arg . PHP_EOL;'`);
27+
}
28+
}
29+
30+
@unlink($filename_txt);
31+
32+
echo "Done\n";
33+
?>
34+
--EXPECT--
35+
string(6) "1
36+
2
37+
3
38+
"
39+
string(15) "foo
40+
test
41+
hello
42+
"
43+
Done

0 commit comments

Comments
 (0)