diff options
author | Máté Kocsis <kocsismate@woohoolabs.com> | 2019-11-04 19:44:48 +0100 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2019-11-06 12:56:47 +0100 |
commit | 14bdb0cfc7b62205c75f6d5c783e343259796776 (patch) | |
tree | 3286c308a90d7dcc88989a641f87bf4bd6152435 /Zend | |
parent | 0b0d4eb0eb4336d50c6d5d645a1838e26ca9c181 (diff) | |
download | php-git-14bdb0cfc7b62205c75f6d5c783e343259796776.tar.gz |
Fix consistency issues with array accesses warnings/exceptions
* Change a number of "resource used as offset" notices to warnings,
which were previously missed.
* Throw the "resource used as offset" warning for isset() as well.
* Make array_key_exists() behavior with regard to different key
types consistent with isset() and normal array accesses. All key
types now use the usual coercions and array/object keys throw
TypeError.
Closes GH-4887.
Diffstat (limited to 'Zend')
-rw-r--r-- | Zend/tests/const_array_with_resource_key.phpt | 2 | ||||
-rw-r--r-- | Zend/tests/isset_003.phpt | 2 | ||||
-rw-r--r-- | Zend/tests/isset_array.phpt | 48 | ||||
-rw-r--r-- | Zend/zend_API.c | 2 | ||||
-rw-r--r-- | Zend/zend_ast.c | 4 | ||||
-rw-r--r-- | Zend/zend_execute.c | 16 |
6 files changed, 68 insertions, 6 deletions
diff --git a/Zend/tests/const_array_with_resource_key.phpt b/Zend/tests/const_array_with_resource_key.phpt index 0bf546af50..1e3dee4734 100644 --- a/Zend/tests/const_array_with_resource_key.phpt +++ b/Zend/tests/const_array_with_resource_key.phpt @@ -8,7 +8,7 @@ var_dump(FOO); ?> --EXPECTF-- -Notice: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d +Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d array(1) { [%d]=> int(42) diff --git a/Zend/tests/isset_003.phpt b/Zend/tests/isset_003.phpt index c80a174daf..7b29c1e6b8 100644 --- a/Zend/tests/isset_003.phpt +++ b/Zend/tests/isset_003.phpt @@ -1,5 +1,5 @@ --TEST-- -Testing isset accessing undefined array itens and properties +Testing isset accessing undefined array items and properties --FILE-- <?php diff --git a/Zend/tests/isset_array.phpt b/Zend/tests/isset_array.phpt new file mode 100644 index 0000000000..c7c1876410 --- /dev/null +++ b/Zend/tests/isset_array.phpt @@ -0,0 +1,48 @@ +--TEST-- +Using isset() with arrays +--FILE-- +<?php + +$array = [ + 0 => true, + "a" => true, +]; + +var_dump(isset($array[0])); + +var_dump(isset($array["a"])); + +var_dump(isset($array[false])); + +var_dump(isset($array[0.6])); + +var_dump(isset($array[true])); + +var_dump(isset($array[null])); + +var_dump(isset($array[STDIN])); + +try { + isset($array[[]]); +} catch (TypeError $exception) { + echo $exception->getMessage() . "\n"; +} + +try { + isset($array[new stdClass()]); +} catch (TypeError $exception) { + echo $exception->getMessage() . "\n"; +} +?> +--EXPECTF-- +bool(true) +bool(true) +bool(true) +bool(true) +bool(false) +bool(false) + +Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d +bool(false) +Illegal offset type in isset or empty +Illegal offset type in isset or empty diff --git a/Zend/zend_API.c b/Zend/zend_API.c index 5781a95bfd..387f6e1a98 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -1531,7 +1531,7 @@ ZEND_API int array_set_zval_key(HashTable *ht, zval *key, zval *value) /* {{{ */ result = zend_symtable_update(ht, ZSTR_EMPTY_ALLOC(), value); break; case IS_RESOURCE: - zend_error(E_NOTICE, "Resource ID#%d used as offset, casting to integer (%d)", Z_RES_HANDLE_P(key), Z_RES_HANDLE_P(key)); + zend_error(E_WARNING, "Resource ID#%d used as offset, casting to integer (%d)", Z_RES_HANDLE_P(key), Z_RES_HANDLE_P(key)); result = zend_hash_index_update(ht, Z_RES_HANDLE_P(key), value); break; case IS_FALSE: diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c index 660e6d5b6f..f0b524b30e 100644 --- a/Zend/zend_ast.c +++ b/Zend/zend_ast.c @@ -436,7 +436,7 @@ static int zend_ast_add_array_element(zval *result, zval *offset, zval *expr) zend_hash_index_update(Z_ARRVAL_P(result), zend_dval_to_lval(Z_DVAL_P(offset)), expr); break; case IS_RESOURCE: - zend_error(E_NOTICE, "Resource ID#%d used as offset, casting to integer (%d)", Z_RES_HANDLE_P(offset), Z_RES_HANDLE_P(offset)); + zend_error(E_WARNING, "Resource ID#%d used as offset, casting to integer (%d)", Z_RES_HANDLE_P(offset), Z_RES_HANDLE_P(offset)); zend_hash_index_update(Z_ARRVAL_P(result), Z_RES_HANDLE_P(offset), expr); break; default: @@ -451,7 +451,7 @@ static int zend_ast_add_unpacked_element(zval *result, zval *expr) { HashTable *ht = Z_ARRVAL_P(expr); zval *val; zend_string *key; - + ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) { if (key) { zend_throw_error(NULL, "Cannot unpack array with string keys"); diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index 345335b708..8278f698cd 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -2369,6 +2369,7 @@ str_idx: hval = 1; goto num_idx; } else if (Z_TYPE_P(offset) == IS_RESOURCE) { + zend_use_resource_as_offset(offset); hval = Z_RES_HANDLE_P(offset); goto num_idx; } else if (/*OP2_TYPE == IS_CV &&*/ Z_TYPE_P(offset) == IS_UNDEF) { @@ -2478,6 +2479,19 @@ num_key: } else if (EXPECTED(Z_ISREF_P(key))) { key = Z_REFVAL_P(key); goto try_again; + } else if (Z_TYPE_P(key) == IS_DOUBLE) { + hval = zend_dval_to_lval(Z_DVAL_P(key)); + goto num_key; + } else if (Z_TYPE_P(key) == IS_FALSE) { + hval = 0; + goto num_key; + } else if (Z_TYPE_P(key) == IS_TRUE) { + hval = 1; + goto num_key; + } else if (Z_TYPE_P(key) == IS_RESOURCE) { + zend_use_resource_as_offset(key); + hval = Z_RES_HANDLE_P(key); + goto num_key; } else if (Z_TYPE_P(key) <= IS_NULL) { if (UNEXPECTED(Z_TYPE_P(key) == IS_UNDEF)) { ZVAL_UNDEFINED_OP1(); @@ -2485,7 +2499,7 @@ num_key: str = ZSTR_EMPTY_ALLOC(); goto str_key; } else { - zend_error(E_WARNING, "array_key_exists(): The first argument should be either a string or an integer"); + zend_type_error("Illegal offset type"); return 0; } } |