From 0b6063f33112b4685bc3c23ae823556fec8f32f1 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Thu, 11 Oct 2018 16:08:58 +0200 Subject: Restore array_key_exists() compatibility for ArrayObject Doing this by special-casing array_key_exists() for ArrayObject. --- ext/spl/spl_array.c | 1 + ext/spl/tests/bug61347.phpt | 4 ++-- ext/standard/array.c | 38 +++++++++++++++++++++----------------- 3 files changed, 24 insertions(+), 19 deletions(-) (limited to 'ext') diff --git a/ext/spl/spl_array.c b/ext/spl/spl_array.c index 9d7a51af8e..04e73bf26e 100644 --- a/ext/spl/spl_array.c +++ b/ext/spl/spl_array.c @@ -805,6 +805,7 @@ static HashTable *spl_array_get_properties_for(zval *object, zend_prop_purpose p break; case ZEND_PROP_PURPOSE_VAR_EXPORT: case ZEND_PROP_PURPOSE_JSON: + case _ZEND_PROP_PURPOSE_ARRAY_KEY_EXISTS: dup = 0; break; default: diff --git a/ext/spl/tests/bug61347.phpt b/ext/spl/tests/bug61347.phpt index 720cf5b0f2..cb091858ae 100644 --- a/ext/spl/tests/bug61347.phpt +++ b/ext/spl/tests/bug61347.phpt @@ -12,7 +12,7 @@ var_dump(isset($b['no_exists'])); //false var_dump(empty($b['b'])); //true var_dump(empty($b[37])); //true -var_dump(array_key_exists('b', $b)); //false +var_dump(array_key_exists('b', $b)); //true var_dump($b['b']); $a = array('b' => '', 37 => false); @@ -31,7 +31,7 @@ bool(false) bool(false) bool(true) bool(true) -bool(false) +bool(true) NULL bool(true) bool(true) diff --git a/ext/standard/array.c b/ext/standard/array.c index 8ebf4e1789..4d94c54ff1 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -6247,34 +6247,38 @@ PHP_FUNCTION(array_map) Checks if the given key or index exists in the array */ PHP_FUNCTION(array_key_exists) { - zval *key; /* key to check for */ - HashTable *array; /* array to check in */ + zval *key; + zval *array; + HashTable *ht; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_ZVAL(key) - Z_PARAM_ARRAY_OR_OBJECT_HT(array) + Z_PARAM_ARRAY_OR_OBJECT(array) ZEND_PARSE_PARAMETERS_END(); + if (EXPECTED(Z_TYPE_P(array) == IS_ARRAY)) { + ht = Z_ARRVAL_P(array); + } else { + ht = zend_get_properties_for(array, ZEND_PROP_PURPOSE_ARRAY_CAST); + } + switch (Z_TYPE_P(key)) { case IS_STRING: - if (zend_symtable_exists_ind(array, Z_STR_P(key))) { - RETURN_TRUE; - } - RETURN_FALSE; + RETVAL_BOOL(zend_symtable_exists_ind(ht, Z_STR_P(key))); + break; case IS_LONG: - if (zend_hash_index_exists(array, Z_LVAL_P(key))) { - RETURN_TRUE; - } - RETURN_FALSE; + RETVAL_BOOL(zend_hash_index_exists(ht, Z_LVAL_P(key))); + break; case IS_NULL: - if (zend_hash_exists_ind(array, ZSTR_EMPTY_ALLOC())) { - RETURN_TRUE; - } - RETURN_FALSE; - + RETVAL_BOOL(zend_hash_exists_ind(ht, ZSTR_EMPTY_ALLOC())); + break; default: php_error_docref(NULL, E_WARNING, "The first argument should be either a string or an integer"); - RETURN_FALSE; + RETVAL_FALSE; + } + + if (Z_TYPE_P(array) != IS_ARRAY) { + zend_release_properties(ht); } } /* }}} */ -- cgit v1.2.1