From a579fa807c260cd571e41486678758d277a541f7 Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Sun, 3 Sep 2023 00:59:20 +0100 Subject: [PATCH] Fixed bug GH-12020: intl_get_error_message() broken after MessageFormatter::formatMessage() fails Passing NULL as the pointer to intl_error* will use the global error stack. This is what we need to do instead of pushing it onto the temporary format object that is released. --- NEWS | 4 ++++ ext/intl/msgformat/msgformat_format.c | 13 ++++++------- ext/intl/tests/gh11658.phpt | 6 ++++++ ext/intl/tests/gh12020.phpt | 22 ++++++++++++++++++++++ 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 ext/intl/tests/gh12020.phpt diff --git a/NEWS b/NEWS index be860fb96d1..e001635a235 100644 --- a/NEWS +++ b/NEWS @@ -14,6 +14,10 @@ PHP NEWS . Fixed build for NetBSD which still uses the old iconv signature. (David Carlier) +- Intl: + . Fixed bug GH-12020 (intl_get_error_message() broken after + MessageFormatter::formatMessage() fails). (Girgias) + - MySQLnd: . Fixed bug GH-10270 (Invalid error message when connection via SSL fails: "trying to connect via (null)"). (Kamil Tekiela) diff --git a/ext/intl/msgformat/msgformat_format.c b/ext/intl/msgformat/msgformat_format.c index 098c6a2b922..f6ec60fe199 100644 --- a/ext/intl/msgformat/msgformat_format.c +++ b/ext/intl/msgformat/msgformat_format.c @@ -98,7 +98,7 @@ PHP_FUNCTION( msgfmt_format_message ) intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo)); if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) ) { - intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, + intl_error_set(/* intl_error* */ NULL, U_ILLEGAL_ARGUMENT_ERROR, "msgfmt_format_message: error converting pattern to UTF-16", 0 ); RETURN_FALSE; } @@ -113,7 +113,7 @@ PHP_FUNCTION( msgfmt_format_message ) #ifdef MSG_FORMAT_QUOTE_APOS if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) { - intl_error_set( NULL, U_INVALID_FORMAT_ERROR, + intl_error_set(/* intl_error* */ NULL, U_INVALID_FORMAT_ERROR, "msgfmt_format_message: error converting pattern to quote-friendly format", 0 ); RETURN_FALSE; } @@ -134,15 +134,14 @@ PHP_FUNCTION( msgfmt_format_message ) spprintf( &msg, 0, "pattern syntax error (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "unknown parser error" ); smart_str_free( &parse_error_str ); - intl_error_set_code( NULL, INTL_DATA_ERROR_CODE( mfo ) ); - intl_errors_set_custom_msg( INTL_DATA_ERROR_P( mfo ), msg, 1 ); + /* Pass NULL to intl_error* parameter to store message in global Intl error msg stack */ + intl_error_set_code(/* intl_error* */ NULL, INTL_DATA_ERROR_CODE( mfo ) ); + intl_errors_set_custom_msg(/* intl_error* */ NULL, msg, 1 ); efree( msg ); } else { - intl_errors_set_custom_msg( INTL_DATA_ERROR_P(mfo), "Creating message formatter failed", 0 ); + intl_errors_set_custom_msg(/* intl_error* */ NULL, "Creating message formatter failed", 0 ); } - /* Reset custom error message as this is a static method that has no object */ - intl_errors_reset(INTL_DATA_ERROR_P(mfo)); umsg_close(MSG_FORMAT_OBJECT(mfo)); RETURN_FALSE; } diff --git a/ext/intl/tests/gh11658.phpt b/ext/intl/tests/gh11658.phpt index b29786255ca..f0cfab9280e 100644 --- a/ext/intl/tests/gh11658.phpt +++ b/ext/intl/tests/gh11658.phpt @@ -9,7 +9,13 @@ ini_set("intl.error_level", E_WARNING); $s = MessageFormatter::formatMessage('en', 'some {wrong.format}', []); var_dump($s); + +$s = msgfmt_format_message('en', 'some {wrong.format}', []); +var_dump($s); ?> --EXPECTF-- Warning: MessageFormatter::formatMessage(): pattern syntax error (parse error at offset 6, after "some {", before or at "wrong.format}") in %s on line %d bool(false) + +Warning: msgfmt_format_message(): pattern syntax error (parse error at offset 6, after "some {", before or at "wrong.format}") in %s on line %d +bool(false) diff --git a/ext/intl/tests/gh12020.phpt b/ext/intl/tests/gh12020.phpt new file mode 100644 index 00000000000..e4102606ca5 --- /dev/null +++ b/ext/intl/tests/gh12020.phpt @@ -0,0 +1,22 @@ +--TEST-- +GitHub #12020 intl_get_error_message() broken after MessageFormatter::formatMessage() fails +--EXTENSIONS-- +intl +--FILE-- + +--EXPECT-- +bool(false) +string(128) "pattern syntax error (parse error at offset 19, after " message with {", before or at "invalid format}"): U_PATTERN_SYNTAX_ERROR" +bool(false) +string(116) "pattern syntax error (parse error at offset 6, after "some {", before or at "wrong.format}"): U_PATTERN_SYNTAX_ERROR" +bool(false) +string(128) "pattern syntax error (parse error at offset 19, after " message with {", before or at "invalid format}"): U_PATTERN_SYNTAX_ERROR" +bool(false) +string(116) "pattern syntax error (parse error at offset 6, after "some {", before or at "wrong.format}"): U_PATTERN_SYNTAX_ERROR"