Skip to content

Commit a579fa8

Browse files
committed
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.
1 parent a022ec5 commit a579fa8

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ PHP NEWS
1414
. Fixed build for NetBSD which still uses the old iconv signature.
1515
(David Carlier)
1616

17+
- Intl:
18+
. Fixed bug GH-12020 (intl_get_error_message() broken after
19+
MessageFormatter::formatMessage() fails). (Girgias)
20+
1721
- MySQLnd:
1822
. Fixed bug GH-10270 (Invalid error message when connection via SSL fails:
1923
"trying to connect via (null)"). (Kamil Tekiela)

ext/intl/msgformat/msgformat_format.c

+6-7
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ PHP_FUNCTION( msgfmt_format_message )
9898
intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
9999
if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) )
100100
{
101-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
101+
intl_error_set(/* intl_error* */ NULL, U_ILLEGAL_ARGUMENT_ERROR,
102102
"msgfmt_format_message: error converting pattern to UTF-16", 0 );
103103
RETURN_FALSE;
104104
}
@@ -113,7 +113,7 @@ PHP_FUNCTION( msgfmt_format_message )
113113

114114
#ifdef MSG_FORMAT_QUOTE_APOS
115115
if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
116-
intl_error_set( NULL, U_INVALID_FORMAT_ERROR,
116+
intl_error_set(/* intl_error* */ NULL, U_INVALID_FORMAT_ERROR,
117117
"msgfmt_format_message: error converting pattern to quote-friendly format", 0 );
118118
RETURN_FALSE;
119119
}
@@ -134,15 +134,14 @@ PHP_FUNCTION( msgfmt_format_message )
134134
spprintf( &msg, 0, "pattern syntax error (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "unknown parser error" );
135135
smart_str_free( &parse_error_str );
136136

137-
intl_error_set_code( NULL, INTL_DATA_ERROR_CODE( mfo ) );
138-
intl_errors_set_custom_msg( INTL_DATA_ERROR_P( mfo ), msg, 1 );
137+
/* Pass NULL to intl_error* parameter to store message in global Intl error msg stack */
138+
intl_error_set_code(/* intl_error* */ NULL, INTL_DATA_ERROR_CODE( mfo ) );
139+
intl_errors_set_custom_msg(/* intl_error* */ NULL, msg, 1 );
139140

140141
efree( msg );
141142
} else {
142-
intl_errors_set_custom_msg( INTL_DATA_ERROR_P(mfo), "Creating message formatter failed", 0 );
143+
intl_errors_set_custom_msg(/* intl_error* */ NULL, "Creating message formatter failed", 0 );
143144
}
144-
/* Reset custom error message as this is a static method that has no object */
145-
intl_errors_reset(INTL_DATA_ERROR_P(mfo));
146145
umsg_close(MSG_FORMAT_OBJECT(mfo));
147146
RETURN_FALSE;
148147
}

ext/intl/tests/gh11658.phpt

+6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ ini_set("intl.error_level", E_WARNING);
99

1010
$s = MessageFormatter::formatMessage('en', 'some {wrong.format}', []);
1111
var_dump($s);
12+
13+
$s = msgfmt_format_message('en', 'some {wrong.format}', []);
14+
var_dump($s);
1215
?>
1316
--EXPECTF--
1417
Warning: MessageFormatter::formatMessage(): pattern syntax error (parse error at offset 6, after "some {", before or at "wrong.format}") in %s on line %d
1518
bool(false)
19+
20+
Warning: msgfmt_format_message(): pattern syntax error (parse error at offset 6, after "some {", before or at "wrong.format}") in %s on line %d
21+
bool(false)

ext/intl/tests/gh12020.phpt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GitHub #12020 intl_get_error_message() broken after MessageFormatter::formatMessage() fails
3+
--EXTENSIONS--
4+
intl
5+
--FILE--
6+
<?php
7+
8+
var_dump(\MessageFormatter::formatMessage('en', 'some message with {invalid format}', []), intl_get_error_message());
9+
var_dump(\MessageFormatter::formatMessage('en', 'some {wrong.format}', []), intl_get_error_message());
10+
11+
var_dump(msgfmt_format_message('en', 'some message with {invalid format}', []), intl_get_error_message());
12+
var_dump(msgfmt_format_message('en', 'some {wrong.format}', []), intl_get_error_message());
13+
?>
14+
--EXPECT--
15+
bool(false)
16+
string(128) "pattern syntax error (parse error at offset 19, after " message with {", before or at "invalid format}"): U_PATTERN_SYNTAX_ERROR"
17+
bool(false)
18+
string(116) "pattern syntax error (parse error at offset 6, after "some {", before or at "wrong.format}"): U_PATTERN_SYNTAX_ERROR"
19+
bool(false)
20+
string(128) "pattern syntax error (parse error at offset 19, after " message with {", before or at "invalid format}"): U_PATTERN_SYNTAX_ERROR"
21+
bool(false)
22+
string(116) "pattern syntax error (parse error at offset 6, after "some {", before or at "wrong.format}"): U_PATTERN_SYNTAX_ERROR"

0 commit comments

Comments
 (0)