Skip to content

Commit 6086512

Browse files
committed
main: Deprecate deriving $_SERVER['argc'] and $_SERVER['argv'] from the query string
RFC: https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_register_argc_argv_ini_directive
1 parent ca4a841 commit 6086512

8 files changed

+181
-18
lines changed

main/php_variables.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -785,10 +785,13 @@ static void php_autoglobal_merge(HashTable *dest, HashTable *src)
785785
PHPAPI zend_result php_hash_environment(void)
786786
{
787787
memset(PG(http_globals), 0, sizeof(PG(http_globals)));
788+
/* Register $argc and $argv for CLI SAPIs. $_SERVER['argc'] and $_SERVER['argv']
789+
* will be registered in php_auto_globals_create_server() which clears
790+
* PG(http_globals)[TRACK_VARS_SERVER] anyways, making registration at this point
791+
* useless.
792+
*/
793+
php_build_argv(NULL, NULL);
788794
zend_activate_auto_globals();
789-
if (PG(register_argc_argv)) {
790-
php_build_argv(SG(request_info).query_string, &PG(http_globals)[TRACK_VARS_SERVER]);
791-
}
792795
return SUCCESS;
793796
}
794797
/* }}} */
@@ -875,19 +878,18 @@ static bool php_auto_globals_create_server(zend_string *name)
875878
if (PG(variables_order) && (strchr(PG(variables_order),'S') || strchr(PG(variables_order),'s'))) {
876879
php_register_server_variables();
877880

878-
if (PG(register_argc_argv)) {
879-
if (SG(request_info).argc) {
880-
zval *argc, *argv;
881+
if (SG(request_info).argc) {
882+
zval *argc, *argv;
881883

882-
if ((argc = zend_hash_find_ex_ind(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGC), 1)) != NULL &&
883-
(argv = zend_hash_find_ex_ind(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGV), 1)) != NULL) {
884-
Z_ADDREF_P(argv);
885-
zend_hash_update(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), ZSTR_KNOWN(ZEND_STR_ARGV), argv);
886-
zend_hash_update(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), ZSTR_KNOWN(ZEND_STR_ARGC), argc);
887-
}
888-
} else {
889-
php_build_argv(SG(request_info).query_string, &PG(http_globals)[TRACK_VARS_SERVER]);
884+
if ((argc = zend_hash_find_ex_ind(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGC), 1)) != NULL &&
885+
(argv = zend_hash_find_ex_ind(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGV), 1)) != NULL) {
886+
Z_ADDREF_P(argv);
887+
zend_hash_update(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), ZSTR_KNOWN(ZEND_STR_ARGV), argv);
888+
zend_hash_update(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), ZSTR_KNOWN(ZEND_STR_ARGC), argc);
890889
}
890+
} else if (PG(register_argc_argv)) {
891+
zend_error(E_DEPRECATED, "Deriving $_SERVER['argc'] and $_SERVER['argv'] from $_SERVER['QUERY_STRING'] is deprecated, configure register_argc_argv=0 to suppress this message and access the query parameters via $_SERVER['QUERY_STRING'] or $_GET");
892+
php_build_argv(SG(request_info).query_string, &PG(http_globals)[TRACK_VARS_SERVER]);
891893
}
892894

893895
} else {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
--TEST--
2+
FPM: bug75712 - getenv should not read from $_ENV and $_SERVER
3+
--SKIPIF--
4+
<?php include "skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
8+
require_once "tester.inc";
9+
10+
$cfg = <<<EOT
11+
[global]
12+
error_log = {{FILE:LOG}}
13+
[unconfined]
14+
listen = {{ADDR}}
15+
pm = static
16+
pm.max_children = 1
17+
env[TEST] = test
18+
php_value[register_argc_argv] = on
19+
php_value[html_errors] = off
20+
EOT;
21+
22+
$code = <<<EOT
23+
<?php
24+
25+
var_dump(isset(getenv()['argv']));
26+
var_dump(isset(getenv()['SERVER_NAME']));
27+
var_dump(getenv()['TEST']);
28+
var_dump(isset(getenv()['DTEST']));
29+
var_dump(getenv('DTEST'));
30+
putenv('DTEST=dt');
31+
var_dump(getenv()['DTEST']);
32+
var_dump(getenv('DTEST'));
33+
34+
function notcalled()
35+
{
36+
\$_SERVER['argv'];
37+
}
38+
EOT;
39+
40+
$tester = new FPM\Tester($cfg, $code);
41+
$tester->start();
42+
$tester->expectLogStartNotices();
43+
$response = $tester->request();
44+
echo "=====", PHP_EOL;
45+
$response->printBody();
46+
echo "=====", PHP_EOL;
47+
$tester->terminate();
48+
$tester->close();
49+
50+
?>
51+
Done
52+
--EXPECTF--
53+
=====
54+
Deprecated: Deriving $_SERVER['argc'] and $_SERVER['argv'] from $_SERVER['QUERY_STRING'] is deprecated, configure register_argc_argv=0 to suppress this message and access the query parameters via $_SERVER['QUERY_STRING'] or $_GET in %s on line %d
55+
bool(false)
56+
bool(true)
57+
string(4) "test"
58+
bool(false)
59+
bool(false)
60+
string(2) "dt"
61+
string(2) "dt"
62+
=====
63+
Done
64+
--CLEAN--
65+
<?php
66+
require_once "tester.inc";
67+
FPM\Tester::clean();
68+
?>

sapi/fpm/tests/bug75712-getenv-server-vars.phpt renamed to sapi/fpm/tests/bug75712-getenv-server-vars_002.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
FPM: bug75712 - getenv should not read from $_ENV and $_SERVER
2+
FPM: bug75712 - getenv should not read from $_ENV and $_SERVER (register_argc_argv=off)
33
--SKIPIF--
44
<?php include "skipif.inc"; ?>
55
--FILE--
@@ -15,7 +15,7 @@ listen = {{ADDR}}
1515
pm = static
1616
pm.max_children = 1
1717
env[TEST] = test
18-
php_value[register_argc_argv] = on
18+
php_value[register_argc_argv] = off
1919
EOT;
2020

2121
$code = <<<EOT

tests/basic/011.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ for ($i=0; $i<$argc; $i++) {
1818
}
1919

2020
?>
21-
--EXPECT--
21+
--EXPECTF--
22+
Deprecated: Deriving $_SERVER['argc'] and $_SERVER['argv'] from $_SERVER['QUERY_STRING'] is deprecated, configure register_argc_argv=0 to suppress this message and access the query parameters via $_SERVER['QUERY_STRING'] or $_GET in %s on line %d
2223
0: ab
2324
1: cd
2425
2: ef

tests/basic/011_empty_query.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Testing $argc and $argv handling (GET empty)
3+
--SKIPIF--
4+
<?php
5+
if(substr(PHP_OS, 0, 3) == 'WIN') die("skip on windows: --INI-- is ignored due to 4b9cd27ff5c0177dcb160caeae1ea79e761ada58");
6+
?>
7+
--INI--
8+
register_argc_argv=1
9+
--CGI--
10+
--FILE--
11+
<?php
12+
13+
var_dump($_SERVER['argc'], $_SERVER['argv']);
14+
15+
?>
16+
--EXPECTF--
17+
Deprecated: Deriving $_SERVER['argc'] and $_SERVER['argv'] from $_SERVER['QUERY_STRING'] is deprecated, configure register_argc_argv=0 to suppress this message and access the query parameters via $_SERVER['QUERY_STRING'] or $_GET in %s on line %d
18+
int(0)
19+
array(0) {
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Testing $argc and $argv handling (GET, register_argc_argv=0)
3+
--SKIPIF--
4+
<?php
5+
if(substr(PHP_OS, 0, 3) == 'WIN') die("skip on windows: --INI-- is ignored due to 4b9cd27ff5c0177dcb160caeae1ea79e761ada58");
6+
?>
7+
--INI--
8+
register_argc_argv=0
9+
--GET--
10+
ab+cd+ef+123+test
11+
--FILE--
12+
<?php
13+
14+
var_dump($_SERVER['argc'], $_SERVER['argv']);
15+
16+
?>
17+
--EXPECTF--
18+
Warning: Undefined array key "argc" in %s on line %d
19+
20+
Warning: Undefined array key "argv" in %s on line %d
21+
NULL
22+
NULL

tests/basic/011_windows.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ for ($i=0; $i<$argc; $i++) {
1414
}
1515

1616
?>
17-
--EXPECT--
17+
--EXPECTF--
18+
Deprecated: Deriving $_SERVER['argc'] and $_SERVER['argv'] from $_SERVER['QUERY_STRING'] is deprecated, configure register_argc_argv=0 to suppress this message and access the query parameters via $_SERVER['QUERY_STRING'] or $_GET in %s on line %d
1819
0: foo=ab
1920
1: cd
2021
2: ef
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
Testing $argc and $argv handling (cli, register_argc_argv=0)
3+
--INI--
4+
register_argc_argv=0
5+
variables_order=GPS
6+
--ARGS--
7+
ab cd ef 123 test
8+
--FILE--
9+
<?php
10+
11+
var_dump(
12+
$argc,
13+
$argv,
14+
$_SERVER['argc'],
15+
$_SERVER['argv'],
16+
);
17+
18+
?>
19+
--EXPECTF--
20+
int(6)
21+
array(6) {
22+
[0]=>
23+
string(%d) "%s"
24+
[1]=>
25+
string(2) "ab"
26+
[2]=>
27+
string(2) "cd"
28+
[3]=>
29+
string(2) "ef"
30+
[4]=>
31+
string(3) "123"
32+
[5]=>
33+
string(4) "test"
34+
}
35+
int(6)
36+
array(6) {
37+
[0]=>
38+
string(%d) "%s"
39+
[1]=>
40+
string(2) "ab"
41+
[2]=>
42+
string(2) "cd"
43+
[3]=>
44+
string(2) "ef"
45+
[4]=>
46+
string(3) "123"
47+
[5]=>
48+
string(4) "test"
49+
}

0 commit comments

Comments
 (0)