summaryrefslogtreecommitdiff
path: root/sapi
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2020-01-27 13:32:29 +0100
committerNikita Popov <nikita.ppv@gmail.com>2020-01-27 13:32:38 +0100
commit1cccbb8ff1339c44075e7dee8613d98dc8056f68 (patch)
tree3eb848778ca364b6a819b684b4ee6c694ef693c1 /sapi
parent41e1891e0c2c4d9c3827d8e95dff735eee38268d (diff)
parentfd08f062ae5a3c92bfc0345da7e83ab320046864 (diff)
downloadphp-git-1cccbb8ff1339c44075e7dee8613d98dc8056f68.tar.gz
Merge branch 'PHP-7.3' into PHP-7.4
* PHP-7.3: Fix bug #78323: Code 0 is returned on invalid options
Diffstat (limited to 'sapi')
-rw-r--r--sapi/cgi/cgi_main.c4
-rw-r--r--sapi/cgi/tests/bug78323.phpt41
-rw-r--r--sapi/cli/php_cli.c6
-rw-r--r--sapi/cli/tests/015.phpt2
-rw-r--r--sapi/cli/tests/bug78323.phpt78
-rw-r--r--sapi/fpm/fpm/fpm_main.c3
-rw-r--r--sapi/fpm/tests/bug78323.phpt39
7 files changed, 170 insertions, 3 deletions
diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c
index c885f30677..29c61e9a18 100644
--- a/sapi/cgi/cgi_main.c
+++ b/sapi/cgi/cgi_main.c
@@ -2282,6 +2282,7 @@ parent_loop_end:
break;
case 'h':
case '?':
+ case PHP_GETOPT_INVALID_ARG:
if (request) {
fcgi_destroy_request(request);
}
@@ -2291,6 +2292,9 @@ parent_loop_end:
php_cgi_usage(argv[0]);
php_output_end_all();
exit_status = 0;
+ if (c == PHP_GETOPT_INVALID_ARG) {
+ exit_status = 1;
+ }
goto out;
}
}
diff --git a/sapi/cgi/tests/bug78323.phpt b/sapi/cgi/tests/bug78323.phpt
new file mode 100644
index 0000000000..d89e51874a
--- /dev/null
+++ b/sapi/cgi/tests/bug78323.phpt
@@ -0,0 +1,41 @@
+--TEST--
+Bug #78323 Test exit code and error message for invalid parameters
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--FILE--
+<?php
+include "include.inc";
+$php = get_cgi_path();
+reset_env_vars();
+
+
+// no argument for option
+ob_start();
+passthru("$php --memory-limit=1G 2>&1", $exitCode);
+$output = ob_get_contents();
+ob_end_clean();
+
+$lines = preg_split('/\R/', $output);
+echo $lines[0], "\n",
+ $lines[1], "\n",
+ "Done: $exitCode\n\n";
+
+
+// Successful execution
+ob_start();
+passthru("$php -dmemory-limit=1G -v", $exitCode);
+$output = ob_get_contents();
+ob_end_clean();
+
+$lines = preg_split('/\R/', $output);
+echo $lines[0], "\n",
+ "Done: $exitCode\n";
+
+?>
+--EXPECTF--
+Error in argument 1, char 1: no argument for option -
+Usage: %s
+Done: 1
+
+PHP %s
+Done: 0
diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c
index a305c36ade..1b14b7586b 100644
--- a/sapi/cli/php_cli.c
+++ b/sapi/cli/php_cli.c
@@ -1228,7 +1228,7 @@ int main(int argc, char *argv[])
setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
#endif
- while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2))!=-1) {
+ while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 1, 2))!=-1) {
switch (c) {
case 'c':
if (ini_path_override) {
@@ -1280,6 +1280,10 @@ int main(int argc, char *argv[])
case '?':
php_cli_usage(argv[0]);
goto out;
+ case PHP_GETOPT_INVALID_ARG: /* print usage on bad options, exit 1 */
+ php_cli_usage(argv[0]);
+ exit_status = 1;
+ goto out;
case 'i': case 'v': case 'm':
sapi_module = &cli_sapi_module;
goto exit_loop;
diff --git a/sapi/cli/tests/015.phpt b/sapi/cli/tests/015.phpt
index 01f5328e99..5a5e6c5190 100644
--- a/sapi/cli/tests/015.phpt
+++ b/sapi/cli/tests/015.phpt
@@ -16,7 +16,7 @@ $php = getenv('TEST_PHP_EXECUTABLE');
echo `"$php" -n --version | grep built:`;
echo `echo "<?php print_r(\\\$argv);" | "$php" -n -- foo bar baz`, "\n";
echo `"$php" -n --version foo bar baz | grep built:`;
-echo `"$php" -n --notexisting foo bar baz | grep Usage:`;
+echo `"$php" -n --notexisting foo bar baz 2>&1 | grep Usage:`;
echo "Done\n";
?>
diff --git a/sapi/cli/tests/bug78323.phpt b/sapi/cli/tests/bug78323.phpt
new file mode 100644
index 0000000000..02b18e02a2
--- /dev/null
+++ b/sapi/cli/tests/bug78323.phpt
@@ -0,0 +1,78 @@
+--TEST--
+Bug #78323 Test exit code and error message for invalid parameters
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+$php = getenv('TEST_PHP_EXECUTABLE');
+
+// There are 3 types of option errors:
+// 1 : in flags
+// 2 option not found
+// 3 no argument for option
+
+
+// colon in flags
+ob_start();
+passthru("$php -a:Z 2>&1", $exitCode);
+$output = ob_get_contents();
+ob_end_clean();
+
+$lines = preg_split('/\R/', $output);
+echo $lines[0], "\n",
+ $lines[1], "\n",
+ "Done: $exitCode\n\n";
+
+
+// option not found
+ob_start();
+passthru("$php -Z 2>&1", $exitCode);
+$output = ob_get_contents();
+ob_end_clean();
+
+$lines = preg_split('/\R/', $output);
+echo $lines[0], "\n",
+ $lines[1], "\n",
+ "Done: $exitCode\n\n";
+
+
+// no argument for option
+ob_start();
+passthru("$php --memory-limit=1G 2>&1", $exitCode);
+$output = ob_get_contents();
+ob_end_clean();
+
+$lines = preg_split('/\R/', $output);
+echo $lines[0], "\n",
+ $lines[1], "\n",
+ "Done: $exitCode\n\n";
+
+
+// Successful execution
+ob_start();
+passthru("$php -dmemory-limit=1G -v", $exitCode);
+$output = ob_get_contents();
+ob_end_clean();
+
+$lines = preg_split('/\R/', $output);
+echo $lines[0], "\n",
+ "Done: $exitCode\n";
+
+?>
+--EXPECTF--
+Error in argument %d, char %d: : in flags
+Usage: %s [options] [-f] <file> [--] [args...]
+Done: 1
+
+Error in argument %d, char %d: option not found %s
+Usage: %s [options] [-f] <file> [--] [args...]
+Done: 1
+
+Error in argument %d, char %d: no argument for option %s
+Usage: %s [options] [-f] <file> [--] [args...]
+Done: 1
+
+PHP %s
+Done: 0
diff --git a/sapi/fpm/fpm/fpm_main.c b/sapi/fpm/fpm/fpm_main.c
index 342c9abaed..bc3c145ca0 100644
--- a/sapi/fpm/fpm/fpm_main.c
+++ b/sapi/fpm/fpm/fpm_main.c
@@ -1697,6 +1697,7 @@ int main(int argc, char *argv[])
default:
case 'h':
case '?':
+ case PHP_GETOPT_INVALID_ARG:
cgi_sapi_module.startup(&cgi_sapi_module);
php_output_activate();
SG(headers_sent) = 1;
@@ -1704,7 +1705,7 @@ int main(int argc, char *argv[])
php_output_end_all();
php_output_deactivate();
fcgi_shutdown();
- exit_status = (c == 'h') ? FPM_EXIT_OK : FPM_EXIT_USAGE;
+ exit_status = (c != PHP_GETOPT_INVALID_ARG) ? FPM_EXIT_OK : FPM_EXIT_USAGE;
goto out;
case 'v': /* show php version & quit */
diff --git a/sapi/fpm/tests/bug78323.phpt b/sapi/fpm/tests/bug78323.phpt
new file mode 100644
index 0000000000..cf91020573
--- /dev/null
+++ b/sapi/fpm/tests/bug78323.phpt
@@ -0,0 +1,39 @@
+--TEST--
+FPM: Bug #78323 Test exit code for invalid parameters
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--FILE--
+<?php
+
+require_once "tester.inc";
+
+$php = \FPM\Tester::findExecutable();
+
+// no argument for option
+ob_start();
+passthru("$php --memory-limit=1G 2>&1", $exitCode);
+$output = ob_get_contents();
+ob_end_clean();
+
+$lines = preg_split('/\R/', $output);
+echo $lines[0], "\n",
+ "Done: $exitCode\n\n";
+
+
+// Successful execution
+ob_start();
+passthru("$php -dmemory-limit=1G -v", $exitCode);
+$output = ob_get_contents();
+ob_end_clean();
+
+$lines = preg_split('/\R/', $output);
+echo $lines[0], "\n",
+ "Done: $exitCode\n";
+
+?>
+--EXPECTF--
+Usage: %s
+Done: 64
+
+PHP %s
+Done: 0