Repository navigation
ext/intl: Preserve the previous MessageFormatter pattern on failure - #25642
Merged
LamentXU123 merged 1 commit intoNov 24, 2026
Merged
Conversation
devnexen
requested changes
Nov 24, 2026
| @@ -76,18 +76,24 @@ PHP_FUNCTION( msgfmt_set_pattern ) | |||
| } | |||
Member
There was a problem hiding this comment.
diff --git a/ext/intl/msgformat/msgformat_attr.c b/ext/intl/msgformat/msgformat_attr.c
index b56bf360d11..a406d75b984 100644
--- a/ext/intl/msgformat/msgformat_attr.c
+++ b/ext/intl/msgformat/msgformat_attr.c
@@ -76,18 +76,23 @@ PHP_FUNCTION( msgfmt_set_pattern )
}
#endif
- umsg_applyPattern(MSG_FORMAT_OBJECT(mfo), spattern, spattern_len, &spattern_error, &INTL_DATA_ERROR_CODE(mfo));
+ 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);
}and for >= 8.5 something like this
diff --git a/ext/intl/msgformat/msgformat_attr.cpp b/ext/intl/msgformat/msgformat_attr.cpp
index bb7b2164015..c6614c6d136 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>
+
#include "php_intl.h"
#include "msgformat_class.h"
#include "msgformat_data.h"
@@ -76,7 +80,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);
}
@@ -88,6 +98,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);
}
Member
Author
There was a problem hiding this comment.
Hmm, I think this is correct cuz it effectively prevent the regression from setPattern('{0} {0,number}')
Co-authored-by: David Carlier <devnexen@gmail.com>
LamentXU123
force-pushed
the
intl-84-preserve-message-pattern
branch
from
November 24, 2026 19:55
dea6dcc to
bd6f8d8
Compare
devnexen
approved these changes
Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
MessageFormatter::setPattern()rejects an invalid patterngetPattern()still returns the previous pattern but ICU may already have discarded it.This is originally reported by @wwkenwong