Commit d315d7ade85 for php

commit d315d7ade85eb06c394357cea78ebecb1840b537
Author: ChrisAbner <chrisabner@hotmail.com>
Date:   Thu Dec 3 02:56:19 2026 +0000

    ext/intl: Fix stale global error state in MessageFormatter and formatObject()

    Some MessageFormatter functions and IntlDateFormatter::formatObject() did
    not keep the global intl error right. Failures in formatMessage() (other
    than U_PATTERN_SYNTAX_ERROR), setPattern() and formatObject() on an
    unsupported object did not store an error code, and the static
    formatMessage(), parseMessage() and formatObject() did not reset the error
    on entry, so a successful call reported the error of an earlier one.

    Close GH-26039

diff --git a/NEWS b/NEWS
index b4e6149982d..804773c6417 100644
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,10 @@ PHP                                                                        NEWS
   . Fixed enchant_broker_set_dict_path() and enchant_broker_get_dict_path()
     returning null with libenchant 2. Both now return false. (lacatoire)

+- Intl:
+  . Fixed stale or missing global error code and message in MessageFormatter
+    and IntlDateFormatter::formatObject(). (ChrisAbner)
+
 - Standard:
   . Fixed bug GH-25959 ("consumed" stream filter loses or repeats data).
     (Artem Ukrainskiy)
diff --git a/ext/intl/dateformat/dateformat_format_object.cpp b/ext/intl/dateformat/dateformat_format_object.cpp
index c5dc9acd9d5..e1ad7e20a77 100644
--- a/ext/intl/dateformat/dateformat_format_object.cpp
+++ b/ext/intl/dateformat/dateformat_format_object.cpp
@@ -77,6 +77,8 @@ U_CFUNC PHP_FUNCTION(datefmt_format_object)
 	DateFormat::EStyle	dateStyle = DateFormat::kDefault,
 						timeStyle = DateFormat::kDefault;

+	intl_error_reset(NULL);
+
 	ZEND_PARSE_PARAMETERS_START(1, 3)
 		Z_PARAM_OBJECT(object)
 		Z_PARAM_OPTIONAL
@@ -182,7 +184,7 @@ U_CFUNC PHP_FUNCTION(datefmt_format_object)
 			goto cleanup;
 		}
 	} else {
-		intl_error_set(NULL, status, "datefmt_format_object: the passed object "
+		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_format_object: the passed object "
 				"must be an instance of either IntlCalendar or DateTimeInterface",
 				0);
 		RETURN_FALSE;
diff --git a/ext/intl/msgformat/msgformat_attr.c b/ext/intl/msgformat/msgformat_attr.c
index 25c960b956b..9670fc87160 100644
--- a/ext/intl/msgformat/msgformat_attr.c
+++ b/ext/intl/msgformat/msgformat_attr.c
@@ -86,6 +86,7 @@ PHP_FUNCTION( msgfmt_set_pattern )
 		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_error_set_code(NULL, INTL_DATA_ERROR_CODE(mfo));
 		intl_errors_set_custom_msg(INTL_DATA_ERROR_P(mfo), msg, 1);
 		efree(msg);
 		RETURN_FALSE;
diff --git a/ext/intl/msgformat/msgformat_format.c b/ext/intl/msgformat/msgformat_format.c
index d948c64bc03..53b9f598b02 100644
--- a/ext/intl/msgformat/msgformat_format.c
+++ b/ext/intl/msgformat/msgformat_format.c
@@ -89,6 +89,8 @@ PHP_FUNCTION( msgfmt_format_message )
 	MessageFormatter_object *mfo = &mf;
 	UParseError parse_error;

+	intl_error_reset(NULL);
+
 	/* Parse parameters. */
 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "ssa",
 		  &slocale, &slocale_len, &pattern, &pattern_len, &args ) == FAILURE )
@@ -147,6 +149,7 @@ PHP_FUNCTION( msgfmt_format_message )

 			efree( msg );
 		} else {
+			intl_error_set_code(/* intl_error* */ NULL, INTL_DATA_ERROR_CODE( mfo ) );
 			intl_errors_set_custom_msg(/* intl_error* */ NULL, "Creating message formatter failed", 0 );
 		}
 		umsg_close(MSG_FORMAT_OBJECT(mfo));
diff --git a/ext/intl/msgformat/msgformat_parse.c b/ext/intl/msgformat/msgformat_parse.c
index cf84aed65b6..411f4968c07 100644
--- a/ext/intl/msgformat/msgformat_parse.c
+++ b/ext/intl/msgformat/msgformat_parse.c
@@ -86,6 +86,8 @@ PHP_FUNCTION( msgfmt_parse_message )
 	MessageFormatter_object mf;
 	MessageFormatter_object *mfo = &mf;

+	intl_error_reset(NULL);
+
 	ZEND_PARSE_PARAMETERS_START(3, 3)
 		Z_PARAM_STRING(slocale, slocale_len)
 		Z_PARAM_STRING(pattern, pattern_len)
diff --git a/ext/intl/tests/datefmt_format_object_error_reset.phpt b/ext/intl/tests/datefmt_format_object_error_reset.phpt
new file mode 100644
index 00000000000..377b8d61eca
--- /dev/null
+++ b/ext/intl/tests/datefmt_format_object_error_reset.phpt
@@ -0,0 +1,28 @@
+--TEST--
+IntlDateFormatter::formatObject() resets the intl error on success
+--EXTENSIONS--
+intl
+--FILE--
+<?php
+$df = new IntlDateFormatter('en_US', IntlDateFormatter::NONE, IntlDateFormatter::NONE, 'UTC', IntlDateFormatter::GREGORIAN, 'yyyy');
+$date = new DateTime('@0');
+
+var_dump($df->parse('x'));
+var_dump(intl_error_name(intl_get_error_code()));
+var_dump(IntlDateFormatter::formatObject($date, 'yyyy', 'en_US'));
+var_dump(intl_error_name(intl_get_error_code()));
+var_dump(intl_get_error_message());
+
+var_dump($df->parse('x'));
+var_dump(datefmt_format_object($date, 'yyyy', 'en_US'));
+var_dump(intl_error_name(intl_get_error_code()));
+?>
+--EXPECT--
+bool(false)
+string(13) "U_PARSE_ERROR"
+string(4) "1970"
+string(12) "U_ZERO_ERROR"
+string(12) "U_ZERO_ERROR"
+bool(false)
+string(4) "1970"
+string(12) "U_ZERO_ERROR"
diff --git a/ext/intl/tests/datefmt_format_object_invalid_object.phpt b/ext/intl/tests/datefmt_format_object_invalid_object.phpt
new file mode 100644
index 00000000000..33415c04ea0
--- /dev/null
+++ b/ext/intl/tests/datefmt_format_object_invalid_object.phpt
@@ -0,0 +1,19 @@
+--TEST--
+IntlDateFormatter::formatObject() sets an error code when the object is not supported
+--EXTENSIONS--
+intl
+--FILE--
+<?php
+var_dump(IntlDateFormatter::formatObject(new stdClass));
+var_dump(intl_error_name(intl_get_error_code()));
+var_dump(intl_is_failure(intl_get_error_code()));
+
+var_dump(datefmt_format_object(new stdClass));
+var_dump(intl_error_name(intl_get_error_code()));
+?>
+--EXPECT--
+bool(false)
+string(24) "U_ILLEGAL_ARGUMENT_ERROR"
+bool(true)
+bool(false)
+string(24) "U_ILLEGAL_ARGUMENT_ERROR"
diff --git a/ext/intl/tests/msgfmt_format_message_error_code.phpt b/ext/intl/tests/msgfmt_format_message_error_code.phpt
new file mode 100644
index 00000000000..95451376c4b
--- /dev/null
+++ b/ext/intl/tests/msgfmt_format_message_error_code.phpt
@@ -0,0 +1,28 @@
+--TEST--
+MessageFormatter::formatMessage() stores the error code when the pattern is rejected
+--EXTENSIONS--
+intl
+--FILE--
+<?php
+// A failure in another class leaves U_PARSE_ERROR behind.
+$df = new IntlDateFormatter('en_US', IntlDateFormatter::NONE, IntlDateFormatter::NONE, 'UTC', IntlDateFormatter::GREGORIAN, 'yyyy');
+var_dump($df->parse('x'));
+var_dump(intl_error_name(intl_get_error_code()));
+
+var_dump(MessageFormatter::formatMessage('en_US', '{0,', [1]));
+var_dump(intl_error_name(intl_get_error_code()));
+var_dump(intl_get_error_message());
+
+var_dump($df->parse('x'));
+var_dump(msgfmt_format_message('en_US', '{0,', [1]));
+var_dump(intl_error_name(intl_get_error_code()));
+?>
+--EXPECTF--
+bool(false)
+string(13) "U_PARSE_ERROR"
+bool(false)
+string(18) "U_UNMATCHED_BRACES"
+string(%d) "%SCreating message formatter failed: U_UNMATCHED_BRACES"
+bool(false)
+bool(false)
+string(18) "U_UNMATCHED_BRACES"
diff --git a/ext/intl/tests/msgfmt_set_pattern_error_code.phpt b/ext/intl/tests/msgfmt_set_pattern_error_code.phpt
new file mode 100644
index 00000000000..3c5b2b7388f
--- /dev/null
+++ b/ext/intl/tests/msgfmt_set_pattern_error_code.phpt
@@ -0,0 +1,26 @@
+--TEST--
+MessageFormatter::setPattern() stores the error code globally when the pattern is rejected
+--EXTENSIONS--
+intl
+--FILE--
+<?php
+$mf = new MessageFormatter('en_US', '{0}');
+var_dump($mf->setPattern('{0,'));
+var_dump(intl_error_name(intl_get_error_code()));
+var_dump(intl_get_error_message());
+var_dump(intl_error_name($mf->getErrorCode()));
+var_dump($mf->getErrorMessage());
+var_dump($mf->getPattern());
+
+var_dump(msgfmt_set_pattern($mf, '{0,'));
+var_dump(intl_is_failure(intl_get_error_code()));
+?>
+--EXPECTF--
+bool(false)
+string(18) "U_UNMATCHED_BRACES"
+string(%d) "%SError setting symbol value at line %i, offset %i: U_UNMATCHED_BRACES"
+string(18) "U_UNMATCHED_BRACES"
+string(%d) "%SError setting symbol value at line %i, offset %i: U_UNMATCHED_BRACES"
+string(3) "{0}"
+bool(false)
+bool(true)
diff --git a/ext/intl/tests/msgfmt_static_error_reset.phpt b/ext/intl/tests/msgfmt_static_error_reset.phpt
new file mode 100644
index 00000000000..73cf82cc23f
--- /dev/null
+++ b/ext/intl/tests/msgfmt_static_error_reset.phpt
@@ -0,0 +1,49 @@
+--TEST--
+MessageFormatter::formatMessage() and parseMessage() reset the intl error on success
+--EXTENSIONS--
+intl
+--FILE--
+<?php
+$df = new IntlDateFormatter('en_US', IntlDateFormatter::NONE, IntlDateFormatter::NONE, 'UTC', IntlDateFormatter::GREGORIAN, 'yyyy');
+
+var_dump($df->parse('x'));
+var_dump(intl_get_error_message());
+var_dump(MessageFormatter::formatMessage('en_US', '{0}', [1]));
+var_dump(intl_error_name(intl_get_error_code()));
+var_dump(intl_get_error_message());
+
+var_dump($df->parse('x'));
+var_dump(MessageFormatter::parseMessage('en_US', '{0}', '1'));
+var_dump(intl_error_name(intl_get_error_code()));
+var_dump(intl_get_error_message());
+
+var_dump($df->parse('x'));
+var_dump(msgfmt_format_message('en_US', '{0}', [1]));
+var_dump(intl_get_error_message());
+
+var_dump($df->parse('x'));
+var_dump(msgfmt_parse_message('en_US', '{0}', '1'));
+var_dump(intl_get_error_message());
+?>
+--EXPECTF--
+bool(false)
+string(%d) "%SDate parsing failed: U_PARSE_ERROR"
+string(1) "1"
+string(12) "U_ZERO_ERROR"
+string(12) "U_ZERO_ERROR"
+bool(false)
+array(1) {
+  [0]=>
+  string(1) "1"
+}
+string(12) "U_ZERO_ERROR"
+string(12) "U_ZERO_ERROR"
+bool(false)
+string(1) "1"
+string(12) "U_ZERO_ERROR"
+bool(false)
+array(1) {
+  [0]=>
+  string(1) "1"
+}
+string(12) "U_ZERO_ERROR"