[php-src] PHP-8.6: Merge branch 'PHP-8.4' into PHP-8.5
| From: | Weilin Du | Date: | Tue, 24 Nov 2026 20:54:59 +0000 |
| Subject: | [php-src] PHP-8.6: Merge branch 'PHP-8.4' into PHP-8.5 | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-144135@lists.php.net to get a copy of this message | ||
Author: Weilin Du (LamentXU123)
Date: 2026-11-25T04:52:44+08:00
Commit: https://github.com/php/php-src/commit/55f3138c5f1308aa90646fd91db336defd249b3a
Raw diff: https://github.com/php/php-src/commit/55f3138c5f1308aa90646fd91db336defd249b3a.diff
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4:
ext/intl: Preserve the previous MessageFormatter pattern on failure (#25642)
Changed paths:
A ext/intl/tests/msgfmt_set_pattern_preserves_state.phpt
M NEWS
M ext/intl/msgformat/msgformat_attr.cpp
Diff:
diff --git a/NEWS b/NEWS
index 8f684032bfcb..9138f3586af8 100644
--- a/NEWS
+++ b/NEWS
@@ -77,6 +77,8 @@ PHP NEWS
. Fixed Locale::lookup() ignoring the default locale when the language tag
list is empty. (Weilin Du)
. 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.cpp b/ext/intl/msgformat/msgformat_attr.cpp
index e7ec006e5e79..da68ef6fa955 100644
--- a/ext/intl/msgformat/msgformat_attr.cpp
+++ b/ext/intl/msgformat/msgformat_attr.cpp
@@ -16,6 +16,10 @@
#include <config.h>
#endif
+#include <memory>
+
+#include <unicode/msgfmt.h>
+
extern "C" {
#include "php_intl.h"
#include "msgformat_class.h"
@@ -78,7 +82,13 @@ U_CFUNC PHP_FUNCTION( msgfmt_set_pattern )
}
#endif
- umsg_applyPattern(MSG_FORMAT_OBJECT(mfo), spattern, spattern_len, &spattern_error,
&INTL_DATA_ERROR_CODE(mfo));
+ icu::MessageFormat *mf = reinterpret_cast<icu::MessageFormat *>(MSG_FORMAT_OBJECT(mfo));
+ std::unique_ptr<icu::MessageFormat> new_mf(static_cast<icu::MessageFormat
*>(mf->clone()));
+ if (!new_mf) {
+ INTL_DATA_ERROR_CODE(mfo) = U_MEMORY_ALLOCATION_ERROR;
+ } else {
+ new_mf->applyPattern(icu::UnicodeString(spattern, spattern_len), spattern_error,
INTL_DATA_ERROR_CODE(mfo));
+ }
if (spattern) {
efree(spattern);
}
@@ -90,6 +100,9 @@ U_CFUNC PHP_FUNCTION( msgfmt_set_pattern )
RETURN_FALSE;
}
+ delete mf;
+ MSG_FORMAT_OBJECT(mfo) = reinterpret_cast<UMessageFormat *>(new_mf.release());
+ 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)