[php-src] PHP-8.4: ext/intl: Preserve the previous MessageFormatter pattern on failure (#25642)
| From: | Weilin Du via GitHub | Date: | Tue, 24 Nov 2026 20:40:13 +0000 |
| Subject: | [php-src] PHP-8.4: ext/intl: Preserve the previous MessageFormatter pattern on failure (#25642) | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-144133@lists.php.net to get a copy of this message | ||
Author: Weilin Du (LamentXU123)
Committer: GitHub (web-flow)
Pusher: LamentXU123
Date: 2026-11-25T04:40:09+08:00
Commit: https://github.com/php/php-src/commit/3f28d393ef5b9feb8538625c9df9ed3f828fbf76
Raw diff: https://github.com/php/php-src/commit/3f28d393ef5b9feb8538625c9df9ed3f828fbf76.diff
ext/intl: Preserve the previous MessageFormatter pattern on failure (#25642)
Co-authored-by: David Carlier <devnexen@gmail.com>
Changed paths:
A ext/intl/tests/msgfmt_set_pattern_preserves_state.phpt
M NEWS
M ext/intl/msgformat/msgformat_attr.c
Diff:
diff --git a/NEWS b/NEWS
index de80e308a4df..3cdfc7969523 100644
--- a/NEWS
+++ b/NEWS
@@ -87,6 +87,8 @@ PHP NEWS
. Fixed intl_error_name() truncating error codes outside the int range.
(lacatoire)
. Fixed IntlCalendar::setTime() accepting non-finite timestamps. (Weilin Du)
+ . Fixed MessageFormatter::setPattern() losing the previous pattern when
+ the new pattern is invalid. (Weilin Du, David Carlier)
- JSON:
. Fixed json_decode() and json_validate() error messages. (lacatoire)
diff --git a/ext/intl/msgformat/msgformat_attr.c b/ext/intl/msgformat/msgformat_attr.c
index b56bf360d114..25c960b956b2 100644
--- a/ext/intl/msgformat/msgformat_attr.c
+++ b/ext/intl/msgformat/msgformat_attr.c
@@ -76,18 +76,24 @@ PHP_FUNCTION( msgfmt_set_pattern )
}
#endif
- umsg_applyPattern(MSG_FORMAT_OBJECT(mfo), spattern, spattern_len, &spattern_error,
&INTL_DATA_ERROR_CODE(mfo));
+ /* ICU may discard the old pattern if parsing the new one fails. */
+ UMessageFormat *new_format = umsg_clone(MSG_FORMAT_OBJECT(mfo), &INTL_DATA_ERROR_CODE(mfo));
+ umsg_applyPattern(new_format, spattern, spattern_len, &spattern_error,
&INTL_DATA_ERROR_CODE(mfo));
if (spattern) {
efree(spattern);
}
if (U_FAILURE(INTL_DATA_ERROR_CODE(mfo))) {
char *msg;
+ umsg_close(new_format);
spprintf(&msg, 0, "Error setting symbol value at line %d, offset %d",
spattern_error.line, spattern_error.offset);
intl_errors_set_custom_msg(INTL_DATA_ERROR_P(mfo), msg, 1);
efree(msg);
RETURN_FALSE;
}
+ umsg_close(MSG_FORMAT_OBJECT(mfo));
+ MSG_FORMAT_OBJECT(mfo) = new_format;
+ mfo->mf_data.tz_set = 0;
if(mfo->mf_data.orig_format) {
efree(mfo->mf_data.orig_format);
}
diff --git a/ext/intl/tests/msgfmt_set_pattern_preserves_state.phpt
b/ext/intl/tests/msgfmt_set_pattern_preserves_state.phpt
new file mode 100644
index 000000000000..e26b42b3ed35
--- /dev/null
+++ b/ext/intl/tests/msgfmt_set_pattern_preserves_state.phpt
@@ -0,0 +1,28 @@
+--TEST--
+MessageFormatter::setPattern() preserves the previous pattern on failure
+--CREDITS--
+Wong Wai Kin
+--EXTENSIONS--
+intl
+--FILE--
+<?php
+$formatter = new MessageFormatter('en', 'Hello, {0}!');
+var_dump($formatter->format(['world']));
+
+var_dump($formatter->setPattern('{0,date'));
+var_dump($formatter->getPattern());
+var_dump($formatter->format(['world']));
+
+var_dump($formatter->setPattern('{0,number,integer} items'));
+var_dump($formatter->format([2]));
+
+var_dump($formatter->setPattern('{0} {0,number}'));
+?>
+--EXPECT--
+string(13) "Hello, world!"
+bool(false)
+string(11) "Hello, {0}!"
+string(13) "Hello, world!"
+bool(true)
+string(7) "2 items"
+bool(true)