[php-src] PR #26039: ext/intl: Fix stale global error state in MessageFormatter and IntlDateFormatter::formatObject()
| From: | ChrisAbner | Date: | Wed, 02 Dec 2026 22:22:57 +0000 |
| Subject: | [php-src] PR #26039: ext/intl: Fix stale global error state in MessageFormatter and IntlDateFormatter::formatObject() | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-41377@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/26039
Author: ChrisAbner
### The problem
ext/intl/ERROR_CONVENTIONS.md says that the last error is always stored globally. Code
that follows the documented
way of checking for errors reads intl_get_error_code() after a call. Some
MessageFormatter functions and
IntlDateFormatter::formatObject() do not keep that state right.
```php
$df = new IntlDateFormatter('en_US', IntlDateFormatter::NONE, IntlDateFormatter::NONE,
'UTC', IntlDateFormatter::GREGORIAN, 'yyyy');
$df->parse('x'); // fails with U_PARSE_ERROR
var_dump(MessageFormatter::formatMessage('en_US', '{0,', [1])); // the pattern
is invalid
var_dump(intl_error_name(intl_get_error_code()));
```
```
bool(false)
string(13) "U_PARSE_ERROR" // expected: U_UNMATCHED_BRACES
```
The failed formatMessage() reports the error of the earlier parse(). With
no earlier error it reports
U_ZERO_ERROR, so intl_is_failure(intl_get_error_code()) is false right
after a failure.
There are two kinds of bug. Some failures do not store an error code. Some functions do not reset
the error when they
start, so a successful call shows the error of an earlier call.
| Call | Before | After | Commit |
|---|---|---|---|
| MessageFormatter::formatMessage() with a pattern that ICU rejects with something
other than U_PATTERN_SYNTAX_ERROR ('{0,' gives
U_UNMATCHED_BRACES) | false, code left from an earlier call, or
U_ZERO_ERROR | false, U_UNMATCHED_BRACES | 1 |
| MessageFormatter::setPattern() with an invalid pattern | false, the
object has U_UNMATCHED_BRACES, the global code is U_ZERO_ERROR |
false, U_UNMATCHED_BRACES in both | 2 |
| IntlDateFormatter::formatObject() that succeeds after an earlier failure | the
earlier code and message stay | U_ZERO_ERROR | 3 |
| MessageFormatter::formatMessage() or parseMessage() that succeeds after
an earlier failure | code U_ZERO_ERROR, but the earlier message stays (Date
parsing failed: U_ZERO_ERROR) | U_ZERO_ERROR, message U_ZERO_ERROR
| 4 |
| IntlDateFormatter::formatObject(new stdClass) | false,
U_ZERO_ERROR | false, U_ILLEGAL_ARGUMENT_ERROR | 5 |
### The cause and the fix
Each commit fixes one row of the table and adds a test for it. In total it is 6 lines of code. No
message text
changes.
1. msgfmt_format_message() has two branches for a failed umsg_open(). The
U_PATTERN_SYNTAX_ERROR branch stores the
code and the message. The other branch stores only the message. This is a regression from
536dbd74fa1
([GH-11658](https://github.com/php/php-src/issues/11658), July 2023, PHP 8.1.22 and 8.2.9).
Before it,
INTL_METHOD_CHECK_STATUS() stored the code. PHP 8.1.2 reports
U_UNMATCHED_BRACES for formatMessage('en_US', '{0,',
[1]).
[GH-12020](https://github.com/php/php-src/issues/12020) then fixed the message and left the code
out. The fix stores
the code in that branch too.
2. msgfmt_set_pattern() keeps the ICU code in the object and sets the message in both
places, but never copies the
code to the global error. The fix copies it.
3. datefmt_format_object() is a static method, so it does not get the reset that object
methods get from their
*_METHOD_INIT_VARS macro. The fix calls intl_error_reset(NULL) on
entry, as #22500 did for IntlChar.
4. msgfmt_format_message() and msgfmt_parse_message() have the same
missing reset. They set the code back to
U_ZERO_ERROR on success through INTL_METHOD_CHECK_STATUS(), but the
message stays. The fix calls
intl_error_reset(NULL) on entry.
5. When the object is neither an IntlCalendar nor a DateTimeInterface,
datefmt_format_object() passes its local
status to intl_error_set(). At that point status is still
U_ZERO_ERROR. The fix uses
U_ILLEGAL_ARGUMENT_ERROR, like the other argument checks of the function.
### Versions and branch
All these cases are in PHP 8.4, so the PR targets PHP-8.4. The affected code is the
same in 8.5, 8.6 and master, but
the merge-up to 8.5 needs some care:
- Two 8.5 tests expect the old wrong code, so they need updating.
msgfmt_format_message_errors.phpt (#19266)
expects U_ZERO_ERROR after a failed formatMessage() call, and gets
U_ILLEGAL_ARGUMENT_ERROR with the fix (two
lines). dateformat_formatObject_error.phpt (#19196) expects U_ZERO_ERROR
for an unsupported object, and gets
U_ILLEGAL_ARGUMENT_ERROR (one line).
- There are small conflicts in msgformat_format.cpp, msgformat_attr.cpp
and dateformat_format_object.cpp, because
8.5 removed the third argument of intl_errors_set_custom_msg() and changed the text
of some messages.
I did that merge-up locally with those changes, and the ext/intl tests pass on
PHP-8.5 (476 passed, 0 failed). If
it helps, the resolved commits are on the
[intl-error-state-mergeup-85](https://github.com/ChrisAbner/php-src/commits/intl-error-state-mergeup-85)
branch of my
fork.
Locale::isRightToLeft() (new in 8.5) is also missing the reset. I will send that fix to
PHP-8.5 separately.
### Verification
- Debug build with ASan and UBSan (GCC 13.3), PHP-8.4 at e56af28005b, ICU
74.2.
- The tests depend on ICU giving U_UNMATCHED_BRACES for '{0,'. I
checked that with ICU 70.1, 74.2, 76.1 and 78.3.
- The 5 new tests fail without the fix and pass with it.
- I removed each line of the fix one at a time and rebuilt. Each removal makes its new test fail.
- ext/intl tests: 473 passed, 0 failed. dateformat_format_variant3.phpt is
an expected failure (XFAIL), as before.
- The "Before" and "After" columns come from running the same script on
PHP-8.4 with and without the fix.
### Not covered
- Other intl functions are also missing the reset on entry. For example, after a failed call,
grapheme_strlen() and
grapheme_strpos() keep the earlier code and message, and
IntlTimeZone::getWindowsID() keeps the earlier message.
This PR stays with MessageFormatter and formatObject(). I plan to send
the grapheme functions as a separate PR.
- The setPattern() message still says "Error setting symbol value" (copied
from numfmt_set_symbol()). Changing it
breaks msgfmt_get_set_pattern.phpt, and it is not needed for this fix.