Skip to content

Commit 5b20fab

Browse files
committed
ext/Intl: fix IntlListFormatter object error state after format() failures
1 parent 8e3befd commit 5b20fab

4 files changed

Lines changed: 36 additions & 4 deletions

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ PHP NEWS
381381
when pos.size < 2). (Oblivionsage)
382382

383383
- Intl:
384+
. Fixed IntlListFormatter::getErrorCode() and getErrorMessage() not
385+
reflecting format() failures. (Weilin Du)
384386
. Fix leak in umsg_format_helper(). (ndossche)
385387

386388
- LDAP:

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,9 @@ PHP 8.5 UPGRADE NOTES
846846
indicates more accurately which call site caused what error.
847847
Moreover, some ext/date exceptions have been wrapped inside a
848848
IntlException now.
849+
. IntlListFormatter::getErrorCode() and getErrorMessage() now reflect
850+
IntlListFormatter::format() failures.
851+
(Weilin Du)
849852

850853
- Lexbor:
851854
. An always enabled lexbor extension is added. It contains the lexbor

ext/intl/listformatter/listformatter_class.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ PHP_METHOD(IntlListFormatter, format)
124124
Z_PARAM_ARRAY_HT(ht)
125125
ZEND_PARSE_PARAMETERS_END();
126126

127+
intl_errors_reset(LISTFORMATTER_ERROR_P(obj));
128+
127129
uint32_t count = zend_hash_num_elements(ht);
128130
if (count == 0) {
129131
RETURN_EMPTY_STRING();
@@ -154,7 +156,7 @@ PHP_METHOD(IntlListFormatter, format)
154156
}
155157
efree(items);
156158
efree(itemLengths);
157-
intl_error_set(NULL, status, "Failed to convert string to UTF-16");
159+
intl_errors_set(LISTFORMATTER_ERROR_P(obj), status, "Failed to convert string to UTF-16");
158160
RETURN_FALSE;
159161
}
160162

@@ -170,7 +172,7 @@ PHP_METHOD(IntlListFormatter, format)
170172
resultLength = ulistfmt_format(LISTFORMATTER_OBJECT(obj), items, itemLengths, count, NULL, 0, &status);
171173

172174
if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) {
173-
intl_error_set(NULL, status, "Failed to format list");
175+
intl_errors_set(LISTFORMATTER_ERROR_P(obj), status, "Failed to format list");
174176
RETVAL_FALSE;
175177
goto cleanup;
176178
}
@@ -184,7 +186,7 @@ PHP_METHOD(IntlListFormatter, format)
184186
if (result) {
185187
efree(result);
186188
}
187-
intl_error_set(NULL, status, "Failed to format list");
189+
intl_errors_set(LISTFORMATTER_ERROR_P(obj), status, "Failed to format list");
188190
RETVAL_FALSE;
189191
goto cleanup;
190192
}
@@ -194,7 +196,7 @@ PHP_METHOD(IntlListFormatter, format)
194196
efree(result);
195197

196198
if (!ret) {
197-
intl_error_set(NULL, status, "Failed to convert result to UTF-8");
199+
intl_errors_set(LISTFORMATTER_ERROR_P(obj), status, "Failed to convert result to UTF-8");
198200
RETVAL_FALSE;
199201
} else {
200202
RETVAL_NEW_STR(ret);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
IntlListFormatter getErrorCode()/getErrorMessage() reflect format() failures
3+
--EXTENSIONS--
4+
intl
5+
--FILE--
6+
<?php
7+
8+
$formatter = new IntlListFormatter('en_US');
9+
10+
var_dump($formatter->format(["\x80"]));
11+
var_dump($formatter->getErrorCode() === U_INVALID_CHAR_FOUND);
12+
var_dump($formatter->getErrorMessage());
13+
14+
var_dump($formatter->format(['a', 'b']));
15+
var_dump($formatter->getErrorCode() === U_ZERO_ERROR);
16+
var_dump($formatter->getErrorMessage());
17+
18+
?>
19+
--EXPECT--
20+
bool(false)
21+
bool(true)
22+
string(81) "IntlListFormatter::format(): Failed to convert string to UTF-16: U_INVALID_CHAR_FOUND"
23+
string(7) "a and b"
24+
bool(true)
25+
string(12) "U_ZERO_ERROR"

0 commit comments

Comments
 (0)