[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-cvs
Subject:    [PHP-CVS] [php-src] master: Don't rely on HT internals in datefmt_format_object
From:       Nikita Popov <noreply () php ! net>
Date:       2021-09-30 9:48:07
Message-ID: wywNsaCnpjqpoLrWABG8cmMovmPXpYyxhgDoXwwZZM () main ! php ! net
[Download RAW message or body]

Author: Nikita Popov (nikic)
Date: 2021-09-30T11:48:01+02:00

Commit: https://github.com/php/php-src/commit/6e1d9f6a0946d01b8e9eee6a67dce3e62d0b94cb
 Raw diff: https://github.com/php/php-src/commit/6e1d9f6a0946d01b8e9eee6a67dce3e62d0b94cb.diff


Don't rely on HT internals in datefmt_format_object

Use ZEND_HASH_FOREACH_VAL instead.

Changed paths:
  M  ext/intl/dateformat/dateformat_format_object.cpp


Diff:

diff --git a/ext/intl/dateformat/dateformat_format_object.cpp \
b/ext/intl/dateformat/dateformat_format_object.cpp index 2f82cfe3b9ab..6afc9e6dca50 \
                100644
--- a/ext/intl/dateformat/dateformat_format_object.cpp
+++ b/ext/intl/dateformat/dateformat_format_object.cpp
@@ -89,10 +89,7 @@ U_CFUNC PHP_FUNCTION(datefmt_format_object)
 	if (format == NULL || Z_TYPE_P(format) == IS_NULL) {
 		//nothing
 	} else if (Z_TYPE_P(format) == IS_ARRAY) {
-		HashTable		*ht	= Z_ARRVAL_P(format);
-		uint32_t         idx;
-		zval			*z;
-
+		HashTable *ht = Z_ARRVAL_P(format);
 		if (zend_hash_num_elements(ht) != 2) {
 			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
 					"datefmt_format_object: bad format; if array, it must have "
@@ -100,37 +97,31 @@ U_CFUNC PHP_FUNCTION(datefmt_format_object)
 			RETURN_FALSE;
 		}
 
-		idx = 0;
-		while (idx < ht->nNumUsed) {
-			z = &ht->arData[idx].val;
-			if (Z_TYPE_P(z) != IS_UNDEF) {
-				break;
+		uint32_t idx = 0;
+		zval *z;
+		ZEND_HASH_FOREACH_VAL(ht, z) {
+			if (!valid_format(z)) {
+				if (idx == 0) {
+					intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
+						"datefmt_format_object: bad format; the date format (first "
+						"element of the array) is not valid", 0);
+				} else {
+					ZEND_ASSERT(idx == 1 && "We checked that there are two elements above");
+					intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
+						"datefmt_format_object: bad format; the time format (second "
+						"element of the array) is not valid", 0);
+				}
+				RETURN_FALSE;
 			}
-			idx++;
-		}
-		if (idx >= ht->nNumUsed || !valid_format(z)) {
-			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
-					"datefmt_format_object: bad format; the date format (first "
-					"element of the array) is not valid", 0);
-			RETURN_FALSE;
-		}
-		dateStyle = (DateFormat::EStyle)Z_LVAL_P(z);
-
-		idx++;
-		while (idx < ht->nNumUsed) {
-			z = &ht->arData[idx].val;
-			if (Z_TYPE_P(z) != IS_UNDEF) {
-				break;
+			if (idx == 0) {
+				dateStyle = (DateFormat::EStyle)Z_LVAL_P(z);
+			} else {
+				ZEND_ASSERT(idx == 1 && "We checked that there are two elements above");
+				timeStyle = (DateFormat::EStyle)Z_LVAL_P(z);
 			}
 			idx++;
-		}
-		if (idx >= ht->nNumUsed || !valid_format(z)) {
-			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
-					"datefmt_format_object: bad format; the time format ("
-					"second element of the array) is not valid", 0);
-			RETURN_FALSE;
-		}
-		timeStyle = (DateFormat::EStyle)Z_LVAL_P(z);
+		} ZEND_HASH_FOREACH_END();
+		ZEND_ASSERT(idx == 2 && "We checked that there are two elements above");
 	} else if (Z_TYPE_P(format) == IS_LONG) {
 		if (!valid_format(format)) {
 			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic