Skip to content

Fix GH-20871: Improve error message for invalid -d options#20899

Open
khaledalam wants to merge 4 commits intophp:masterfrom
khaledalam:fix-gh20871-d-option-error-message
Open

Fix GH-20871: Improve error message for invalid -d options#20899
khaledalam wants to merge 4 commits intophp:masterfrom
khaledalam:fix-gh20871-d-option-error-message

Conversation

@khaledalam
Copy link
Copy Markdown
Contributor

Description

Fixes #20871 by improving error messages for invalid -d command-line options.

Previously, when an invalid -d option was passed to PHP CLI, the error message reported "Unknown on line X". This was confusing for users who don't know about PHP's internal INI handling.

image

Changes

  • Modified Zend/zend_ini_parser.y to detect CLI/FPM -d option parsing context
  • When parsing -d options (filename is "Unknown" and unbuffered_errors is true), error messages now report "-d option on line 0" instead of "Unknown on line X"
  • Added test case sapi/cli/tests/gh20871.phpt to verify the fix with multiple invalid syntax scenarios

@iluuu1994
Copy link
Copy Markdown
Member

It would probably help even more if there was an indication that the parsing error happens for INI parsing, rather than parsing of the PHP code. For example, instead of "in Unknown" it could print "in INI command line parameter '-d'". Or maybe even the INI line itself.

@khaledalam
Copy link
Copy Markdown
Contributor Author

It would probably help even more if there was an indication that the parsing error happens for INI parsing, rather than parsing of the PHP code. For example, instead of "in Unknown" it could print "in INI command line parameter '-d'". Or maybe even the INI line itself.

@iluuu1994 Thank you for the feedback! I've updated the message. The error message now clearly indicates this is INI parsing,

e.g. PHP: syntax error, unexpected '=' in INI command line parameter '-d'

Copy link
Copy Markdown
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better if this is also reviewed by somebody else. ^^ @bukka?

Comment thread Zend/zend_ini_parser.y Outdated
error_buf = (char *) emalloc(error_buf_len);

sprintf(error_buf, "%s in %s on line %" PRIu32 "\n", msg, currently_parsed_filename, zend_ini_scanner_get_lineno());
if (strcmp(currently_parsed_filename, "Unknown") == 0 && CG(ini_parser_unbuffered_errors)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want to check if (ini_filename) instead. Side-note: The if (currently_parsed_filename) { is useless because zend_ini_scanner_get_filename() never returns NULL.

Comment thread Zend/zend_ini_parser.y Outdated
error_buf = (char *) emalloc(error_buf_len);

sprintf(error_buf, "%s in %s on line %" PRIu32 "\n", msg, currently_parsed_filename, zend_ini_scanner_get_lineno());
if (strcmp(currently_parsed_filename, "Unknown") == 0 && CG(ini_parser_unbuffered_errors)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this check CG(ini_parser_unbuffered_errors)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To distinguish CLI/FPM -d INI overrides from parse_ini_string(). in basic_functions.c it's called with unbuffered_errors = 0, while the other call sites pass 1.

Comment thread Zend/zend_ini_parser.y Outdated
error_buf = (char *) emalloc(error_buf_len);

sprintf(error_buf, "%s in %s on line %" PRIu32 "\n", msg, currently_parsed_filename, zend_ini_scanner_get_lineno());
if (strcmp(currently_parsed_filename, "Unknown") == 0 && CG(ini_parser_unbuffered_errors)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can ini_filename be unset for any other reasons? E.g. when set in php-fpm.conf or php_value in Apache?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unbuffered_errors check correctly identifies CLI/FPM command-line cases. For fpm conf and Apache php_value, I think those use zend_parse_ini_file() which sets ini_filename from the file handle.

@khaledalam
Copy link
Copy Markdown
Contributor Author

Hi @bukka, can you review this PR please.

@jorgsowa
Copy link
Copy Markdown
Contributor

PHP: syntax error, unexpected '=' in INI command line parameter '-d'

  1. There is double space. Could be fixed as well when you touch the code.
  2. I'm not sure about this naming. It's still not very clear what is INI command here. What do you think about PHP: syntax error, invalid -d parameter. Unexpected '=' in INI value or PHP: syntax error, malformed INI override passed via '-d'.

Or modified proposed version PHP: syntax error, unexpected '=' in parameter '-d'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bad error line and file for invalid "-d" arguments

3 participants