Skip to content

Commit 6e1d9f6

Browse files
committed
Don't rely on HT internals in datefmt_format_object
Use ZEND_HASH_FOREACH_VAL instead.
1 parent 5bda4cd commit 6e1d9f6

File tree

1 file changed

+23
-32
lines changed

1 file changed

+23
-32
lines changed

ext/intl/dateformat/dateformat_format_object.cpp

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -89,48 +89,39 @@ U_CFUNC PHP_FUNCTION(datefmt_format_object)
8989
if (format == NULL || Z_TYPE_P(format) == IS_NULL) {
9090
//nothing
9191
} else if (Z_TYPE_P(format) == IS_ARRAY) {
92-
HashTable *ht = Z_ARRVAL_P(format);
93-
uint32_t idx;
94-
zval *z;
95-
92+
HashTable *ht = Z_ARRVAL_P(format);
9693
if (zend_hash_num_elements(ht) != 2) {
9794
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
9895
"datefmt_format_object: bad format; if array, it must have "
9996
"two elements", 0);
10097
RETURN_FALSE;
10198
}
10299

103-
idx = 0;
104-
while (idx < ht->nNumUsed) {
105-
z = &ht->arData[idx].val;
106-
if (Z_TYPE_P(z) != IS_UNDEF) {
107-
break;
100+
uint32_t idx = 0;
101+
zval *z;
102+
ZEND_HASH_FOREACH_VAL(ht, z) {
103+
if (!valid_format(z)) {
104+
if (idx == 0) {
105+
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
106+
"datefmt_format_object: bad format; the date format (first "
107+
"element of the array) is not valid", 0);
108+
} else {
109+
ZEND_ASSERT(idx == 1 && "We checked that there are two elements above");
110+
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
111+
"datefmt_format_object: bad format; the time format (second "
112+
"element of the array) is not valid", 0);
113+
}
114+
RETURN_FALSE;
108115
}
109-
idx++;
110-
}
111-
if (idx >= ht->nNumUsed || !valid_format(z)) {
112-
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
113-
"datefmt_format_object: bad format; the date format (first "
114-
"element of the array) is not valid", 0);
115-
RETURN_FALSE;
116-
}
117-
dateStyle = (DateFormat::EStyle)Z_LVAL_P(z);
118-
119-
idx++;
120-
while (idx < ht->nNumUsed) {
121-
z = &ht->arData[idx].val;
122-
if (Z_TYPE_P(z) != IS_UNDEF) {
123-
break;
116+
if (idx == 0) {
117+
dateStyle = (DateFormat::EStyle)Z_LVAL_P(z);
118+
} else {
119+
ZEND_ASSERT(idx == 1 && "We checked that there are two elements above");
120+
timeStyle = (DateFormat::EStyle)Z_LVAL_P(z);
124121
}
125122
idx++;
126-
}
127-
if (idx >= ht->nNumUsed || !valid_format(z)) {
128-
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
129-
"datefmt_format_object: bad format; the time format ("
130-
"second element of the array) is not valid", 0);
131-
RETURN_FALSE;
132-
}
133-
timeStyle = (DateFormat::EStyle)Z_LVAL_P(z);
123+
} ZEND_HASH_FOREACH_END();
124+
ZEND_ASSERT(idx == 2 && "We checked that there are two elements above");
134125
} else if (Z_TYPE_P(format) == IS_LONG) {
135126
if (!valid_format(format)) {
136127
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,

0 commit comments

Comments
 (0)