--- embedaddon/php/ext/spl/spl_array.c 2012/02/21 23:48:01 1.1.1.1 +++ embedaddon/php/ext/spl/spl_array.c 2013/10/14 08:02:31 1.1.1.4 @@ -2,7 +2,7 @@ +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ - | Copyright (c) 1997-2012 The PHP Group | + | Copyright (c) 1997-2013 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: spl_array.c,v 1.1.1.1 2012/02/21 23:48:01 misho Exp $ */ +/* $Id: spl_array.c,v 1.1.1.4 2013/10/14 08:02:31 misho Exp $ */ #ifdef HAVE_CONFIG_H # include "config.h" @@ -58,35 +58,41 @@ PHPAPI zend_class_entry *spl_ce_RecursiveArrayIterato #define SPL_ARRAY_INT_MASK 0xFFFF0000 #define SPL_ARRAY_CLONE_MASK 0x0300FFFF +#define SPL_ARRAY_METHOD_NO_ARG 0 +#define SPL_ARRAY_METHOD_USE_ARG 1 +#define SPL_ARRAY_METHOD_MAY_USER_ARG 2 + typedef struct _spl_array_object { - zend_object std; - zval *array; - zval *retval; - HashPosition pos; - ulong pos_h; - int ar_flags; - int is_self; - zend_function *fptr_offset_get; - zend_function *fptr_offset_set; - zend_function *fptr_offset_has; - zend_function *fptr_offset_del; - zend_function *fptr_count; - zend_function *fptr_serialize; - zend_function *fptr_unserialize; - zend_class_entry *ce_get_iterator; - php_serialize_data_t *serialize_data; - php_unserialize_data_t *unserialize_data; + zend_object std; + zval *array; + zval *retval; + HashPosition pos; + ulong pos_h; + int ar_flags; + int is_self; + zend_function *fptr_offset_get; + zend_function *fptr_offset_set; + zend_function *fptr_offset_has; + zend_function *fptr_offset_del; + zend_function *fptr_count; + zend_class_entry* ce_get_iterator; HashTable *debug_info; unsigned char nApplyCount; } spl_array_object; static inline HashTable *spl_array_get_hash_table(spl_array_object* intern, int check_std_props TSRMLS_DC) { /* {{{ */ if ((intern->ar_flags & SPL_ARRAY_IS_SELF) != 0) { + if (!intern->std.properties) { + rebuild_object_properties(&intern->std); + } return intern->std.properties; } else if ((intern->ar_flags & SPL_ARRAY_USE_OTHER) && (check_std_props == 0 || (intern->ar_flags & SPL_ARRAY_STD_PROP_LIST) == 0) && Z_TYPE_P(intern->array) == IS_OBJECT) { spl_array_object *other = (spl_array_object*)zend_object_store_get_object(intern->array TSRMLS_CC); return spl_array_get_hash_table(other, check_std_props TSRMLS_CC); } else if ((intern->ar_flags & ((check_std_props ? SPL_ARRAY_STD_PROP_LIST : 0) | SPL_ARRAY_IS_SELF)) != 0) { + if (!intern->std.properties) { + rebuild_object_properties(&intern->std); + } return intern->std.properties; } else { return HASH_OF(intern->array); @@ -156,13 +162,11 @@ static void spl_array_object_free_storage(void *object /* }}} */ zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC); -int spl_array_serialize(zval *object, unsigned char **buffer, zend_uint *buf_len, zend_serialize_data *data TSRMLS_DC); -int spl_array_unserialize(zval **object, zend_class_entry *ce, const unsigned char *buf, zend_uint buf_len, zend_unserialize_data *data TSRMLS_DC); /* {{{ spl_array_object_new_ex */ static zend_object_value spl_array_object_new_ex(zend_class_entry *class_type, spl_array_object **obj, zval *orig, int clone_orig TSRMLS_DC) { - zend_object_value retval; + zend_object_value retval = {0}; spl_array_object *intern; zval *tmp; zend_class_entry * parent = class_type; @@ -174,11 +178,9 @@ static zend_object_value spl_array_object_new_ex(zend_ ALLOC_INIT_ZVAL(intern->retval); zend_object_std_init(&intern->std, class_type TSRMLS_CC); - zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *)); + object_properties_init(&intern->std, class_type); intern->ar_flags = 0; - intern->serialize_data = NULL; - intern->unserialize_data = NULL; intern->debug_info = NULL; intern->ce_get_iterator = spl_ce_ArrayIterator; if (orig) { @@ -245,14 +247,6 @@ static zend_object_value spl_array_object_new_ex(zend_ if (intern->fptr_count->common.scope == parent) { intern->fptr_count = NULL; } - zend_hash_find(&class_type->function_table, "serialize", sizeof("serialize"), (void **) &intern->fptr_serialize); - if (intern->fptr_serialize->common.scope == parent) { - intern->fptr_serialize = NULL; - } - zend_hash_find(&class_type->function_table, "unserialize", sizeof("unserialize"), (void **) &intern->fptr_unserialize); - if (intern->fptr_unserialize->common.scope == parent) { - intern->fptr_unserialize = NULL; - } } /* Cache iterator functions if ArrayIterator or derived. Check current's */ /* cache since only current is always required */ @@ -312,38 +306,41 @@ static zval **spl_array_get_dimension_ptr_ptr(int chec long index; HashTable *ht = spl_array_get_hash_table(intern, 0 TSRMLS_CC); -/* We cannot get the pointer pointer so we don't allow it here for now - if (check_inherited && intern->fptr_offset_get) { - return zend_call_method_with_1_params(&object, Z_OBJCE_P(object), &intern->fptr_offset_get, "offsetGet", NULL, offset); - }*/ - if (!offset) { return &EG(uninitialized_zval_ptr); } if ((type == BP_VAR_W || type == BP_VAR_RW) && (ht->nApplyCount > 0)) { zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited"); - return &EG(uninitialized_zval_ptr);; + return &EG(error_zval_ptr);; } switch(Z_TYPE_P(offset)) { + case IS_NULL: + Z_STRVAL_P(offset) = ""; + Z_STRLEN_P(offset) = 0; case IS_STRING: if (zend_symtable_find(ht, Z_STRVAL_P(offset), Z_STRLEN_P(offset)+1, (void **) &retval) == FAILURE) { - if (type == BP_VAR_W || type == BP_VAR_RW) { - zval *value; - ALLOC_INIT_ZVAL(value); - zend_symtable_update(ht, Z_STRVAL_P(offset), Z_STRLEN_P(offset)+1, (void**)&value, sizeof(void*), NULL); - zend_symtable_find(ht, Z_STRVAL_P(offset), Z_STRLEN_P(offset)+1, (void **) &retval); - return retval; - } else { - zend_error(E_NOTICE, "Undefined index: %s", Z_STRVAL_P(offset)); - return &EG(uninitialized_zval_ptr); + switch (type) { + case BP_VAR_R: + zend_error(E_NOTICE, "Undefined index: %s", Z_STRVAL_P(offset)); + case BP_VAR_UNSET: + case BP_VAR_IS: + retval = &EG(uninitialized_zval_ptr); + break; + case BP_VAR_RW: + zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset)); + case BP_VAR_W: { + zval *value; + ALLOC_INIT_ZVAL(value); + zend_symtable_update(ht, Z_STRVAL_P(offset), Z_STRLEN_P(offset)+1, (void**)&value, sizeof(void*), (void **)&retval); + } } - } else { - return retval; } - case IS_DOUBLE: + return retval; case IS_RESOURCE: + zend_error(E_STRICT, "Resource ID#%ld used as offset, casting to integer (%ld)", Z_LVAL_P(offset), Z_LVAL_P(offset)); + case IS_DOUBLE: case IS_BOOL: case IS_LONG: if (offset->type == IS_DOUBLE) { @@ -352,23 +349,27 @@ static zval **spl_array_get_dimension_ptr_ptr(int chec index = Z_LVAL_P(offset); } if (zend_hash_index_find(ht, index, (void **) &retval) == FAILURE) { - if (type == BP_VAR_W || type == BP_VAR_RW) { - zval *value; - ALLOC_INIT_ZVAL(value); - zend_hash_index_update(ht, index, (void**)&value, sizeof(void*), NULL); - zend_hash_index_find(ht, index, (void **) &retval); - return retval; - } else { - zend_error(E_NOTICE, "Undefined offset: %ld", index); - return &EG(uninitialized_zval_ptr); + switch (type) { + case BP_VAR_R: + zend_error(E_NOTICE, "Undefined offset: %ld", index); + case BP_VAR_UNSET: + case BP_VAR_IS: + retval = &EG(uninitialized_zval_ptr); + break; + case BP_VAR_RW: + zend_error(E_NOTICE, "Undefined offset: %ld", index); + case BP_VAR_W: { + zval *value; + ALLOC_INIT_ZVAL(value); + zend_hash_index_update(ht, index, (void**)&value, sizeof(void*), (void **)&retval); + } } - } else { - return retval; } - break; + return retval; default: zend_error(E_WARNING, "Illegal offset type"); - return &EG(uninitialized_zval_ptr); + return (type == BP_VAR_W || type == BP_VAR_RW) ? + &EG(error_zval_ptr) : &EG(uninitialized_zval_ptr); } } /* }}} */ @@ -380,7 +381,11 @@ static zval *spl_array_read_dimension_ex(int check_inh spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC); if (intern->fptr_offset_get) { zval *rv; - SEPARATE_ARG_IF_REF(offset); + if (!offset) { + ALLOC_INIT_ZVAL(offset); + } else { + SEPARATE_ARG_IF_REF(offset); + } zend_call_method_with_1_params(&object, Z_OBJCE_P(object), &intern->fptr_offset_get, "offsetGet", &rv, offset); zval_ptr_dtor(&offset); if (rv) { @@ -397,7 +402,7 @@ static zval *spl_array_read_dimension_ex(int check_inh /* When in a write context, * ZE has to be fooled into thinking this is in a reference set * by separating (if necessary) and returning as an is_ref=1 zval (even if refcount == 1) */ - if ((type == BP_VAR_W || type == BP_VAR_RW) && !Z_ISREF_PP(ret)) { + if ((type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) && !Z_ISREF_PP(ret)) { if (Z_REFCOUNT_PP(ret) > 1) { zval *newval; @@ -519,12 +524,39 @@ static void spl_array_unset_dimension_ex(int check_inh } if (ht == &EG(symbol_table)) { if (zend_delete_global_variable(Z_STRVAL_P(offset), Z_STRLEN_P(offset) TSRMLS_CC)) { - zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset)); + zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset)); } } else { if (zend_symtable_del(ht, Z_STRVAL_P(offset), Z_STRLEN_P(offset)+1) == FAILURE) { - zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset)); - } + zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset)); + } else { + spl_array_object *obj = intern; + + while (1) { + if ((obj->ar_flags & SPL_ARRAY_IS_SELF) != 0) { + break; + } else if (Z_TYPE_P(obj->array) == IS_OBJECT) { + if ((obj->ar_flags & SPL_ARRAY_USE_OTHER) == 0) { + obj = (spl_array_object*)zend_object_store_get_object(obj->array TSRMLS_CC); + break; + } else { + obj = (spl_array_object*)zend_object_store_get_object(obj->array TSRMLS_CC); + } + } else { + obj = NULL; + break; + } + } + if (obj) { + zend_property_info *property_info = zend_get_property_info(obj->std.ce, offset, 1 TSRMLS_CC); + + if (property_info && + (property_info->flags & ZEND_ACC_STATIC) == 0 && + property_info->offset >= 0) { + obj->std.properties_table[property_info->offset] = NULL; + } + } + } } break; case IS_DOUBLE: @@ -542,7 +574,7 @@ static void spl_array_unset_dimension_ex(int check_inh return; } if (zend_hash_index_del(ht, index) == FAILURE) { - zend_error(E_NOTICE,"Undefined offset: %ld", Z_LVAL_P(offset)); + zend_error(E_NOTICE,"Undefined offset: %ld", Z_LVAL_P(offset)); } break; default: @@ -578,49 +610,46 @@ static int spl_array_has_dimension_ex(int check_inheri } switch(Z_TYPE_P(offset)) { - case IS_STRING: - if (check_empty) { - if (zend_symtable_find(spl_array_get_hash_table(intern, 0 TSRMLS_CC), Z_STRVAL_P(offset), Z_STRLEN_P(offset)+1, (void **) &tmp) != FAILURE) { - switch (check_empty) { - case 0: - return Z_TYPE_PP(tmp) != IS_NULL; - case 2: - return 1; - default: - return zend_is_true(*tmp); + case IS_STRING: + { + HashTable *ht = spl_array_get_hash_table(intern, 0 TSRMLS_CC); + if (zend_symtable_find(ht, Z_STRVAL_P(offset), Z_STRLEN_P(offset)+1, (void **) &tmp) != FAILURE) { + switch (check_empty) { + case 0: + return Z_TYPE_PP(tmp) != IS_NULL; + case 2: + return 1; + default: + return zend_is_true(*tmp); + } } } return 0; - } else { - return zend_symtable_exists(spl_array_get_hash_table(intern, 0 TSRMLS_CC), Z_STRVAL_P(offset), Z_STRLEN_P(offset)+1); - } - case IS_DOUBLE: - case IS_RESOURCE: - case IS_BOOL: - case IS_LONG: - if (offset->type == IS_DOUBLE) { - index = (long)Z_DVAL_P(offset); - } else { - index = Z_LVAL_P(offset); - } - if (check_empty) { - HashTable *ht = spl_array_get_hash_table(intern, 0 TSRMLS_CC); - if (zend_hash_index_find(ht, index, (void **)&tmp) != FAILURE) { - switch (check_empty) { - case 0: - return Z_TYPE_PP(tmp) != IS_NULL; - case 2: - return 1; - default: - return zend_is_true(*tmp); + case IS_DOUBLE: + case IS_RESOURCE: + case IS_BOOL: + case IS_LONG: + { + HashTable *ht = spl_array_get_hash_table(intern, 0 TSRMLS_CC); + if (offset->type == IS_DOUBLE) { + index = (long)Z_DVAL_P(offset); + } else { + index = Z_LVAL_P(offset); } + if (zend_hash_index_find(ht, index, (void **)&tmp) != FAILURE) { + switch (check_empty) { + case 0: + return Z_TYPE_PP(tmp) != IS_NULL; + case 2: + return 1; + default: + return zend_is_true(*tmp); + } + } + return 0; } - return 0; - } else { - return zend_hash_index_exists(spl_array_get_hash_table(intern, 0 TSRMLS_CC), index); - } - default: - zend_error(E_WARNING, "Illegal offset type"); + default: + zend_error(E_WARNING, "Illegal offset type"); } return 0; } /* }}} */ @@ -630,6 +659,28 @@ static int spl_array_has_dimension(zval *object, zval return spl_array_has_dimension_ex(1, object, offset, check_empty TSRMLS_CC); } /* }}} */ +/* {{{ spl_array_object_verify_pos_ex */ +static inline int spl_array_object_verify_pos_ex(spl_array_object *object, HashTable *ht, const char *msg_prefix TSRMLS_DC) +{ + if (!ht) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%sArray was modified outside object and is no longer an array", msg_prefix); + return FAILURE; + } + + if (object->pos && (object->ar_flags & SPL_ARRAY_IS_REF) && spl_hash_verify_pos_ex(object, ht TSRMLS_CC) == FAILURE) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%sArray was modified outside object and internal position is no longer valid", msg_prefix); + return FAILURE; + } + + return SUCCESS; +} /* }}} */ + +/* {{{ spl_array_object_verify_pos */ +static inline int spl_array_object_verify_pos(spl_array_object *object, HashTable *ht TSRMLS_DC) +{ + return spl_array_object_verify_pos_ex(object, ht, "" TSRMLS_CC); +} /* }}} */ + /* {{{ proto bool ArrayObject::offsetExists(mixed $index) proto bool ArrayIterator::offsetExists(mixed $index) Returns whether the requested $index exists. */ @@ -639,7 +690,7 @@ SPL_METHOD(Array, offsetExists) if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &index) == FAILURE) { return; } - RETURN_BOOL(spl_array_has_dimension_ex(0, getThis(), index, 0 TSRMLS_CC)); + RETURN_BOOL(spl_array_has_dimension_ex(0, getThis(), index, 2 TSRMLS_CC)); } /* }}} */ /* {{{ proto mixed ArrayObject::offsetGet(mixed $index) @@ -667,7 +718,6 @@ SPL_METHOD(Array, offsetSet) spl_array_write_dimension_ex(0, getThis(), index, value TSRMLS_CC); } /* }}} */ - void spl_array_iterator_append(zval *object, zval *append_value TSRMLS_DC) /* {{{ */ { spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC); @@ -751,6 +801,10 @@ static HashTable* spl_array_get_debug_info(zval *obj, *is_temp = 0; + if (!intern->std.properties) { + rebuild_object_properties(&intern->std); + } + if (HASH_OF(intern->array) == intern->std.properties) { return intern->std.properties; } else { @@ -777,65 +831,88 @@ static HashTable* spl_array_get_debug_info(zval *obj, } /* }}} */ -static zval *spl_array_read_property(zval *object, zval *member, int type TSRMLS_DC) /* {{{ */ +static zval *spl_array_read_property(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC) /* {{{ */ { spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC); if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0 - && !std_object_handlers.has_property(object, member, 2 TSRMLS_CC)) { + && !std_object_handlers.has_property(object, member, 2, key TSRMLS_CC)) { return spl_array_read_dimension(object, member, type TSRMLS_CC); } - return std_object_handlers.read_property(object, member, type TSRMLS_CC); + return std_object_handlers.read_property(object, member, type, key TSRMLS_CC); } /* }}} */ -static void spl_array_write_property(zval *object, zval *member, zval *value TSRMLS_DC) /* {{{ */ +static void spl_array_write_property(zval *object, zval *member, zval *value, const zend_literal *key TSRMLS_DC) /* {{{ */ { spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC); if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0 - && !std_object_handlers.has_property(object, member, 2 TSRMLS_CC)) { + && !std_object_handlers.has_property(object, member, 2, key TSRMLS_CC)) { spl_array_write_dimension(object, member, value TSRMLS_CC); return; } - std_object_handlers.write_property(object, member, value TSRMLS_CC); + std_object_handlers.write_property(object, member, value, key TSRMLS_CC); } /* }}} */ -static zval **spl_array_get_property_ptr_ptr(zval *object, zval *member TSRMLS_DC) /* {{{ */ +static zval **spl_array_get_property_ptr_ptr(zval *object, zval *member, const zend_literal *key TSRMLS_DC) /* {{{ */ { spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC); if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0 - && !std_object_handlers.has_property(object, member, 2 TSRMLS_CC)) { + && !std_object_handlers.has_property(object, member, 2, key TSRMLS_CC)) { return spl_array_get_dimension_ptr_ptr(1, object, member, BP_VAR_RW TSRMLS_CC); } - return std_object_handlers.get_property_ptr_ptr(object, member TSRMLS_CC); + return std_object_handlers.get_property_ptr_ptr(object, member, key TSRMLS_CC); } /* }}} */ -static int spl_array_has_property(zval *object, zval *member, int has_set_exists TSRMLS_DC) /* {{{ */ +static int spl_array_has_property(zval *object, zval *member, int has_set_exists, const zend_literal *key TSRMLS_DC) /* {{{ */ { spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC); if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0 - && !std_object_handlers.has_property(object, member, 2 TSRMLS_CC)) { + && !std_object_handlers.has_property(object, member, 2, key TSRMLS_CC)) { return spl_array_has_dimension(object, member, has_set_exists TSRMLS_CC); } - return std_object_handlers.has_property(object, member, has_set_exists TSRMLS_CC); - + return std_object_handlers.has_property(object, member, has_set_exists, key TSRMLS_CC); } /* }}} */ -static void spl_array_unset_property(zval *object, zval *member TSRMLS_DC) /* {{{ */ +static void spl_array_unset_property(zval *object, zval *member, const zend_literal *key TSRMLS_DC) /* {{{ */ { spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC); if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0 - && !std_object_handlers.has_property(object, member, 2 TSRMLS_CC)) { + && !std_object_handlers.has_property(object, member, 2, key TSRMLS_CC)) { spl_array_unset_dimension(object, member TSRMLS_CC); spl_array_rewind(intern TSRMLS_CC); /* because deletion might invalidate position */ return; } - std_object_handlers.unset_property(object, member TSRMLS_CC); + std_object_handlers.unset_property(object, member, key TSRMLS_CC); } /* }}} */ +static int spl_array_compare_objects(zval *o1, zval *o2 TSRMLS_DC) /* {{{ */ +{ + HashTable *ht1, + *ht2; + spl_array_object *intern1, + *intern2; + int result = 0; + zval temp_zv; + + intern1 = (spl_array_object*)zend_object_store_get_object(o1 TSRMLS_CC); + intern2 = (spl_array_object*)zend_object_store_get_object(o2 TSRMLS_CC); + ht1 = spl_array_get_hash_table(intern1, 0 TSRMLS_CC); + ht2 = spl_array_get_hash_table(intern2, 0 TSRMLS_CC); + + zend_compare_symbol_tables(&temp_zv, ht1, ht2 TSRMLS_CC); + result = (int)Z_LVAL(temp_zv); + /* if we just compared std.properties, don't do it again */ + if (result == 0 && + !(ht1 == intern1->std.properties && ht2 == intern2->std.properties)) { + result = std_object_handlers.compare_objects(o1, o2 TSRMLS_CC); + } + return result; +} /* }}} */ + static int spl_array_skip_protected(spl_array_object *intern, HashTable *aht TSRMLS_DC) /* {{{ */ { char *string_key; @@ -916,17 +993,11 @@ static int spl_array_it_valid(zend_object_iterator *it if (object->ar_flags & SPL_ARRAY_OVERLOADED_VALID) { return zend_user_it_valid(iter TSRMLS_CC); } else { - if (!aht) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "ArrayIterator::valid(): Array was modified outside object and is no longer an array"); + if (spl_array_object_verify_pos_ex(object, aht, "ArrayIterator::valid(): " TSRMLS_CC) == FAILURE) { return FAILURE; } - - if (object->pos && (object->ar_flags & SPL_ARRAY_IS_REF) && spl_hash_verify_pos_ex(object, aht TSRMLS_CC) == FAILURE) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "ArrayIterator::valid(): Array was modified outside object and internal position is no longer valid"); - return FAILURE; - } else { - return zend_hash_has_more_elements_ex(aht, &object->pos); - } + + return zend_hash_has_more_elements_ex(aht, &object->pos); } } /* }}} */ @@ -956,16 +1027,10 @@ static int spl_array_it_get_current_key(zend_object_it if (object->ar_flags & SPL_ARRAY_OVERLOADED_KEY) { return zend_user_it_get_current_key(iter, str_key, str_key_len, int_key TSRMLS_CC); } else { - if (!aht) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "ArrayIterator::current(): Array was modified outside object and is no longer an array"); + if (spl_array_object_verify_pos_ex(object, aht, "ArrayIterator::current(): " TSRMLS_CC) == FAILURE) { return HASH_KEY_NON_EXISTANT; } - if ((object->ar_flags & SPL_ARRAY_IS_REF) && spl_hash_verify_pos_ex(object, aht TSRMLS_CC) == FAILURE) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "ArrayIterator::current(): Array was modified outside object and internal position is no longer valid"); - return HASH_KEY_NON_EXISTANT; - } - return zend_hash_get_current_key_ex(aht, str_key, str_key_len, int_key, 1, &object->pos); } } @@ -1369,14 +1434,28 @@ static void spl_array_method(INTERNAL_FUNCTION_PARAMET { spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(getThis() TSRMLS_CC); HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC); - zval *tmp, *arg; + zval *tmp, *arg = NULL; zval *retval_ptr = NULL; MAKE_STD_ZVAL(tmp); Z_TYPE_P(tmp) = IS_ARRAY; Z_ARRVAL_P(tmp) = aht; - if (use_arg) { + if (!use_arg) { + aht->nApplyCount++; + zend_call_method(NULL, NULL, NULL, fname, fname_len, &retval_ptr, 1, tmp, NULL TSRMLS_CC); + aht->nApplyCount--; + } else if (use_arg == SPL_ARRAY_METHOD_MAY_USER_ARG) { + if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "|z", &arg) == FAILURE) { + Z_TYPE_P(tmp) = IS_NULL; + zval_ptr_dtor(&tmp); + zend_throw_exception(spl_ce_BadMethodCallException, "Function expects one argument at most", 0 TSRMLS_CC); + return; + } + aht->nApplyCount++; + zend_call_method(NULL, NULL, NULL, fname, fname_len, &retval_ptr, arg? 2 : 1, tmp, arg TSRMLS_CC); + aht->nApplyCount--; + } else { if (ZEND_NUM_ARGS() != 1 || zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg) == FAILURE) { Z_TYPE_P(tmp) = IS_NULL; zval_ptr_dtor(&tmp); @@ -1386,10 +1465,6 @@ static void spl_array_method(INTERNAL_FUNCTION_PARAMET aht->nApplyCount++; zend_call_method(NULL, NULL, NULL, fname, fname_len, &retval_ptr, 2, tmp, arg TSRMLS_CC); aht->nApplyCount--; - } else { - aht->nApplyCount++; - zend_call_method(NULL, NULL, NULL, fname, fname_len, &retval_ptr, 1, tmp, NULL TSRMLS_CC); - aht->nApplyCount--; } Z_TYPE_P(tmp) = IS_NULL; /* we want to destroy the zval, not the hashtable */ zval_ptr_dtor(&tmp); @@ -1404,35 +1479,35 @@ SPL_METHOD(cname, fname) \ spl_array_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, #fname, sizeof(#fname)-1, use_arg); \ } -/* {{{ proto int ArrayObject::asort() - proto int ArrayIterator::asort() +/* {{{ proto int ArrayObject::asort([int $sort_flags = SORT_REGULAR ]) + proto int ArrayIterator::asort([int $sort_flags = SORT_REGULAR ]) Sort the entries by values. */ -SPL_ARRAY_METHOD(Array, asort, 0) /* }}} */ +SPL_ARRAY_METHOD(Array, asort, SPL_ARRAY_METHOD_MAY_USER_ARG) /* }}} */ -/* {{{ proto int ArrayObject::ksort() - proto int ArrayIterator::ksort() +/* {{{ proto int ArrayObject::ksort([int $sort_flags = SORT_REGULAR ]) + proto int ArrayIterator::ksort([int $sort_flags = SORT_REGULAR ]) Sort the entries by key. */ -SPL_ARRAY_METHOD(Array, ksort, 0) /* }}} */ +SPL_ARRAY_METHOD(Array, ksort, SPL_ARRAY_METHOD_MAY_USER_ARG) /* }}} */ /* {{{ proto int ArrayObject::uasort(callback cmp_function) proto int ArrayIterator::uasort(callback cmp_function) Sort the entries by values user defined function. */ -SPL_ARRAY_METHOD(Array, uasort, 1) /* }}} */ +SPL_ARRAY_METHOD(Array, uasort, SPL_ARRAY_METHOD_USE_ARG) /* }}} */ /* {{{ proto int ArrayObject::uksort(callback cmp_function) proto int ArrayIterator::uksort(callback cmp_function) Sort the entries by key using user defined function. */ -SPL_ARRAY_METHOD(Array, uksort, 1) /* }}} */ +SPL_ARRAY_METHOD(Array, uksort, SPL_ARRAY_METHOD_USE_ARG) /* }}} */ /* {{{ proto int ArrayObject::natsort() proto int ArrayIterator::natsort() Sort the entries by values using "natural order" algorithm. */ -SPL_ARRAY_METHOD(Array, natsort, 0) /* }}} */ +SPL_ARRAY_METHOD(Array, natsort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */ /* {{{ proto int ArrayObject::natcasesort() proto int ArrayIterator::natcasesort() Sort the entries by key using case insensitive "natural order" algorithm. */ -SPL_ARRAY_METHOD(Array, natcasesort, 0) /* }}} */ +SPL_ARRAY_METHOD(Array, natcasesort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */ /* {{{ proto mixed|NULL ArrayIterator::current() Return current array entry */ @@ -1447,16 +1522,10 @@ SPL_METHOD(Array, current) return; } - if (!aht) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array"); + if (spl_array_object_verify_pos(intern, aht TSRMLS_CC) == FAILURE) { return; } - if ((intern->ar_flags & SPL_ARRAY_IS_REF) && spl_hash_verify_pos_ex(intern, aht TSRMLS_CC) == FAILURE) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and internal position is no longer valid"); - return; - } - if (zend_hash_get_current_data_ex(aht, (void **) &entry, &intern->pos) == FAILURE) { return; } @@ -1483,16 +1552,10 @@ void spl_array_iterator_key(zval *object, zval *return ulong num_key; HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC); - if (!aht) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array"); + if (spl_array_object_verify_pos(intern, aht TSRMLS_CC) == FAILURE) { return; } - if ((intern->ar_flags & SPL_ARRAY_IS_REF) && spl_hash_verify_pos_ex(intern, aht TSRMLS_CC) == FAILURE) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and internal position is no longer valid"); - return; - } - switch (zend_hash_get_current_key_ex(aht, &string_key, &string_length, &num_key, 1, &intern->pos)) { case HASH_KEY_IS_STRING: RETVAL_STRINGL(string_key, string_length - 1, 0); @@ -1517,13 +1580,12 @@ SPL_METHOD(Array, next) if (zend_parse_parameters_none() == FAILURE) { return; } - - if (!aht) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array"); + + if (spl_array_object_verify_pos(intern, aht TSRMLS_CC) == FAILURE) { return; } - spl_array_next_ex(intern, aht TSRMLS_CC); + spl_array_next_no_verify(intern, aht TSRMLS_CC); } /* }}} */ @@ -1538,14 +1600,8 @@ SPL_METHOD(Array, valid) if (zend_parse_parameters_none() == FAILURE) { return; } - - if (!aht) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array"); - return; - } - if (intern->pos && (intern->ar_flags & SPL_ARRAY_IS_REF) && spl_hash_verify_pos_ex(intern, aht TSRMLS_CC) == FAILURE) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and internal position is no longer valid"); + if (spl_array_object_verify_pos(intern, aht TSRMLS_CC) == FAILURE) { RETURN_FALSE; } else { RETURN_BOOL(zend_hash_has_more_elements_ex(aht, &intern->pos) == SUCCESS); @@ -1564,14 +1620,8 @@ SPL_METHOD(Array, hasChildren) if (zend_parse_parameters_none() == FAILURE) { return; } - - if (!aht) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array"); - RETURN_FALSE; - } - if ((intern->ar_flags & SPL_ARRAY_IS_REF) && spl_hash_verify_pos_ex(intern, aht TSRMLS_CC) == FAILURE) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and internal position is no longer valid"); + if (spl_array_object_verify_pos(intern, aht TSRMLS_CC) == FAILURE) { RETURN_FALSE; } @@ -1595,16 +1645,10 @@ SPL_METHOD(Array, getChildren) return; } - if (!aht) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array"); + if (spl_array_object_verify_pos(intern, aht TSRMLS_CC) == FAILURE) { return; } - if ((intern->ar_flags & SPL_ARRAY_IS_REF) && spl_hash_verify_pos_ex(intern, aht TSRMLS_CC) == FAILURE) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and internal position is no longer valid"); - return; - } - if (zend_hash_get_current_data_ex(aht, (void **) &entry, &intern->pos) == FAILURE) { return; } @@ -1614,7 +1658,7 @@ SPL_METHOD(Array, getChildren) return; } if (instanceof_function(Z_OBJCE_PP(entry), Z_OBJCE_P(getThis()) TSRMLS_CC)) { - RETURN_ZVAL(*entry, 0, 0); + RETURN_ZVAL(*entry, 1, 0); } } @@ -1625,69 +1669,56 @@ SPL_METHOD(Array, getChildren) } /* }}} */ -smart_str spl_array_serialize_helper(spl_array_object *intern, php_serialize_data_t *var_hash_p TSRMLS_DC) { /* {{{ */ +/* {{{ proto string ArrayObject::serialize() + Serialize the object */ +SPL_METHOD(Array, serialize) +{ + zval *object = getThis(); + spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC); HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC); zval members, *pmembers; + php_serialize_data_t var_hash; smart_str buf = {0}; zval *flags; + if (zend_parse_parameters_none() == FAILURE) { + return; + } + if (!aht) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array"); - return buf; + return; } + PHP_VAR_SERIALIZE_INIT(var_hash); + MAKE_STD_ZVAL(flags); ZVAL_LONG(flags, (intern->ar_flags & SPL_ARRAY_CLONE_MASK)); /* storage */ smart_str_appendl(&buf, "x:", 2); - php_var_serialize(&buf, &flags, var_hash_p TSRMLS_CC); + php_var_serialize(&buf, &flags, &var_hash TSRMLS_CC); zval_ptr_dtor(&flags); if (!(intern->ar_flags & SPL_ARRAY_IS_SELF)) { - php_var_serialize(&buf, &intern->array, var_hash_p TSRMLS_CC); + php_var_serialize(&buf, &intern->array, &var_hash TSRMLS_CC); smart_str_appendc(&buf, ';'); } /* members */ smart_str_appendl(&buf, "m:", 2); INIT_PZVAL(&members); + if (!intern->std.properties) { + rebuild_object_properties(&intern->std); + } Z_ARRVAL(members) = intern->std.properties; Z_TYPE(members) = IS_ARRAY; pmembers = &members; - php_var_serialize(&buf, &pmembers, var_hash_p TSRMLS_CC); /* finishes the string */ + php_var_serialize(&buf, &pmembers, &var_hash TSRMLS_CC); /* finishes the string */ /* done */ - return buf; -} -/* }}} */ + PHP_VAR_SERIALIZE_DESTROY(var_hash); -/* {{{ proto string ArrayObject::serialize() - Serialize the object */ -SPL_METHOD(Array, serialize) -{ - zval *object = getThis(); - spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC); - int was_in_serialize = intern->serialize_data != NULL; - smart_str buf; - - if (zend_parse_parameters_none() == FAILURE) { - return; - } - - if (!was_in_serialize) { - intern->serialize_data = emalloc(sizeof(php_serialize_data_t)); - PHP_VAR_SERIALIZE_INIT(*intern->serialize_data); - } - - buf = spl_array_serialize_helper(intern, intern->serialize_data TSRMLS_CC); - - if (!was_in_serialize) { - PHP_VAR_SERIALIZE_DESTROY(*intern->serialize_data); - efree(intern->serialize_data); - intern->serialize_data = NULL; - } - if (buf.c) { RETURN_STRINGL(buf.c, buf.len, 0); } @@ -1695,47 +1726,32 @@ SPL_METHOD(Array, serialize) RETURN_NULL(); } /* }}} */ -int spl_array_serialize(zval *object, unsigned char **buffer, zend_uint *buf_len, zend_serialize_data *data TSRMLS_DC) /* {{{ */ +/* {{{ proto void ArrayObject::unserialize(string serialized) + * unserialize the object + */ +SPL_METHOD(Array, unserialize) { - spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC); + spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(getThis() TSRMLS_CC); - if (intern->fptr_serialize) { - int retval; - php_serialize_data_t *before; - - before = intern->serialize_data; - intern->serialize_data = (php_serialize_data_t *)data; - - retval = zend_user_serialize(object, buffer, buf_len, data TSRMLS_CC); - - intern->serialize_data = before; - - return retval; - } else { - smart_str buf; - - buf = spl_array_serialize_helper(intern, (php_serialize_data_t *)data TSRMLS_CC); - - if (buf.c) { - *buffer = (unsigned char*)estrndup(buf.c, buf.len); - *buf_len = buf.len; - efree(buf.c); - return SUCCESS; - } else { - return FAILURE; - } - } -} -/* }}} */ - -void spl_array_unserialize_helper(spl_array_object *intern, const unsigned char *buf, int buf_len, php_unserialize_data_t *var_hash_p TSRMLS_DC) /* {{{ */ -{ + char *buf; + int buf_len; const unsigned char *p, *s; + php_unserialize_data_t var_hash; zval *pmembers, *pflags = NULL; long flags; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &buf, &buf_len) == FAILURE) { + return; + } + if (buf_len == 0) { + zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Empty serialized string cannot be empty"); + return; + } + /* storage */ - s = p = buf; + s = p = (const unsigned char*)buf; + PHP_VAR_UNSERIALIZE_INIT(var_hash); if (*p!= 'x' || *++p != ':') { goto outexcept; @@ -1743,7 +1759,7 @@ void spl_array_unserialize_helper(spl_array_object *in ++p; ALLOC_INIT_ZVAL(pflags); - if (!php_var_unserialize(&pflags, &p, s + buf_len, var_hash_p TSRMLS_CC) || Z_TYPE_P(pflags) != IS_LONG) { + if (!php_var_unserialize(&pflags, &p, s + buf_len, &var_hash TSRMLS_CC) || Z_TYPE_P(pflags) != IS_LONG) { zval_ptr_dtor(&pflags); goto outexcept; } @@ -1762,14 +1778,14 @@ void spl_array_unserialize_helper(spl_array_object *in ++p; if (*p!='m') { - if (*p!='a' && *p!='O' && *p!='C') { + if (*p!='a' && *p!='O' && *p!='C' && *p!='r') { goto outexcept; } intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK; intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK; zval_ptr_dtor(&intern->array); ALLOC_INIT_ZVAL(intern->array); - if (!php_var_unserialize(&intern->array, &p, s + buf_len, var_hash_p TSRMLS_CC)) { + if (!php_var_unserialize(&intern->array, &p, s + buf_len, &var_hash TSRMLS_CC)) { goto outexcept; } } @@ -1785,90 +1801,30 @@ void spl_array_unserialize_helper(spl_array_object *in ++p; ALLOC_INIT_ZVAL(pmembers); - if (!php_var_unserialize(&pmembers, &p, s + buf_len, var_hash_p TSRMLS_CC)) { + if (!php_var_unserialize(&pmembers, &p, s + buf_len, &var_hash TSRMLS_CC)) { zval_ptr_dtor(&pmembers); goto outexcept; } /* copy members */ + if (!intern->std.properties) { + rebuild_object_properties(&intern->std); + } zend_hash_copy(intern->std.properties, Z_ARRVAL_P(pmembers), (copy_ctor_func_t) zval_add_ref, (void *) NULL, sizeof(zval *)); zval_ptr_dtor(&pmembers); /* done reading $serialized */ + + PHP_VAR_UNSERIALIZE_DESTROY(var_hash); return; outexcept: - zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Error at offset %ld of %d bytes", (long)((char*)p - (char *)buf), buf_len); + PHP_VAR_UNSERIALIZE_DESTROY(var_hash); + zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Error at offset %ld of %d bytes", (long)((char*)p - buf), buf_len); return; -} -/* }}} */ - -/* {{{ proto void ArrayObject::unserialize(string serialized) - Unserialize the object */ -SPL_METHOD(Array, unserialize) -{ - char *buf; - int buf_len; - spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(getThis() TSRMLS_CC); - int was_in_unserialize = intern->unserialize_data != NULL; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &buf, &buf_len) == FAILURE) { - return; - } - - if (buf_len == 0) { - zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Empty serialized string cannot be empty"); - return; - } - - if (!was_in_unserialize) { - intern->unserialize_data = emalloc(sizeof(php_unserialize_data_t)); - PHP_VAR_UNSERIALIZE_INIT(*intern->unserialize_data); - } - - spl_array_unserialize_helper(intern, (const unsigned char *)buf, buf_len, intern->unserialize_data TSRMLS_CC); - - if (!was_in_unserialize) { - PHP_VAR_UNSERIALIZE_DESTROY(*intern->unserialize_data); - efree(intern->unserialize_data); - intern->unserialize_data = NULL; - } } /* }}} */ -int spl_array_unserialize(zval **object, zend_class_entry *ce, const unsigned char *buf, zend_uint buf_len, zend_unserialize_data *data TSRMLS_DC) -{ - spl_array_object *intern; - - object_init_ex(*object, ce); - intern = (spl_array_object*)zend_object_store_get_object(*object TSRMLS_CC); - - if (intern->fptr_unserialize) { - zval *zdata; - php_unserialize_data_t *before; - MAKE_STD_ZVAL(zdata); - ZVAL_STRINGL(zdata, (char *)buf, buf_len, 1); - - before = intern->unserialize_data; - intern->unserialize_data = (php_unserialize_data_t *)data; - - zend_call_method_with_1_params(object, ce, &ce->unserialize_func, "unserialize", NULL, zdata); - - intern->unserialize_data = before; - - zval_ptr_dtor(&zdata); - } else { - spl_array_unserialize_helper(intern, buf, buf_len, (php_unserialize_data_t *)data TSRMLS_CC); - } - - if (EG(exception)) { - return FAILURE; - } else { - return SUCCESS; - } -} -/* }}} */ - /* {{{ arginfo and function tbale */ ZEND_BEGIN_ARG_INFO(arginfo_array___construct, 0) ZEND_ARG_INFO(0, array) @@ -1984,8 +1940,7 @@ PHP_MINIT_FUNCTION(spl_array) REGISTER_SPL_IMPLEMENTS(ArrayObject, Aggregate); REGISTER_SPL_IMPLEMENTS(ArrayObject, ArrayAccess); REGISTER_SPL_IMPLEMENTS(ArrayObject, Serializable); - spl_ce_ArrayObject->serialize = spl_array_serialize; - spl_ce_ArrayObject->unserialize = spl_array_unserialize; + REGISTER_SPL_IMPLEMENTS(ArrayObject, Countable); memcpy(&spl_handler_ArrayObject, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); spl_handler_ArrayObject.clone_obj = spl_array_object_clone; @@ -2003,22 +1958,20 @@ PHP_MINIT_FUNCTION(spl_array) spl_handler_ArrayObject.has_property = spl_array_has_property; spl_handler_ArrayObject.unset_property = spl_array_unset_property; + spl_handler_ArrayObject.compare_objects = spl_array_compare_objects; + REGISTER_SPL_STD_CLASS_EX(ArrayIterator, spl_array_object_new, spl_funcs_ArrayIterator); REGISTER_SPL_IMPLEMENTS(ArrayIterator, Iterator); REGISTER_SPL_IMPLEMENTS(ArrayIterator, ArrayAccess); REGISTER_SPL_IMPLEMENTS(ArrayIterator, SeekableIterator); REGISTER_SPL_IMPLEMENTS(ArrayIterator, Serializable); - spl_ce_ArrayIterator->serialize = spl_array_serialize; - spl_ce_ArrayIterator->unserialize = spl_array_unserialize; + REGISTER_SPL_IMPLEMENTS(ArrayIterator, Countable); memcpy(&spl_handler_ArrayIterator, &spl_handler_ArrayObject, sizeof(zend_object_handlers)); spl_ce_ArrayIterator->get_iterator = spl_array_get_iterator; REGISTER_SPL_SUB_CLASS_EX(RecursiveArrayIterator, ArrayIterator, spl_array_object_new, spl_funcs_RecursiveArrayIterator); REGISTER_SPL_IMPLEMENTS(RecursiveArrayIterator, RecursiveIterator); spl_ce_RecursiveArrayIterator->get_iterator = spl_array_get_iterator; - - REGISTER_SPL_IMPLEMENTS(ArrayObject, Countable); - REGISTER_SPL_IMPLEMENTS(ArrayIterator, Countable); REGISTER_SPL_CLASS_CONST_LONG(ArrayObject, "STD_PROP_LIST", SPL_ARRAY_STD_PROP_LIST); REGISTER_SPL_CLASS_CONST_LONG(ArrayObject, "ARRAY_AS_PROPS", SPL_ARRAY_ARRAY_AS_PROPS);