Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion ext/intl/msgformat/msgformat_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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}')

#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);
}
Expand Down
28 changes: 28 additions & 0 deletions ext/intl/tests/msgfmt_set_pattern_preserves_state.phpt
Original file line number Diff line number Diff line change
@@ -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)
Loading