Skip to content

Commit 1e41b0f

Browse files
committed
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1: ext/intl: Fix memory leak in MessageFormatter::format()
2 parents 6d98c08 + 536dbd7 commit 1e41b0f

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ PHP NEWS
4141
- GD:
4242
. Fix most of the external libgd test failures. (Michael Orlitzky)
4343

44+
- Intl:
45+
. Fix memory leak in MessageFormatter::format() on failure. (Girgias)
46+
4447
- MBString:
4548
. Fix GH-11300 (license issue: restricted unicode license headers).
4649
(nielsdos)

ext/intl/msgformat/msgformat_format.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,27 @@ PHP_FUNCTION( msgfmt_format_message )
125125
efree(spattern);
126126
}
127127

128-
if (INTL_DATA_ERROR_CODE( mfo ) == U_PATTERN_SYNTAX_ERROR) {
129-
char *msg = NULL;
130-
smart_str parse_error_str;
131-
parse_error_str = intl_parse_error_to_string( &parse_error );
132-
spprintf( &msg, 0, "pattern syntax error (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "unknown parser error" );
133-
smart_str_free( &parse_error_str );
134-
135-
intl_error_set_code( NULL, INTL_DATA_ERROR_CODE( mfo ) );
136-
intl_errors_set_custom_msg( INTL_DATA_ERROR_P( mfo ), msg, 1 );
137-
138-
efree( msg );
128+
/* Cannot use INTL_METHOD_CHECK_STATUS() as we need to free the message object formatter */
129+
if (U_FAILURE(INTL_DATA_ERROR_CODE(mfo))) {
130+
if (INTL_DATA_ERROR_CODE( mfo ) == U_PATTERN_SYNTAX_ERROR) {
131+
char *msg = NULL;
132+
smart_str parse_error_str;
133+
parse_error_str = intl_parse_error_to_string( &parse_error );
134+
spprintf( &msg, 0, "pattern syntax error (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "unknown parser error" );
135+
smart_str_free( &parse_error_str );
136+
137+
intl_error_set_code( NULL, INTL_DATA_ERROR_CODE( mfo ) );
138+
intl_errors_set_custom_msg( INTL_DATA_ERROR_P( mfo ), msg, 1 );
139+
140+
efree( msg );
141+
} else {
142+
intl_errors_set_custom_msg( INTL_DATA_ERROR_P(mfo), "Creating message formatter failed", 0 );
143+
}
144+
/* Reset custom error message as this is a static method that has no object */
145+
intl_errors_reset(INTL_DATA_ERROR_P(mfo));
146+
umsg_close(MSG_FORMAT_OBJECT(mfo));
139147
RETURN_FALSE;
140-
}
141-
142-
INTL_METHOD_CHECK_STATUS(mfo, "Creating message formatter failed");
148+
}
143149

144150
msgfmt_do_format(mfo, args, return_value);
145151

ext/intl/tests/gh11658.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
GitHub #11658 MessageFormatter::format() leaks memory
3+
--EXTENSIONS--
4+
intl
5+
--FILE--
6+
<?php
7+
8+
ini_set("intl.error_level", E_WARNING);
9+
10+
$s = MessageFormatter::formatMessage('en', 'some {wrong.format}', []);
11+
var_dump($s);
12+
?>
13+
--EXPECTF--
14+
Warning: MessageFormatter::formatMessage(): pattern syntax error (parse error at offset 6, after "some {", before or at "wrong.format}") in %s on line %d
15+
bool(false)

0 commit comments

Comments
 (0)