Skip to content

Commit 2aaa799

Browse files
committed
Merge branch 'PHP-8.2'
* PHP-8.2: ext/intl: Fix memory leak in MessageFormatter::format()
2 parents c1a7058 + 1e41b0f commit 2aaa799

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ PHP NEWS
1414
. Added zend_call_stack_get implementation for OpenBSD. (David Carlier)
1515
. Fixed oss-fuzz #60411 (Fix double-compilation of arrow-functions). (ilutov)
1616

17+
- Intl:
18+
. Fix memory leak in MessageFormatter::format() on failure. (Girgias)
19+
1720
- LDAP:
1821
. Deprecate calling ldap_connect() with separate hostname and port.
1922
(heiglandreas)

ext/intl/msgformat/msgformat_format.c

+20-14
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

+15
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)