Skip to content

ext/intl: Preserve the previous MessageFormatter pattern on failure - #25642

Merged
LamentXU123 merged 1 commit into
php:PHP-8.4from
LamentXU123:intl-84-preserve-message-pattern
Nov 24, 2026
Merged

LamentXU123 merged 1 commit into
php:PHP-8.4from
LamentXU123:intl-84-preserve-message-pattern

Conversation

@LamentXU123

Copy link
Copy Markdown
Member

When MessageFormatter::setPattern() rejects an invalid pattern getPattern() still returns the previous pattern but ICU may already have discarded it.

$fmt = new MessageFormatter('en', 'Hello, {0}!');
$fmt->setPattern('{0,date');   // false
echo $fmt->format(['world']);  // Before: {}  After: Hello, world!

This is originally reported by @wwkenwong

@@ -76,18 +76,24 @@ PHP_FUNCTION( msgfmt_set_pattern )
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
        }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
LamentXU123 force-pushed the intl-84-preserve-message-pattern branch from dea6dcc to bd6f8d8 Compare November 24, 2026 19:55
@LamentXU123
LamentXU123 merged commit 3f28d39 into php:PHP-8.4 Nov 24, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants