Skip to content

Commit 16f194c

Browse files
committed
Merge branch 'PHP-7.4'
* PHP-7.4: Fix bug #78323: Code 0 is returned on invalid options
2 parents 3f67798 + 1cccbb8 commit 16f194c

File tree

10 files changed

+176
-5
lines changed

10 files changed

+176
-5
lines changed

ext/standard/basic_functions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1856,7 +1856,7 @@ PHP_FUNCTION(getopt)
18561856

18571857
while ((o = php_getopt(argc, argv, opts, &php_optarg, &php_optind, 0, 1)) != -1) {
18581858
/* Skip unknown arguments. */
1859-
if (o == '?') {
1859+
if (o == PHP_GETOPT_INVALID_ARG) {
18601860
continue;
18611861
}
18621862

main/getopt.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#define OPTERRNF (2)
2525
#define OPTERRARG (3)
2626

27+
// Print error message to stderr and return -2 to distinguish it from '?' command line option.
2728
static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int err, int show_err) /* {{{ */
2829
{
2930
if (show_err)
@@ -45,7 +46,7 @@ static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int
4546
break;
4647
}
4748
}
48-
return('?');
49+
return PHP_GETOPT_INVALID_ARG;
4950
}
5051
/* }}} */
5152

main/php_getopt.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ extern PHPAPI int php_optidx;
3333
PHPAPI int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err, int arg_start);
3434
END_EXTERN_C()
3535

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

sapi/cgi/cgi_main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,6 +2283,7 @@ consult the installation file that came with this distribution, or visit \n\
22832283
break;
22842284
case 'h':
22852285
case '?':
2286+
case PHP_GETOPT_INVALID_ARG:
22862287
if (request) {
22872288
fcgi_destroy_request(request);
22882289
}
@@ -2292,6 +2293,9 @@ consult the installation file that came with this distribution, or visit \n\
22922293
php_cgi_usage(argv[0]);
22932294
php_output_end_all();
22942295
exit_status = 0;
2296+
if (c == PHP_GETOPT_INVALID_ARG) {
2297+
exit_status = 1;
2298+
}
22952299
goto out;
22962300
}
22972301
}

sapi/cgi/tests/bug78323.phpt

Lines changed: 41 additions & 0 deletions
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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ int main(int argc, char *argv[])
12261226
setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
12271227
#endif
12281228

1229-
while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2))!=-1) {
1229+
while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 1, 2))!=-1) {
12301230
switch (c) {
12311231
case 'c':
12321232
if (ini_path_override) {
@@ -1278,6 +1278,10 @@ int main(int argc, char *argv[])
12781278
case '?':
12791279
php_cli_usage(argv[0]);
12801280
goto out;
1281+
case PHP_GETOPT_INVALID_ARG: /* print usage on bad options, exit 1 */
1282+
php_cli_usage(argv[0]);
1283+
exit_status = 1;
1284+
goto out;
12811285
case 'i': case 'v': case 'm':
12821286
sapi_module = &cli_sapi_module;
12831287
goto exit_loop;

sapi/cli/tests/015.phpt

Lines changed: 1 addition & 1 deletion
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

Lines changed: 78 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1698,14 +1698,15 @@ int main(int argc, char *argv[])
16981698
default:
16991699
case 'h':
17001700
case '?':
1701+
case PHP_GETOPT_INVALID_ARG:
17011702
cgi_sapi_module.startup(&cgi_sapi_module);
17021703
php_output_activate();
17031704
SG(headers_sent) = 1;
17041705
php_cgi_usage(argv[0]);
17051706
php_output_end_all();
17061707
php_output_deactivate();
17071708
fcgi_shutdown();
1708-
exit_status = (c == 'h') ? FPM_EXIT_OK : FPM_EXIT_USAGE;
1709+
exit_status = (c != PHP_GETOPT_INVALID_ARG) ? FPM_EXIT_OK : FPM_EXIT_USAGE;
17091710
goto out;
17101711

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

sapi/fpm/tests/bug78323.phpt

Lines changed: 39 additions & 0 deletions
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)