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.
This commit is contained in:
George Peter Banyard 2023-09-03 00:59:20 +01:00
parent a022ec53bd
commit a579fa807c
4 changed files with 38 additions and 7 deletions

4
NEWS
View File

@ -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)

View File

@ -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;
}

View File

@ -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)

View File

@ -0,0 +1,22 @@
--TEST--
GitHub #12020 intl_get_error_message() broken after MessageFormatter::formatMessage() fails
--EXTENSIONS--
intl
--FILE--
<?php
var_dump(\MessageFormatter::formatMessage('en', 'some message with {invalid format}', []), intl_get_error_message());
var_dump(\MessageFormatter::formatMessage('en', 'some {wrong.format}', []), intl_get_error_message());
var_dump(msgfmt_format_message('en', 'some message with {invalid format}', []), intl_get_error_message());
var_dump(msgfmt_format_message('en', 'some {wrong.format}', []), intl_get_error_message());
?>
--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"