diff options
author | Xinchen Hui <laruence@gmail.com> | 2018-07-24 11:34:57 +0800 |
---|---|---|
committer | Xinchen Hui <laruence@gmail.com> | 2018-07-24 11:34:57 +0800 |
commit | 38d97557da2614f5fe1d0fa64f33dfb2cf6cbaad (patch) | |
tree | fb8aa8bc5818168b27d2afaeedcbf4f0cbd1f0fe | |
parent | 2139c2c2de5057c3ac740a1d096e0249015cc7f5 (diff) | |
download | php-git-38d97557da2614f5fe1d0fa64f33dfb2cf6cbaad.tar.gz |
Fixed bug #68553 (array_column: null values in $index_key become incrementing keys in result)
-rw-r--r-- | NEWS | 2 | ||||
-rw-r--r-- | ext/standard/array.c | 42 | ||||
-rw-r--r-- | ext/standard/tests/array/bug68553.phpt | 66 |
3 files changed, 100 insertions, 10 deletions
@@ -14,6 +14,8 @@ PHP NEWS . Fixed bug #76488 (Memory leak when fetching a BLOB field). (Simonov Denis) - Standard: + . Fixed bug #68553 (array_column: null values in $index_key become incrementing + keys in result). (Laruence) . Fixed bug #73817 (Incorrect entries in get_html_translation_table). (cmb) - Zip: diff --git a/ext/standard/array.c b/ext/standard/array.c index 646ffad198..a1e3faa3dd 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -3526,16 +3526,38 @@ PHP_FUNCTION(array_column) zkeyval = array_column_fetch_prop(data, zkey, &rvk); } if (zkeyval) { - if (Z_TYPE_P(zkeyval) == IS_STRING) { - zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR_P(zkeyval), zcolval); - } else if (Z_TYPE_P(zkeyval) == IS_LONG) { - add_index_zval(return_value, Z_LVAL_P(zkeyval), zcolval); - } else if (Z_TYPE_P(zkeyval) == IS_OBJECT) { - zend_string *key = zval_get_string(zkeyval); - zend_symtable_update(Z_ARRVAL_P(return_value), key, zcolval); - zend_string_release(key); - } else { - add_next_index_zval(return_value, zcolval); + switch (Z_TYPE_P(zkeyval)) { + case IS_STRING: + zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR_P(zkeyval), zcolval); + break; + case IS_LONG: + zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_P(zkeyval), zcolval); + break; + case IS_OBJECT: + { + zend_string *key = zval_get_string(zkeyval); + zend_symtable_update(Z_ARRVAL_P(return_value), key, zcolval); + zend_string_release(key); + break; + } + case IS_NULL: + zend_hash_update(Z_ARRVAL_P(return_value), ZSTR_EMPTY_ALLOC(), zcolval); + break; + case IS_DOUBLE: + zend_hash_index_update(Z_ARRVAL_P(return_value), zend_dval_to_lval(Z_DVAL_P(zkeyval)), zcolval); + break; + case IS_TRUE: + zend_hash_index_update(Z_ARRVAL_P(return_value), 1, zcolval); + break; + case IS_FALSE: + zend_hash_index_update(Z_ARRVAL_P(return_value), 0, zcolval); + break; + case IS_RESOURCE: + zend_hash_index_update(Z_ARRVAL_P(return_value), Z_RES_HANDLE_P(zkeyval), zcolval); + break; + default: + add_next_index_zval(return_value, zcolval); + break; } if (zkeyval == &rvk) { zval_ptr_dtor(&rvk); diff --git a/ext/standard/tests/array/bug68553.phpt b/ext/standard/tests/array/bug68553.phpt new file mode 100644 index 0000000000..91c5b080a9 --- /dev/null +++ b/ext/standard/tests/array/bug68553.phpt @@ -0,0 +1,66 @@ +--TEST-- +Bug #68553 (array_column: null values in $index_key become incrementing keys in result) +--FILE-- +<?php +$i = 100; +/* increase the resource id to make test stable */ +while ($i--) { + $fd = fopen(__FILE__, "r"); + fclose($fd); +} +$a = [ + ['a' => 10], + ['a' => 20], + ['a' => true], + ['a' => false], + ['a' => fopen(__FILE__, "r")], + ['a' => -5], + ['a' => 7.38], + ['a' => null, "test"], + ['a' => null], +]; + +var_dump(array_column($a, null, 'a')); +--EXPECTF-- +array(8) { + [10]=> + array(1) { + ["a"]=> + int(10) + } + [20]=> + array(1) { + ["a"]=> + int(20) + } + [1]=> + array(1) { + ["a"]=> + bool(true) + } + [0]=> + array(1) { + ["a"]=> + bool(false) + } + [%d]=> + array(1) { + ["a"]=> + resource(%d) of type (stream) + } + [-5]=> + array(1) { + ["a"]=> + int(-5) + } + [7]=> + array(1) { + ["a"]=> + float(7.38) + } + [""]=> + array(1) { + ["a"]=> + NULL + } +} |