Skip to content

Commit 1cccbb8

Browse files
committed
Merge branch 'PHP-7.3' into PHP-7.4
* PHP-7.3: Fix bug #78323: Code 0 is returned on invalid options
2 parents 41e1891 + fd08f06 commit 1cccbb8

File tree

11 files changed

+177
-5
lines changed

11 files changed

+177
-5
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ PHP NEWS
66
. Fixed bug #79146 (cscript can fail to run on some systems). (clarodeus)
77
. Fixed bug #79155 (Property nullability lost when using multiple property
88
definition). (Nikita)
9+
. Fixed bug #78323 (Code 0 is returned on invalid options). (Ivan Mikheykin)
910

1011
- CURL:
1112
. Fixed bug #79078 (Hypothetical use-after-free in curl_multi_add_handle()).

ext/standard/basic_functions.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -4474,7 +4474,7 @@ PHP_FUNCTION(getopt)
44744474

44754475
while ((o = php_getopt(argc, argv, opts, &php_optarg, &php_optind, 0, 1)) != -1) {
44764476
/* Skip unknown arguments. */
4477-
if (o == '?') {
4477+
if (o == PHP_GETOPT_INVALID_ARG) {
44784478
continue;
44794479
}
44804480

main/getopt.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define OPTERRNF (2)
2727
#define OPTERRARG (3)
2828

29+
// Print error message to stderr and return -2 to distinguish it from '?' command line option.
2930
static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int err, int show_err) /* {{{ */
3031
{
3132
if (show_err)
@@ -47,7 +48,7 @@ static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int
4748
break;
4849
}
4950
}
50-
return('?');
51+
return PHP_GETOPT_INVALID_ARG;
5152
}
5253
/* }}} */
5354

main/php_getopt.h

+3
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@ extern PHPAPI int php_optidx;
3535
PHPAPI int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err, int arg_start);
3636
END_EXTERN_C()
3737

38+
/* php_getopt will return this value if there is an error in arguments */
39+
#define PHP_GETOPT_INVALID_ARG (-2)
40+
3841
#endif

sapi/cgi/cgi_main.c

+4
Original file line numberDiff line numberDiff line change
@@ -2282,6 +2282,7 @@ consult the installation file that came with this distribution, or visit \n\
22822282
break;
22832283
case 'h':
22842284
case '?':
2285+
case PHP_GETOPT_INVALID_ARG:
22852286
if (request) {
22862287
fcgi_destroy_request(request);
22872288
}
@@ -2291,6 +2292,9 @@ consult the installation file that came with this distribution, or visit \n\
22912292
php_cgi_usage(argv[0]);
22922293
php_output_end_all();
22932294
exit_status = 0;
2295+
if (c == PHP_GETOPT_INVALID_ARG) {
2296+
exit_status = 1;
2297+
}
22942298
goto out;
22952299
}
22962300
}

sapi/cgi/tests/bug78323.phpt

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
Bug #78323 Test exit code and error message for invalid parameters
3+
--SKIPIF--
4+
<?php include "skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
include "include.inc";
8+
$php = get_cgi_path();
9+
reset_env_vars();
10+
11+
12+
// no argument for option
13+
ob_start();
14+
passthru("$php --memory-limit=1G 2>&1", $exitCode);
15+
$output = ob_get_contents();
16+
ob_end_clean();
17+
18+
$lines = preg_split('/\R/', $output);
19+
echo $lines[0], "\n",
20+
$lines[1], "\n",
21+
"Done: $exitCode\n\n";
22+
23+
24+
// Successful execution
25+
ob_start();
26+
passthru("$php -dmemory-limit=1G -v", $exitCode);
27+
$output = ob_get_contents();
28+
ob_end_clean();
29+
30+
$lines = preg_split('/\R/', $output);
31+
echo $lines[0], "\n",
32+
"Done: $exitCode\n";
33+
34+
?>
35+
--EXPECTF--
36+
Error in argument 1, char 1: no argument for option -
37+
Usage: %s
38+
Done: 1
39+
40+
PHP %s
41+
Done: 0

sapi/cli/php_cli.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ int main(int argc, char *argv[])
12281228
setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
12291229
#endif
12301230

1231-
while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2))!=-1) {
1231+
while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 1, 2))!=-1) {
12321232
switch (c) {
12331233
case 'c':
12341234
if (ini_path_override) {
@@ -1280,6 +1280,10 @@ int main(int argc, char *argv[])
12801280
case '?':
12811281
php_cli_usage(argv[0]);
12821282
goto out;
1283+
case PHP_GETOPT_INVALID_ARG: /* print usage on bad options, exit 1 */
1284+
php_cli_usage(argv[0]);
1285+
exit_status = 1;
1286+
goto out;
12831287
case 'i': case 'v': case 'm':
12841288
sapi_module = &cli_sapi_module;
12851289
goto exit_loop;

sapi/cli/tests/015.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $php = getenv('TEST_PHP_EXECUTABLE');
1616
echo `"$php" -n --version | grep built:`;
1717
echo `echo "<?php print_r(\\\$argv);" | "$php" -n -- foo bar baz`, "\n";
1818
echo `"$php" -n --version foo bar baz | grep built:`;
19-
echo `"$php" -n --notexisting foo bar baz | grep Usage:`;
19+
echo `"$php" -n --notexisting foo bar baz 2>&1 | grep Usage:`;
2020

2121
echo "Done\n";
2222
?>

sapi/cli/tests/bug78323.phpt

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
--TEST--
2+
Bug #78323 Test exit code and error message for invalid parameters
3+
--SKIPIF--
4+
<?php
5+
include "skipif.inc";
6+
?>
7+
--FILE--
8+
<?php
9+
$php = getenv('TEST_PHP_EXECUTABLE');
10+
11+
// There are 3 types of option errors:
12+
// 1 : in flags
13+
// 2 option not found
14+
// 3 no argument for option
15+
16+
17+
// colon in flags
18+
ob_start();
19+
passthru("$php -a:Z 2>&1", $exitCode);
20+
$output = ob_get_contents();
21+
ob_end_clean();
22+
23+
$lines = preg_split('/\R/', $output);
24+
echo $lines[0], "\n",
25+
$lines[1], "\n",
26+
"Done: $exitCode\n\n";
27+
28+
29+
// option not found
30+
ob_start();
31+
passthru("$php -Z 2>&1", $exitCode);
32+
$output = ob_get_contents();
33+
ob_end_clean();
34+
35+
$lines = preg_split('/\R/', $output);
36+
echo $lines[0], "\n",
37+
$lines[1], "\n",
38+
"Done: $exitCode\n\n";
39+
40+
41+
// no argument for option
42+
ob_start();
43+
passthru("$php --memory-limit=1G 2>&1", $exitCode);
44+
$output = ob_get_contents();
45+
ob_end_clean();
46+
47+
$lines = preg_split('/\R/', $output);
48+
echo $lines[0], "\n",
49+
$lines[1], "\n",
50+
"Done: $exitCode\n\n";
51+
52+
53+
// Successful execution
54+
ob_start();
55+
passthru("$php -dmemory-limit=1G -v", $exitCode);
56+
$output = ob_get_contents();
57+
ob_end_clean();
58+
59+
$lines = preg_split('/\R/', $output);
60+
echo $lines[0], "\n",
61+
"Done: $exitCode\n";
62+
63+
?>
64+
--EXPECTF--
65+
Error in argument %d, char %d: : in flags
66+
Usage: %s [options] [-f] <file> [--] [args...]
67+
Done: 1
68+
69+
Error in argument %d, char %d: option not found %s
70+
Usage: %s [options] [-f] <file> [--] [args...]
71+
Done: 1
72+
73+
Error in argument %d, char %d: no argument for option %s
74+
Usage: %s [options] [-f] <file> [--] [args...]
75+
Done: 1
76+
77+
PHP %s
78+
Done: 0

sapi/fpm/fpm/fpm_main.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1697,14 +1697,15 @@ int main(int argc, char *argv[])
16971697
default:
16981698
case 'h':
16991699
case '?':
1700+
case PHP_GETOPT_INVALID_ARG:
17001701
cgi_sapi_module.startup(&cgi_sapi_module);
17011702
php_output_activate();
17021703
SG(headers_sent) = 1;
17031704
php_cgi_usage(argv[0]);
17041705
php_output_end_all();
17051706
php_output_deactivate();
17061707
fcgi_shutdown();
1707-
exit_status = (c == 'h') ? FPM_EXIT_OK : FPM_EXIT_USAGE;
1708+
exit_status = (c != PHP_GETOPT_INVALID_ARG) ? FPM_EXIT_OK : FPM_EXIT_USAGE;
17081709
goto out;
17091710

17101711
case 'v': /* show php version & quit */

sapi/fpm/tests/bug78323.phpt

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
FPM: Bug #78323 Test exit code for invalid parameters
3+
--SKIPIF--
4+
<?php include "skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
8+
require_once "tester.inc";
9+
10+
$php = \FPM\Tester::findExecutable();
11+
12+
// no argument for option
13+
ob_start();
14+
passthru("$php --memory-limit=1G 2>&1", $exitCode);
15+
$output = ob_get_contents();
16+
ob_end_clean();
17+
18+
$lines = preg_split('/\R/', $output);
19+
echo $lines[0], "\n",
20+
"Done: $exitCode\n\n";
21+
22+
23+
// Successful execution
24+
ob_start();
25+
passthru("$php -dmemory-limit=1G -v", $exitCode);
26+
$output = ob_get_contents();
27+
ob_end_clean();
28+
29+
$lines = preg_split('/\R/', $output);
30+
echo $lines[0], "\n",
31+
"Done: $exitCode\n";
32+
33+
?>
34+
--EXPECTF--
35+
Usage: %s
36+
Done: 64
37+
38+
PHP %s
39+
Done: 0

0 commit comments

Comments
 (0)