xref: /php-src/ext/spl/spl_array.c (revision d21bc7f1)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Marcus Boerger <helly@php.net>                              |
14    +----------------------------------------------------------------------+
15 */
16 
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
20 
21 #include "php.h"
22 #include "ext/standard/php_var.h"
23 #include "zend_smart_str.h"
24 #include "zend_interfaces.h"
25 #include "zend_exceptions.h"
26 
27 #include "spl_iterators.h"
28 #include "spl_array.h"
29 #include "spl_array_arginfo.h"
30 #include "spl_exceptions.h"
31 #include "spl_functions.h" /* For spl_set_private_debug_info_property() */
32 
33 /* Defined later in the file */
34 PHPAPI zend_class_entry  *spl_ce_ArrayIterator;
35 PHPAPI zend_class_entry  *spl_ce_RecursiveArrayIterator;
36 
37 /* ArrayObject class */
38 static zend_object_handlers spl_handler_ArrayObject;
39 PHPAPI zend_class_entry  *spl_ce_ArrayObject;
40 
41 typedef struct _spl_array_object {
42 	zval              array;
43 	uint32_t          ht_iter;
44 	int               ar_flags;
45 	unsigned char	  nApplyCount;
46 	bool			  is_child;
47 	Bucket			  *bucket;
48 	zend_function     *fptr_offset_get;
49 	zend_function     *fptr_offset_set;
50 	zend_function     *fptr_offset_has;
51 	zend_function     *fptr_offset_del;
52 	zend_function     *fptr_count;
53 	zend_class_entry* ce_get_iterator;
54 	zend_object       std;
55 } spl_array_object;
56 
spl_array_from_obj(zend_object * obj)57 static inline spl_array_object *spl_array_from_obj(zend_object *obj) /* {{{ */ {
58 	return (spl_array_object*)((char*)(obj) - XtOffsetOf(spl_array_object, std));
59 }
60 /* }}} */
61 
62 #define Z_SPLARRAY_P(zv)  spl_array_from_obj(Z_OBJ_P((zv)))
63 
spl_array_get_hash_table_ptr(spl_array_object * intern)64 static inline HashTable **spl_array_get_hash_table_ptr(spl_array_object* intern) { /* {{{ */
65 	//??? TODO: Delay duplication for arrays; only duplicate for write operations
66 	if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
67 		/* rebuild properties */
68 		zend_std_get_properties_ex(&intern->std);
69 		return &intern->std.properties;
70 	} else if (intern->ar_flags & SPL_ARRAY_USE_OTHER) {
71 		spl_array_object *other = Z_SPLARRAY_P(&intern->array);
72 		return spl_array_get_hash_table_ptr(other);
73 	} else if (Z_TYPE(intern->array) == IS_ARRAY) {
74 		return &Z_ARRVAL(intern->array);
75 	} else {
76 		zend_object *obj = Z_OBJ(intern->array);
77 		/* rebuild properties */
78 		zend_std_get_properties_ex(obj);
79 		if (GC_REFCOUNT(obj->properties) > 1) {
80 			if (EXPECTED(!(GC_FLAGS(obj->properties) & IS_ARRAY_IMMUTABLE))) {
81 				GC_DELREF(obj->properties);
82 			}
83 			obj->properties = zend_array_dup(obj->properties);
84 		}
85 		return &obj->properties;
86 	}
87 }
88 /* }}} */
89 
spl_array_get_hash_table(spl_array_object * intern)90 static inline HashTable *spl_array_get_hash_table(spl_array_object* intern) { /* {{{ */
91 	return *spl_array_get_hash_table_ptr(intern);
92 }
93 /* }}} */
94 
spl_array_is_object(spl_array_object * intern)95 static inline bool spl_array_is_object(spl_array_object *intern) /* {{{ */
96 {
97 	while (intern->ar_flags & SPL_ARRAY_USE_OTHER) {
98 		intern = Z_SPLARRAY_P(&intern->array);
99 	}
100 	return (intern->ar_flags & SPL_ARRAY_IS_SELF) || Z_TYPE(intern->array) == IS_OBJECT;
101 }
102 /* }}} */
103 
104 static zend_result spl_array_skip_protected(spl_array_object *intern, HashTable *aht);
105 
spl_array_create_ht_iter(HashTable * ht,spl_array_object * intern)106 static zend_never_inline void spl_array_create_ht_iter(HashTable *ht, spl_array_object* intern) /* {{{ */
107 {
108 	intern->ht_iter = zend_hash_iterator_add(ht, zend_hash_get_current_pos(ht));
109 	zend_hash_internal_pointer_reset_ex(ht, &EG(ht_iterators)[intern->ht_iter].pos);
110 	spl_array_skip_protected(intern, ht);
111 }
112 /* }}} */
113 
spl_array_get_pos_ptr(HashTable * ht,spl_array_object * intern)114 static zend_always_inline uint32_t *spl_array_get_pos_ptr(HashTable *ht, spl_array_object* intern) /* {{{ */
115 {
116 	if (UNEXPECTED(intern->ht_iter == (uint32_t)-1)) {
117 		spl_array_create_ht_iter(ht, intern);
118 	}
119 	return &EG(ht_iterators)[intern->ht_iter].pos;
120 }
121 /* }}} */
122 
123 /* {{{ spl_array_object_free_storage */
spl_array_object_free_storage(zend_object * object)124 static void spl_array_object_free_storage(zend_object *object)
125 {
126 	spl_array_object *intern = spl_array_from_obj(object);
127 
128 	if (intern->ht_iter != (uint32_t) -1) {
129 		zend_hash_iterator_del(intern->ht_iter);
130 	}
131 
132 	zend_object_std_dtor(&intern->std);
133 
134 	zval_ptr_dtor(&intern->array);
135 }
136 /* }}} */
137 
138 /* {{{ spl_array_object_new_ex */
spl_array_object_new_ex(zend_class_entry * class_type,zend_object * orig,int clone_orig)139 static zend_object *spl_array_object_new_ex(zend_class_entry *class_type, zend_object *orig, int clone_orig)
140 {
141 	spl_array_object *intern;
142 	zend_class_entry *parent = class_type;
143 	int inherited = 0;
144 
145 	intern = zend_object_alloc(sizeof(spl_array_object), parent);
146 
147 	zend_object_std_init(&intern->std, class_type);
148 	object_properties_init(&intern->std, class_type);
149 
150 	intern->ar_flags = 0;
151 	intern->is_child = false;
152 	intern->bucket = NULL;
153 	intern->ce_get_iterator = spl_ce_ArrayIterator;
154 	if (orig) {
155 		spl_array_object *other = spl_array_from_obj(orig);
156 
157 		intern->ar_flags &= ~ SPL_ARRAY_CLONE_MASK;
158 		intern->ar_flags |= (other->ar_flags & SPL_ARRAY_CLONE_MASK);
159 		intern->ce_get_iterator = other->ce_get_iterator;
160 		if (clone_orig) {
161 			if (other->ar_flags & SPL_ARRAY_IS_SELF) {
162 				ZVAL_UNDEF(&intern->array);
163 			} else if (instanceof_function(class_type, spl_ce_ArrayObject)) {
164 				ZVAL_ARR(&intern->array,
165 					zend_array_dup(spl_array_get_hash_table(other)));
166 			} else {
167 				#if ZEND_DEBUG
168 				/* This is because the call to instanceof_function will remain because
169 				 * the compiler can't prove in this compile unit that this function is
170 				 * side-effect-free.
171 				 * See https://github.com/php/php-src/pull/14518#discussion_r1638740932 */
172 				ZEND_ASSERT(instanceof_function(class_type, spl_ce_ArrayIterator));
173 				#endif
174 
175 				ZVAL_OBJ_COPY(&intern->array, orig);
176 				intern->ar_flags |= SPL_ARRAY_USE_OTHER;
177 			}
178 		} else {
179 			ZVAL_OBJ_COPY(&intern->array, orig);
180 			intern->ar_flags |= SPL_ARRAY_USE_OTHER;
181 		}
182 	} else {
183 		array_init(&intern->array);
184 	}
185 
186 	while (parent) {
187 		if (parent == spl_ce_ArrayIterator || parent == spl_ce_RecursiveArrayIterator || parent == spl_ce_ArrayObject) {
188 			break;
189 		}
190 		parent = parent->parent;
191 		inherited = 1;
192 	}
193 
194 	ZEND_ASSERT(parent);
195 
196 	if (inherited) {
197 		intern->fptr_offset_get = zend_hash_str_find_ptr(&class_type->function_table, "offsetget", sizeof("offsetget") - 1);
198 		if (intern->fptr_offset_get->common.scope == parent) {
199 			intern->fptr_offset_get = NULL;
200 		}
201 		intern->fptr_offset_set = zend_hash_str_find_ptr(&class_type->function_table, "offsetset", sizeof("offsetset") - 1);
202 		if (intern->fptr_offset_set->common.scope == parent) {
203 			intern->fptr_offset_set = NULL;
204 		}
205 		intern->fptr_offset_has = zend_hash_str_find_ptr(&class_type->function_table, "offsetexists", sizeof("offsetexists") - 1);
206 		if (intern->fptr_offset_has->common.scope == parent) {
207 			intern->fptr_offset_has = NULL;
208 		}
209 		intern->fptr_offset_del = zend_hash_str_find_ptr(&class_type->function_table, "offsetunset",  sizeof("offsetunset") - 1);
210 		if (intern->fptr_offset_del->common.scope == parent) {
211 			intern->fptr_offset_del = NULL;
212 		}
213 		/* Find count() method */
214 		intern->fptr_count = zend_hash_find_ptr(&class_type->function_table, ZSTR_KNOWN(ZEND_STR_COUNT));
215 		if (intern->fptr_count->common.scope == parent) {
216 			intern->fptr_count = NULL;
217 		}
218 	}
219 
220 	intern->ht_iter = (uint32_t)-1;
221 	return &intern->std;
222 }
223 /* }}} */
224 
225 /* {{{ spl_array_object_new */
spl_array_object_new(zend_class_entry * class_type)226 static zend_object *spl_array_object_new(zend_class_entry *class_type)
227 {
228 	return spl_array_object_new_ex(class_type, NULL, 0);
229 }
230 /* }}} */
231 
232 /* {{{ spl_array_object_clone */
spl_array_object_clone(zend_object * old_object)233 static zend_object *spl_array_object_clone(zend_object *old_object)
234 {
235 	zend_object *new_object;
236 
237 	new_object = spl_array_object_new_ex(old_object->ce, old_object, 1);
238 
239 	zend_objects_clone_members(new_object, old_object);
240 
241 	return new_object;
242 }
243 /* }}} */
244 
245 typedef struct {
246 	zend_string *key;
247 	zend_ulong h;
248 	bool release_key;
249 } spl_hash_key;
250 
spl_hash_key_release(spl_hash_key * key)251 static void spl_hash_key_release(spl_hash_key *key) {
252 	if (key->release_key) {
253 		zend_string_release_ex(key->key, 0);
254 	}
255 }
256 
257 /* This function does not throw any exceptions for illegal offsets, calls to
258  * zend_illegal_container_offset(); need to be made if the return value is FAILURE */
get_hash_key(spl_hash_key * key,spl_array_object * intern,zval * offset)259 static zend_result get_hash_key(spl_hash_key *key, spl_array_object *intern, zval *offset)
260 {
261 	key->release_key = false;
262 try_again:
263 	switch (Z_TYPE_P(offset)) {
264 	case IS_NULL:
265 		key->key = ZSTR_EMPTY_ALLOC();
266 		return SUCCESS;
267 	case IS_STRING:
268 		key->key = Z_STR_P(offset);
269 		if (ZEND_HANDLE_NUMERIC(key->key, key->h)) {
270 			key->key = NULL;
271 			break;
272 		}
273 		return SUCCESS;
274 	case IS_RESOURCE:
275 		zend_use_resource_as_offset(offset);
276 		key->key = NULL;
277 		key->h = Z_RES_P(offset)->handle;
278 		break;
279 	case IS_DOUBLE:
280 		key->key = NULL;
281 		key->h = zend_dval_to_lval_safe(Z_DVAL_P(offset));
282 		break;
283 	case IS_FALSE:
284 		key->key = NULL;
285 		key->h = 0;
286 		break;
287 	case IS_TRUE:
288 		key->key = NULL;
289 		key->h = 1;
290 		break;
291 	case IS_LONG:
292 		key->key = NULL;
293 		key->h = Z_LVAL_P(offset);
294 		break;
295 	case IS_REFERENCE:
296 		ZVAL_DEREF(offset);
297 		goto try_again;
298 	default:
299 		return FAILURE;
300 	}
301 
302 	if (spl_array_is_object(intern)) {
303 		key->key = zend_long_to_str(key->h);
304 		key->release_key = true;
305 	}
306 	return SUCCESS;
307 }
308 
spl_array_get_dimension_ptr(bool check_inherited,spl_array_object * intern,const zend_string * ce_name,zval * offset,int type)309 static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object *intern, const zend_string *ce_name,
310 	zval *offset, int type) /* {{{ */
311 {
312 	zval *retval;
313 	spl_hash_key key;
314 	HashTable *ht = spl_array_get_hash_table(intern);
315 
316 	if (!offset || Z_ISUNDEF_P(offset) || !ht) {
317 		return &EG(uninitialized_zval);
318 	}
319 
320 	if ((type == BP_VAR_W || type == BP_VAR_RW) && intern->nApplyCount > 0) {
321 		zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
322 		return &EG(error_zval);
323 	}
324 
325 	if (get_hash_key(&key, intern, offset) == FAILURE) {
326 		zend_illegal_container_offset(ce_name, offset, type);
327 		return (type == BP_VAR_W || type == BP_VAR_RW) ?
328 			&EG(error_zval) : &EG(uninitialized_zval);
329 	}
330 
331 	if (key.key) {
332 		retval = zend_hash_find(ht, key.key);
333 		if (retval) {
334 			if (Z_TYPE_P(retval) == IS_INDIRECT) {
335 				retval = Z_INDIRECT_P(retval);
336 				if (Z_TYPE_P(retval) == IS_UNDEF) {
337 					switch (type) {
338 						case BP_VAR_R:
339 							zend_error(E_WARNING, "Undefined array key \"%s\"", ZSTR_VAL(key.key));
340 							ZEND_FALLTHROUGH;
341 						case BP_VAR_UNSET:
342 						case BP_VAR_IS:
343 							retval = &EG(uninitialized_zval);
344 							break;
345 						case BP_VAR_RW:
346 							zend_error(E_WARNING,"Undefined array key \"%s\"", ZSTR_VAL(key.key));
347 							ZEND_FALLTHROUGH;
348 						case BP_VAR_W: {
349 							ZVAL_NULL(retval);
350 						}
351 					}
352 				}
353 			}
354 		} else {
355 			switch (type) {
356 				case BP_VAR_R:
357 					zend_error(E_WARNING, "Undefined array key \"%s\"", ZSTR_VAL(key.key));
358 					ZEND_FALLTHROUGH;
359 				case BP_VAR_UNSET:
360 				case BP_VAR_IS:
361 					retval = &EG(uninitialized_zval);
362 					break;
363 				case BP_VAR_RW:
364 					zend_error(E_WARNING,"Undefined array key \"%s\"", ZSTR_VAL(key.key));
365 					ZEND_FALLTHROUGH;
366 				case BP_VAR_W: {
367 				    zval value;
368 					ZVAL_NULL(&value);
369 				    retval = zend_hash_update(ht, key.key, &value);
370 				}
371 			}
372 		}
373 		spl_hash_key_release(&key);
374 	} else {
375 		if ((retval = zend_hash_index_find(ht, key.h)) == NULL) {
376 			switch (type) {
377 				case BP_VAR_R:
378 					zend_error(E_WARNING, "Undefined array key " ZEND_LONG_FMT, key.h);
379 					ZEND_FALLTHROUGH;
380 				case BP_VAR_UNSET:
381 				case BP_VAR_IS:
382 					retval = &EG(uninitialized_zval);
383 					break;
384 				case BP_VAR_RW:
385 					zend_error(E_WARNING, "Undefined array key " ZEND_LONG_FMT, key.h);
386 					ZEND_FALLTHROUGH;
387 				case BP_VAR_W: {
388 				    zval value;
389 					ZVAL_NULL(&value);
390 					retval = zend_hash_index_update(ht, key.h, &value);
391 			   }
392 			}
393 		}
394 	}
395 	return retval;
396 } /* }}} */
397 
398 static int spl_array_has_dimension(zend_object *object, zval *offset, int check_empty);
399 
spl_array_read_dimension_ex(int check_inherited,zend_object * object,zval * offset,int type,zval * rv)400 static zval *spl_array_read_dimension_ex(int check_inherited, zend_object *object, zval *offset, int type, zval *rv) /* {{{ */
401 {
402 	spl_array_object *intern = spl_array_from_obj(object);
403 	zval *ret;
404 
405 	if (check_inherited &&
406 			(intern->fptr_offset_get || (type == BP_VAR_IS && intern->fptr_offset_has))) {
407 		if (type == BP_VAR_IS) {
408 			if (!spl_array_has_dimension(object, offset, 0)) {
409 				return &EG(uninitialized_zval);
410 			}
411 		}
412 
413 		if (intern->fptr_offset_get) {
414 			zval tmp;
415 			if (!offset) {
416 				ZVAL_UNDEF(&tmp);
417 				offset = &tmp;
418 			}
419 			zend_call_method_with_1_params(object, object->ce, &intern->fptr_offset_get, "offsetGet", rv, offset);
420 
421 			if (!Z_ISUNDEF_P(rv)) {
422 				return rv;
423 			}
424 			return &EG(uninitialized_zval);
425 		}
426 	}
427 
428 	ret = spl_array_get_dimension_ptr(check_inherited, intern, object->ce->name, offset, type);
429 
430 	/* When in a write context,
431 	 * ZE has to be fooled into thinking this is in a reference set
432 	 * by separating (if necessary) and returning as IS_REFERENCE (with refcount == 1)
433 	 */
434 
435 	if ((type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) &&
436 	    !Z_ISREF_P(ret) &&
437 	    EXPECTED(ret != &EG(uninitialized_zval))) {
438 		ZVAL_NEW_REF(ret, ret);
439 	}
440 
441 	return ret;
442 } /* }}} */
443 
spl_array_read_dimension(zend_object * object,zval * offset,int type,zval * rv)444 static zval *spl_array_read_dimension(zend_object *object, zval *offset, int type, zval *rv) /* {{{ */
445 {
446 	return spl_array_read_dimension_ex(1, object, offset, type, rv);
447 } /* }}} */
448 
449 /*
450  * The assertion(HT_ASSERT_RC1(ht)) failed because the refcount was increased manually when intern->is_child is true.
451  * We have to set the refcount to 1 to make assertion success and restore the refcount to the original value after
452  * modifying the array when intern->is_child is true.
453  */
spl_array_set_refcount(bool is_child,HashTable * ht,uint32_t refcount)454 static uint32_t spl_array_set_refcount(bool is_child, HashTable *ht, uint32_t refcount) /* {{{ */
455 {
456 	uint32_t old_refcount = 0;
457 	if (is_child) {
458 		old_refcount = GC_REFCOUNT(ht);
459 		GC_SET_REFCOUNT(ht, refcount);
460 	}
461 
462 	return old_refcount;
463 } /* }}} */
464 
spl_array_write_dimension_ex(int check_inherited,zend_object * object,zval * offset,zval * value)465 static void spl_array_write_dimension_ex(int check_inherited, zend_object *object, zval *offset, zval *value) /* {{{ */
466 {
467 	spl_array_object *intern = spl_array_from_obj(object);
468 	HashTable *ht;
469 	spl_hash_key key;
470 
471 	if (check_inherited && intern->fptr_offset_set) {
472 		zval tmp;
473 
474 		if (!offset) {
475 			ZVAL_NULL(&tmp);
476 			offset = &tmp;
477 		}
478 		zend_call_method_with_2_params(object, object->ce, &intern->fptr_offset_set, "offsetSet", NULL, offset, value);
479 		return;
480 	}
481 
482 	if (intern->nApplyCount > 0) {
483 		zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
484 		return;
485 	}
486 
487 	Z_TRY_ADDREF_P(value);
488 
489 	uint32_t refcount = 0;
490 	if (!offset || Z_TYPE_P(offset) == IS_NULL) {
491 		ht = spl_array_get_hash_table(intern);
492 		refcount = spl_array_set_refcount(intern->is_child, ht, 1);
493 		zend_hash_next_index_insert(ht, value);
494 
495 		if (refcount) {
496 			spl_array_set_refcount(intern->is_child, ht, refcount);
497 		}
498 		return;
499 	}
500 
501 	if (get_hash_key(&key, intern, offset) == FAILURE) {
502 		zend_illegal_container_offset(object->ce->name, offset, BP_VAR_W);
503 		zval_ptr_dtor(value);
504 		return;
505 	}
506 
507 	ht = spl_array_get_hash_table(intern);
508 	refcount = spl_array_set_refcount(intern->is_child, ht, 1);
509 	if (key.key) {
510 		zend_hash_update_ind(ht, key.key, value);
511 		spl_hash_key_release(&key);
512 	} else {
513 		zend_hash_index_update(ht, key.h, value);
514 	}
515 
516 	if (refcount) {
517 		spl_array_set_refcount(intern->is_child, ht, refcount);
518 	}
519 } /* }}} */
520 
spl_array_write_dimension(zend_object * object,zval * offset,zval * value)521 static void spl_array_write_dimension(zend_object *object, zval *offset, zval *value) /* {{{ */
522 {
523 	spl_array_write_dimension_ex(1, object, offset, value);
524 } /* }}} */
525 
spl_array_unset_dimension_ex(int check_inherited,zend_object * object,zval * offset)526 static void spl_array_unset_dimension_ex(int check_inherited, zend_object *object, zval *offset) /* {{{ */
527 {
528 	HashTable *ht;
529 	spl_array_object *intern = spl_array_from_obj(object);
530 	spl_hash_key key;
531 
532 	if (check_inherited && intern->fptr_offset_del) {
533 		zend_call_method_with_1_params(object, object->ce, &intern->fptr_offset_del, "offsetUnset", NULL, offset);
534 		return;
535 	}
536 
537 	if (intern->nApplyCount > 0) {
538 		zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
539 		return;
540 	}
541 
542 	if (get_hash_key(&key, intern, offset) == FAILURE) {
543 		zend_illegal_container_offset(object->ce->name, offset, BP_VAR_UNSET);
544 		return;
545 	}
546 
547 	ht = spl_array_get_hash_table(intern);
548 	uint32_t refcount = spl_array_set_refcount(intern->is_child, ht, 1);
549 
550 	if (key.key) {
551 		zval *data = zend_hash_find(ht, key.key);
552 		if (data) {
553 			if (Z_TYPE_P(data) == IS_INDIRECT) {
554 				data = Z_INDIRECT_P(data);
555 				if (Z_TYPE_P(data) != IS_UNDEF) {
556 					zval_ptr_dtor(data);
557 					ZVAL_UNDEF(data);
558 					HT_FLAGS(ht) |= HASH_FLAG_HAS_EMPTY_IND;
559 					zend_hash_move_forward_ex(ht, spl_array_get_pos_ptr(ht, intern));
560 					if (spl_array_is_object(intern)) {
561 						spl_array_skip_protected(intern, ht);
562 					}
563 				}
564 			} else {
565 				zend_hash_del(ht, key.key);
566 			}
567 		}
568 		spl_hash_key_release(&key);
569 	} else {
570 		zend_hash_index_del(ht, key.h);
571 	}
572 
573 	if (refcount) {
574 		spl_array_set_refcount(intern->is_child, ht, refcount);
575 	}
576 } /* }}} */
577 
spl_array_unset_dimension(zend_object * object,zval * offset)578 static void spl_array_unset_dimension(zend_object *object, zval *offset) /* {{{ */
579 {
580 	spl_array_unset_dimension_ex(1, object, offset);
581 } /* }}} */
582 
583 /* check_empty can take value 0, 1, or 2
584  * 0/1 are used as normal boolean, but 2 is used for the case when this function is called from
585  * the offsetExists() method, in which case it needs to report the offset exist even if the value is null */
spl_array_has_dimension_ex(bool check_inherited,zend_object * object,zval * offset,int check_empty)586 static bool spl_array_has_dimension_ex(bool check_inherited, zend_object *object, zval *offset, int check_empty) /* {{{ */
587 {
588 	spl_array_object *intern = spl_array_from_obj(object);
589 	zval rv, *value = NULL, *tmp;
590 
591 	if (check_inherited && intern->fptr_offset_has) {
592 		zend_call_method_with_1_params(object, object->ce, &intern->fptr_offset_has, "offsetExists", &rv, offset);
593 
594 		if (!zend_is_true(&rv)) {
595 			zval_ptr_dtor(&rv);
596 			return 0;
597 		}
598 		zval_ptr_dtor(&rv);
599 
600 		/* For isset calls we don't need to check the value, so return early */
601 		if (!check_empty) {
602 			return 1;
603 		} else if (intern->fptr_offset_get) {
604 			value = spl_array_read_dimension_ex(1, object, offset, BP_VAR_R, &rv);
605 		}
606 	}
607 
608 	if (!value) {
609 		HashTable *ht = spl_array_get_hash_table(intern);
610 		spl_hash_key key;
611 
612 		if (get_hash_key(&key, intern, offset) == FAILURE) {
613 			zend_illegal_container_offset(object->ce->name, offset, BP_VAR_IS);
614 			return 0;
615 		}
616 
617 		if (key.key) {
618 			tmp = zend_hash_find(ht, key.key);
619 			spl_hash_key_release(&key);
620 		} else {
621 			tmp = zend_hash_index_find(ht, key.h);
622 		}
623 
624 		if (!tmp) {
625 			return 0;
626 		}
627 
628 		/* check_empty is only equal to 2 if it is called from offsetExists on this class,
629 		 * where it needs to report an offset exists even if the value is null */
630 		if (check_empty == 2) {
631 			return 1;
632 		}
633 
634 		if (check_empty && check_inherited && intern->fptr_offset_get) {
635 			value = spl_array_read_dimension_ex(1, object, offset, BP_VAR_R, &rv);
636 		} else {
637 			value = tmp;
638 		}
639 	}
640 
641 	if (value == &rv) {
642 		zval_ptr_dtor(&rv);
643 	}
644 
645 	/* empty() check the value is not falsy, isset() only check it is not null */
646 	return check_empty ? zend_is_true(value) : Z_TYPE_P(value) != IS_NULL;
647 } /* }}} */
648 
spl_array_has_dimension(zend_object * object,zval * offset,int check_empty)649 static int spl_array_has_dimension(zend_object *object, zval *offset, int check_empty) /* {{{ */
650 {
651 	return spl_array_has_dimension_ex(/* check_inherited */ true, object, offset, check_empty);
652 } /* }}} */
653 
654 /* {{{ Returns whether the requested $index exists. */
PHP_METHOD(ArrayObject,offsetExists)655 PHP_METHOD(ArrayObject, offsetExists)
656 {
657 	zval *index;
658 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
659 		RETURN_THROWS();
660 	}
661 	RETURN_BOOL(spl_array_has_dimension_ex(/* check_inherited */ false, Z_OBJ_P(ZEND_THIS), index, 2));
662 } /* }}} */
663 
664 /* {{{ Returns the value at the specified $index. */
PHP_METHOD(ArrayObject,offsetGet)665 PHP_METHOD(ArrayObject, offsetGet)
666 {
667 	zval *value, *index;
668 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
669 		RETURN_THROWS();
670 	}
671 	value = spl_array_read_dimension_ex(0, Z_OBJ_P(ZEND_THIS), index, BP_VAR_R, return_value);
672 	if (value != return_value) {
673 		RETURN_COPY_DEREF(value);
674 	}
675 } /* }}} */
676 
677 /* {{{ Sets the value at the specified $index to $newval. */
PHP_METHOD(ArrayObject,offsetSet)678 PHP_METHOD(ArrayObject, offsetSet)
679 {
680 	zval *index, *value;
681 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &index, &value) == FAILURE) {
682 		RETURN_THROWS();
683 	}
684 	spl_array_write_dimension_ex(0, Z_OBJ_P(ZEND_THIS), index, value);
685 } /* }}} */
686 
spl_array_iterator_append(zval * object,zval * append_value)687 void spl_array_iterator_append(zval *object, zval *append_value) /* {{{ */
688 {
689 	spl_array_object *intern = Z_SPLARRAY_P(object);
690 
691 	if (spl_array_is_object(intern)) {
692 		zend_throw_error(NULL, "Cannot append properties to objects, use %s::offsetSet() instead", ZSTR_VAL(Z_OBJCE_P(object)->name));
693 		return;
694 	}
695 
696 	spl_array_write_dimension(Z_OBJ_P(object), NULL, append_value);
697 } /* }}} */
698 
699 /* {{{ Appends the value (cannot be called for objects). */
PHP_METHOD(ArrayObject,append)700 PHP_METHOD(ArrayObject, append)
701 {
702 	zval *value;
703 
704 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
705 		RETURN_THROWS();
706 	}
707 	spl_array_iterator_append(ZEND_THIS, value);
708 } /* }}} */
709 
710 /* {{{ Unsets the value at the specified $index. */
PHP_METHOD(ArrayObject,offsetUnset)711 PHP_METHOD(ArrayObject, offsetUnset)
712 {
713 	zval *index;
714 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
715 		RETURN_THROWS();
716 	}
717 	spl_array_unset_dimension_ex(0, Z_OBJ_P(ZEND_THIS), index);
718 } /* }}} */
719 
720 /* {{{ Return a copy of the contained array */
PHP_METHOD(ArrayObject,getArrayCopy)721 PHP_METHOD(ArrayObject, getArrayCopy)
722 {
723 	zval *object = ZEND_THIS;
724 	spl_array_object *intern = Z_SPLARRAY_P(object);
725 
726 	if (zend_parse_parameters_none() == FAILURE) {
727 		RETURN_THROWS();
728 	}
729 
730 	RETURN_ARR(zend_array_dup(spl_array_get_hash_table(intern)));
731 } /* }}} */
732 
spl_array_get_properties_for(zend_object * object,zend_prop_purpose purpose)733 static HashTable *spl_array_get_properties_for(zend_object *object, zend_prop_purpose purpose) /* {{{ */
734 {
735 	spl_array_object *intern = spl_array_from_obj(object);
736 	HashTable *ht;
737 	bool dup;
738 
739 	if (intern->ar_flags & SPL_ARRAY_STD_PROP_LIST) {
740 		return zend_std_get_properties_for(object, purpose);
741 	}
742 
743 	/* We are supposed to be the only owner of the internal hashtable.
744 	 * The "dup" flag decides whether this is a "long-term" use where
745 	 * we need to duplicate, or a "temporary" one, where we can expect
746 	 * that no operations on the ArrayObject will be performed in the
747 	 * meantime. */
748 	switch (purpose) {
749 		case ZEND_PROP_PURPOSE_ARRAY_CAST:
750 			dup = 1;
751 			break;
752 		case ZEND_PROP_PURPOSE_VAR_EXPORT:
753 		case ZEND_PROP_PURPOSE_JSON:
754 			dup = 0;
755 			break;
756 		default:
757 			return zend_std_get_properties_for(object, purpose);
758 	}
759 
760 	ht = spl_array_get_hash_table(intern);
761 	if (dup) {
762 		ht = zend_array_dup(ht);
763 	} else {
764 		GC_ADDREF(ht);
765 	}
766 	return ht;
767 } /* }}} */
768 
spl_array_get_debug_info(zend_object * obj)769 static inline HashTable* spl_array_get_debug_info(zend_object *obj) /* {{{ */
770 {
771 	spl_array_object *intern = spl_array_from_obj(obj);
772 	HashTable *properties = zend_std_get_properties_ex(&intern->std);
773 
774 	if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
775 		return zend_array_dup(properties);
776 	} else {
777 		HashTable *debug_info;
778 
779 		debug_info = zend_new_array(zend_hash_num_elements(properties) + 1);
780 		zend_hash_copy(debug_info, properties, (copy_ctor_func_t) zval_add_ref);
781 
782 		zval *storage = &intern->array;
783 		Z_TRY_ADDREF_P(storage);
784 
785 		const zend_class_entry *base_class_ce = instanceof_function(obj->ce, spl_ce_ArrayIterator)
786 			? spl_ce_ArrayIterator : spl_ce_ArrayObject;
787 
788 		spl_set_private_debug_info_property(base_class_ce, "storage", strlen("storage"), debug_info, storage);
789 
790 		return debug_info;
791 	}
792 }
793 /* }}} */
794 
spl_array_get_gc(zend_object * obj,zval ** gc_data,int * gc_data_count)795 static HashTable *spl_array_get_gc(zend_object *obj, zval **gc_data, int *gc_data_count) /* {{{ */
796 {
797 	spl_array_object *intern = spl_array_from_obj(obj);
798 	*gc_data = &intern->array;
799 	*gc_data_count = 1;
800 	return zend_std_get_properties(obj);
801 }
802 /* }}} */
803 
spl_array_read_property(zend_object * object,zend_string * name,int type,void ** cache_slot,zval * rv)804 static zval *spl_array_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv) /* {{{ */
805 {
806 	spl_array_object *intern = spl_array_from_obj(object);
807 
808 	if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
809 		&& !zend_std_has_property(object, name, ZEND_PROPERTY_EXISTS, NULL)) {
810 		zval member;
811 		ZVAL_STR(&member, name);
812 		return spl_array_read_dimension(object, &member, type, rv);
813 	}
814 	return zend_std_read_property(object, name, type, cache_slot, rv);
815 } /* }}} */
816 
spl_array_write_property(zend_object * object,zend_string * name,zval * value,void ** cache_slot)817 static zval *spl_array_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot) /* {{{ */
818 {
819 	spl_array_object *intern = spl_array_from_obj(object);
820 
821 	if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
822 	&& !zend_std_has_property(object, name, ZEND_PROPERTY_EXISTS, NULL)) {
823 		zval member;
824 		ZVAL_STR(&member, name);
825 		spl_array_write_dimension(object, &member, value);
826 		return value;
827 	}
828 	return zend_std_write_property(object, name, value, cache_slot);
829 } /* }}} */
830 
spl_array_get_property_ptr_ptr(zend_object * object,zend_string * name,int type,void ** cache_slot)831 static zval *spl_array_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot) /* {{{ */
832 {
833 	spl_array_object *intern = spl_array_from_obj(object);
834 
835 	if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
836 		&& !zend_std_has_property(object, name, ZEND_PROPERTY_EXISTS, NULL)) {
837 		/* If object has offsetGet() overridden, then fallback to read_property,
838 		 * which will call offsetGet(). */
839 		zval member;
840 		if (intern->fptr_offset_get) {
841 			return NULL;
842 		}
843 		ZVAL_STR(&member, name);
844 		return spl_array_get_dimension_ptr(1, intern, object->ce->name, &member, type);
845 	}
846 	return zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
847 } /* }}} */
848 
spl_array_has_property(zend_object * object,zend_string * name,int has_set_exists,void ** cache_slot)849 static int spl_array_has_property(zend_object *object, zend_string *name, int has_set_exists, void **cache_slot) /* {{{ */
850 {
851 	spl_array_object *intern = spl_array_from_obj(object);
852 
853 	if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
854 		&& !zend_std_has_property(object, name, ZEND_PROPERTY_EXISTS, NULL)) {
855 		zval member;
856 		ZVAL_STR(&member, name);
857 		return spl_array_has_dimension(object, &member, has_set_exists);
858 	}
859 	return zend_std_has_property(object, name, has_set_exists, cache_slot);
860 } /* }}} */
861 
spl_array_unset_property(zend_object * object,zend_string * name,void ** cache_slot)862 static void spl_array_unset_property(zend_object *object, zend_string *name, void **cache_slot) /* {{{ */
863 {
864 	spl_array_object *intern = spl_array_from_obj(object);
865 
866 	if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
867 		&& !zend_std_has_property(object, name, ZEND_PROPERTY_EXISTS, NULL)) {
868 		zval member;
869 		ZVAL_STR(&member, name);
870 		spl_array_unset_dimension(object, &member);
871 		return;
872 	}
873 	zend_std_unset_property(object, name, cache_slot);
874 } /* }}} */
875 
spl_array_compare_objects(zval * o1,zval * o2)876 static int spl_array_compare_objects(zval *o1, zval *o2) /* {{{ */
877 {
878 	HashTable			*ht1,
879 						*ht2;
880 	spl_array_object	*intern1,
881 						*intern2;
882 	int					result	= 0;
883 
884 	ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2);
885 
886 	intern1	= Z_SPLARRAY_P(o1);
887 	intern2	= Z_SPLARRAY_P(o2);
888 	ht1		= spl_array_get_hash_table(intern1);
889 	ht2		= spl_array_get_hash_table(intern2);
890 
891 	result = zend_compare_symbol_tables(ht1, ht2);
892 	/* if we just compared std.properties, don't do it again */
893 	if (result == 0 &&
894 			!(ht1 == intern1->std.properties && ht2 == intern2->std.properties)) {
895 		result = zend_std_compare_objects(o1, o2);
896 	}
897 	return result;
898 } /* }}} */
899 
spl_array_skip_protected(spl_array_object * intern,HashTable * aht)900 static zend_result spl_array_skip_protected(spl_array_object *intern, HashTable *aht) /* {{{ */
901 {
902 	zend_string *string_key;
903 	zend_ulong num_key;
904 	zval *data;
905 
906 	if (spl_array_is_object(intern)) {
907 		uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
908 
909 		do {
910 			if (zend_hash_get_current_key_ex(aht, &string_key, &num_key, pos_ptr) == HASH_KEY_IS_STRING) {
911 				data = zend_hash_get_current_data_ex(aht, pos_ptr);
912 				if (data && Z_TYPE_P(data) == IS_INDIRECT &&
913 				    Z_TYPE_P(data = Z_INDIRECT_P(data)) == IS_UNDEF) {
914 					/* skip */
915 				} else if (!ZSTR_LEN(string_key) || ZSTR_VAL(string_key)[0]) {
916 					return SUCCESS;
917 				}
918 			} else {
919 				return SUCCESS;
920 			}
921 			if (zend_hash_has_more_elements_ex(aht, pos_ptr) != SUCCESS) {
922 				return FAILURE;
923 			}
924 			zend_hash_move_forward_ex(aht, pos_ptr);
925 		} while (1);
926 	}
927 	return FAILURE;
928 } /* }}} */
929 
930 /* {{{ spl_array_set_array */
spl_array_set_array(zval * object,spl_array_object * intern,zval * array,zend_long ar_flags,bool just_array)931 static void spl_array_set_array(zval *object, spl_array_object *intern, zval *array, zend_long ar_flags, bool just_array) {
932 	/* Handled by ZPP prior to this, or for __unserialize() before passing to here */
933 	ZEND_ASSERT(Z_TYPE_P(array) == IS_ARRAY || Z_TYPE_P(array) == IS_OBJECT);
934 	if (Z_TYPE_P(array) == IS_ARRAY) {
935 		zval_ptr_dtor(&intern->array);
936 		if (Z_REFCOUNT_P(array) == 1) {
937 			ZVAL_COPY(&intern->array, array);
938 		} else {
939 			//??? TODO: try to avoid array duplication
940 			ZVAL_ARR(&intern->array, zend_array_dup(Z_ARR_P(array)));
941 
942 			if (intern->is_child) {
943 				Z_TRY_DELREF(intern->bucket->val);
944 				/*
945 				 * replace bucket->val with copied array, so the changes between
946 				 * parent and child object can affect each other.
947 				 */
948 				ZVAL_COPY(&intern->bucket->val, &intern->array);
949 			}
950 		}
951 	} else {
952 		if (Z_OBJ_HT_P(array) == &spl_handler_ArrayObject) {
953 			zval_ptr_dtor(&intern->array);
954 			if (just_array)	{
955 				spl_array_object *other = Z_SPLARRAY_P(array);
956 				ar_flags = other->ar_flags & ~SPL_ARRAY_INT_MASK;
957 			}
958 			if (Z_OBJ_P(object) == Z_OBJ_P(array)) {
959 				ar_flags |= SPL_ARRAY_IS_SELF;
960 				ZVAL_UNDEF(&intern->array);
961 			} else {
962 				ar_flags |= SPL_ARRAY_USE_OTHER;
963 				ZVAL_COPY(&intern->array, array);
964 			}
965 		} else {
966 			zend_object_get_properties_t handler = Z_OBJ_HANDLER_P(array, get_properties);
967 			if (handler != zend_std_get_properties || Z_OBJ_HANDLER_P(array, get_properties_for)) {
968 				zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
969 					"Overloaded object of type %s is not compatible with %s",
970 					ZSTR_VAL(Z_OBJCE_P(array)->name), ZSTR_VAL(intern->std.ce->name));
971 				return;
972 			}
973 			if (UNEXPECTED(Z_OBJCE_P(array)->ce_flags & ZEND_ACC_ENUM)) {
974 				zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
975 					"Enums are not compatible with %s",
976 					ZSTR_VAL(intern->std.ce->name));
977 				return;
978 			}
979 			zval_ptr_dtor(&intern->array);
980 			ZVAL_COPY(&intern->array, array);
981 		}
982 	}
983 
984 	intern->ar_flags &= ~SPL_ARRAY_IS_SELF & ~SPL_ARRAY_USE_OTHER;
985 	intern->ar_flags |= ar_flags;
986 	if (intern->ht_iter != (uint32_t)-1) {
987 		zend_hash_iterator_del(intern->ht_iter);
988 		intern->ht_iter = (uint32_t)-1;
989 	}
990 }
991 /* }}} */
992 
993 /* {{{ Constructs a new array object from an array or object. */
PHP_METHOD(ArrayObject,__construct)994 PHP_METHOD(ArrayObject, __construct)
995 {
996 	zval *object = ZEND_THIS;
997 	spl_array_object *intern;
998 	zval *array;
999 	zend_long ar_flags = 0;
1000 	zend_class_entry *ce_get_iterator = spl_ce_ArrayIterator;
1001 
1002 	if (ZEND_NUM_ARGS() == 0) {
1003 		return; /* nothing to do */
1004 	}
1005 
1006 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|AlC", &array, &ar_flags, &ce_get_iterator) == FAILURE) {
1007 		RETURN_THROWS();
1008 	}
1009 
1010 	intern = Z_SPLARRAY_P(object);
1011 
1012 	if (ZEND_NUM_ARGS() > 2) {
1013 		intern->ce_get_iterator = ce_get_iterator;
1014 	}
1015 
1016 	ar_flags &= ~SPL_ARRAY_INT_MASK;
1017 
1018 	spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
1019 }
1020 /* }}} */
1021 
1022 /* {{{ Set the class used in getIterator. */
PHP_METHOD(ArrayObject,setIteratorClass)1023 PHP_METHOD(ArrayObject, setIteratorClass)
1024 {
1025 	zval *object = ZEND_THIS;
1026 	spl_array_object *intern = Z_SPLARRAY_P(object);
1027 	zend_class_entry *ce_get_iterator = spl_ce_ArrayIterator;
1028 
1029 	ZEND_PARSE_PARAMETERS_START(1, 1)
1030 		Z_PARAM_CLASS(ce_get_iterator)
1031 	ZEND_PARSE_PARAMETERS_END();
1032 
1033 	intern->ce_get_iterator = ce_get_iterator;
1034 }
1035 /* }}} */
1036 
1037 /* {{{ Get the class used in getIterator. */
PHP_METHOD(ArrayObject,getIteratorClass)1038 PHP_METHOD(ArrayObject, getIteratorClass)
1039 {
1040 	zval *object = ZEND_THIS;
1041 	spl_array_object *intern = Z_SPLARRAY_P(object);
1042 
1043 	if (zend_parse_parameters_none() == FAILURE) {
1044 		RETURN_THROWS();
1045 	}
1046 
1047 	zend_string_addref(intern->ce_get_iterator->name);
1048 	RETURN_STR(intern->ce_get_iterator->name);
1049 }
1050 /* }}} */
1051 
1052 /* {{{ Get flags */
PHP_METHOD(ArrayObject,getFlags)1053 PHP_METHOD(ArrayObject, getFlags)
1054 {
1055 	zval *object = ZEND_THIS;
1056 	spl_array_object *intern = Z_SPLARRAY_P(object);
1057 
1058 	if (zend_parse_parameters_none() == FAILURE) {
1059 		RETURN_THROWS();
1060 	}
1061 
1062 	RETURN_LONG(intern->ar_flags & ~SPL_ARRAY_INT_MASK);
1063 }
1064 /* }}} */
1065 
1066 /* {{{ Set flags */
PHP_METHOD(ArrayObject,setFlags)1067 PHP_METHOD(ArrayObject, setFlags)
1068 {
1069 	zval *object = ZEND_THIS;
1070 	spl_array_object *intern = Z_SPLARRAY_P(object);
1071 	zend_long ar_flags = 0;
1072 
1073 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &ar_flags) == FAILURE) {
1074 		RETURN_THROWS();
1075 	}
1076 
1077 	intern->ar_flags = (intern->ar_flags & SPL_ARRAY_INT_MASK) | (ar_flags & ~SPL_ARRAY_INT_MASK);
1078 }
1079 /* }}} */
1080 
1081 /* {{{ Replace the referenced array or object with a new one and return the old one (right now copy - to be changed) */
PHP_METHOD(ArrayObject,exchangeArray)1082 PHP_METHOD(ArrayObject, exchangeArray)
1083 {
1084 	zval *object = ZEND_THIS, *array;
1085 	spl_array_object *intern = Z_SPLARRAY_P(object);
1086 
1087 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "A", &array) == FAILURE) {
1088 		RETURN_THROWS();
1089 	}
1090 
1091 	if (intern->nApplyCount > 0) {
1092 		zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
1093 		RETURN_THROWS();
1094 	}
1095 
1096 	RETVAL_ARR(zend_array_dup(spl_array_get_hash_table(intern)));
1097 	spl_array_set_array(object, intern, array, 0L, 1);
1098 }
1099 /* }}} */
1100 
1101 /* {{{ Create a new iterator from a ArrayObject instance */
PHP_METHOD(ArrayObject,getIterator)1102 PHP_METHOD(ArrayObject, getIterator)
1103 {
1104 	zval *object = ZEND_THIS;
1105 	spl_array_object *intern = Z_SPLARRAY_P(object);
1106 
1107 	if (zend_parse_parameters_none() == FAILURE) {
1108 		RETURN_THROWS();
1109 	}
1110 
1111 	RETURN_OBJ(spl_array_object_new_ex(intern->ce_get_iterator, Z_OBJ_P(object), 0));
1112 }
1113 /* }}} */
1114 
spl_array_object_count_elements_helper(spl_array_object * intern)1115 static zend_long spl_array_object_count_elements_helper(spl_array_object *intern) /* {{{ */
1116 {
1117 	HashTable *aht = spl_array_get_hash_table(intern);
1118 	if (spl_array_is_object(intern)) {
1119 		zend_long count = 0;
1120 		zend_string *key;
1121 		zval *val;
1122 		/* Count public/dynamic properties */
1123 		ZEND_HASH_FOREACH_STR_KEY_VAL(aht, key, val) {
1124 			if (Z_TYPE_P(val) == IS_INDIRECT) {
1125 				if (Z_TYPE_P(Z_INDIRECT_P(val)) == IS_UNDEF) continue;
1126 				if (key && ZSTR_VAL(key)[0] == '\0') continue;
1127 			}
1128 			count++;
1129 		} ZEND_HASH_FOREACH_END();
1130 		return count;
1131 	} else {
1132 		return zend_hash_num_elements(aht);
1133 	}
1134 } /* }}} */
1135 
spl_array_object_count_elements(zend_object * object,zend_long * count)1136 static zend_result spl_array_object_count_elements(zend_object *object, zend_long *count) /* {{{ */
1137 {
1138 	spl_array_object *intern = spl_array_from_obj(object);
1139 
1140 	if (intern->fptr_count) {
1141 		zval rv;
1142 		zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
1143 		if (Z_TYPE(rv) != IS_UNDEF) {
1144 			*count = zval_get_long(&rv);
1145 			zval_ptr_dtor(&rv);
1146 			return SUCCESS;
1147 		}
1148 		*count = 0;
1149 		return FAILURE;
1150 	}
1151 	*count = spl_array_object_count_elements_helper(intern);
1152 	return SUCCESS;
1153 } /* }}} */
1154 
1155 /* {{{ Return the number of elements in the Iterator. */
PHP_METHOD(ArrayObject,count)1156 PHP_METHOD(ArrayObject, count)
1157 {
1158 	spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1159 
1160 	if (zend_parse_parameters_none() == FAILURE) {
1161 		RETURN_THROWS();
1162 	}
1163 
1164 	RETURN_LONG(spl_array_object_count_elements_helper(intern));
1165 } /* }}} */
1166 
spl_array_method(INTERNAL_FUNCTION_PARAMETERS,char * fname,size_t fname_len,int use_arg)1167 static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, size_t fname_len, int use_arg) /* {{{ */
1168 {
1169 	spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1170 	HashTable **ht_ptr = spl_array_get_hash_table_ptr(intern);
1171 	HashTable *aht = *ht_ptr;
1172 	zval function_name, params[2], *arg = NULL;
1173 
1174 	ZVAL_STRINGL(&function_name, fname, fname_len);
1175 
1176 	ZVAL_NEW_EMPTY_REF(&params[0]);
1177 	ZVAL_ARR(Z_REFVAL(params[0]), aht);
1178 	GC_ADDREF(aht);
1179 
1180 	if (!use_arg) {
1181 		if (zend_parse_parameters_none() == FAILURE) {
1182 			goto exit;
1183 		}
1184 
1185 		intern->nApplyCount++;
1186 		call_user_function(EG(function_table), NULL, &function_name, return_value, 1, params);
1187 		intern->nApplyCount--;
1188 	} else if (use_arg == SPL_ARRAY_METHOD_SORT_FLAGS_ARG) {
1189 		zend_long sort_flags = 0;
1190 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &sort_flags) == FAILURE) {
1191 			goto exit;
1192 		}
1193 		ZVAL_LONG(&params[1], sort_flags);
1194 		intern->nApplyCount++;
1195 		call_user_function(EG(function_table), NULL, &function_name, return_value, 2, params);
1196 		intern->nApplyCount--;
1197 	} else {
1198 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
1199 			goto exit;
1200 		}
1201 		ZVAL_COPY_VALUE(&params[1], arg);
1202 		intern->nApplyCount++;
1203 		call_user_function(EG(function_table), NULL, &function_name, return_value, 2, params);
1204 		intern->nApplyCount--;
1205 	}
1206 
1207 exit:
1208 	{
1209 		zval *ht_zv = Z_REFVAL(params[0]);
1210 		zend_array_release(*ht_ptr);
1211 		SEPARATE_ARRAY(ht_zv);
1212 		*ht_ptr = Z_ARRVAL_P(ht_zv);
1213 		ZVAL_NULL(ht_zv);
1214 		zval_ptr_dtor(&params[0]);
1215 		zend_string_free(Z_STR(function_name));
1216 	}
1217 } /* }}} */
1218 
1219 #define SPL_ARRAY_METHOD(cname, fname, use_arg) \
1220 PHP_METHOD(cname, fname) \
1221 { \
1222 	spl_array_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, #fname, sizeof(#fname)-1, use_arg); \
1223 }
1224 
1225 /* {{{ Sort the entries by values. */
SPL_ARRAY_METHOD(ArrayObject,asort,SPL_ARRAY_METHOD_SORT_FLAGS_ARG)1226 SPL_ARRAY_METHOD(ArrayObject, asort, SPL_ARRAY_METHOD_SORT_FLAGS_ARG) /* }}} */
1227 
1228 /* {{{ Sort the entries by key. */
1229 SPL_ARRAY_METHOD(ArrayObject, ksort, SPL_ARRAY_METHOD_SORT_FLAGS_ARG) /* }}} */
1230 
1231 /* {{{ Sort the entries by values user defined function. */
1232 SPL_ARRAY_METHOD(ArrayObject, uasort, SPL_ARRAY_METHOD_CALLBACK_ARG) /* }}} */
1233 
1234 /* {{{ Sort the entries by key using user defined function. */
1235 SPL_ARRAY_METHOD(ArrayObject, uksort, SPL_ARRAY_METHOD_CALLBACK_ARG) /* }}} */
1236 
1237 /* {{{ Sort the entries by values using "natural order" algorithm. */
1238 SPL_ARRAY_METHOD(ArrayObject, natsort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1239 
1240 /* {{{ Sort the entries by key using case insensitive "natural order" algorithm. */
1241 SPL_ARRAY_METHOD(ArrayObject, natcasesort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1242 
1243 /* {{{ Serialize the object */
1244 PHP_METHOD(ArrayObject, serialize)
1245 {
1246 	zval *object = ZEND_THIS;
1247 	spl_array_object *intern = Z_SPLARRAY_P(object);
1248 	zval members, flags;
1249 	php_serialize_data_t var_hash;
1250 	smart_str buf = {0};
1251 
1252 	if (zend_parse_parameters_none() == FAILURE) {
1253 		RETURN_THROWS();
1254 	}
1255 
1256 	PHP_VAR_SERIALIZE_INIT(var_hash);
1257 
1258 	ZVAL_LONG(&flags, (intern->ar_flags & SPL_ARRAY_CLONE_MASK));
1259 
1260 	/* storage */
1261 	smart_str_appendl(&buf, "x:", 2);
1262 	php_var_serialize(&buf, &flags, &var_hash);
1263 
1264 	if (!(intern->ar_flags & SPL_ARRAY_IS_SELF)) {
1265 		php_var_serialize(&buf, &intern->array, &var_hash);
1266 		smart_str_appendc(&buf, ';');
1267 	}
1268 
1269 	/* members */
1270 	smart_str_appendl(&buf, "m:", 2);
1271 
1272 	ZVAL_ARR(&members, zend_std_get_properties_ex(&intern->std));
1273 
1274 	php_var_serialize(&buf, &members, &var_hash); /* finishes the string */
1275 
1276 	/* done */
1277 	PHP_VAR_SERIALIZE_DESTROY(var_hash);
1278 
1279 	RETURN_STR(smart_str_extract(&buf));
1280 } /* }}} */
1281 
1282 /* {{{ unserialize the object */
PHP_METHOD(ArrayObject,unserialize)1283 PHP_METHOD(ArrayObject, unserialize)
1284 {
1285 	zval *object = ZEND_THIS;
1286 	spl_array_object *intern = Z_SPLARRAY_P(object);
1287 
1288 	char *buf;
1289 	size_t buf_len;
1290 	const unsigned char *p, *s;
1291 	php_unserialize_data_t var_hash;
1292 	zval *members, *zflags, *array;
1293 	zend_long flags;
1294 
1295 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
1296 		RETURN_THROWS();
1297 	}
1298 
1299 	if (buf_len == 0) {
1300 		return;
1301 	}
1302 
1303 	if (intern->nApplyCount > 0) {
1304 		zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
1305 		RETURN_THROWS();
1306 	}
1307 
1308 	/* storage */
1309 	s = p = (const unsigned char*)buf;
1310 	PHP_VAR_UNSERIALIZE_INIT(var_hash);
1311 
1312 	if (*p!= 'x' || *++p != ':') {
1313 		goto outexcept;
1314 	}
1315 	++p;
1316 
1317 	zflags = var_tmp_var(&var_hash);
1318 	if (!php_var_unserialize(zflags, &p, s + buf_len, &var_hash) || Z_TYPE_P(zflags) != IS_LONG) {
1319 		goto outexcept;
1320 	}
1321 
1322 	--p; /* for ';' */
1323 	flags = Z_LVAL_P(zflags);
1324 	/* flags needs to be verified and we also need to verify whether the next
1325 	 * thing we get is ';'. After that we require an 'm' or something else
1326 	 * where 'm' stands for members and anything else should be an array. If
1327 	 * neither 'a' or 'm' follows we have an error. */
1328 
1329 	if (*p != ';') {
1330 		goto outexcept;
1331 	}
1332 	++p;
1333 
1334 	if (flags & SPL_ARRAY_IS_SELF) {
1335 		/* If IS_SELF is used, the flags are not followed by an array/object */
1336 		intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1337 		intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1338 		zval_ptr_dtor(&intern->array);
1339 		ZVAL_UNDEF(&intern->array);
1340 	} else {
1341 		if (*p!='a' && *p!='O' && *p!='C' && *p!='r') {
1342 			goto outexcept;
1343 		}
1344 
1345 		array = var_tmp_var(&var_hash);
1346 		if (!php_var_unserialize(array, &p, s + buf_len, &var_hash)
1347 				|| (Z_TYPE_P(array) != IS_ARRAY && Z_TYPE_P(array) != IS_OBJECT)) {
1348 			goto outexcept;
1349 		}
1350 
1351 		intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1352 		intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1353 
1354 		if (Z_TYPE_P(array) == IS_ARRAY) {
1355 			zval_ptr_dtor(&intern->array);
1356 			ZVAL_COPY_VALUE(&intern->array, array);
1357 			ZVAL_NULL(array);
1358 			SEPARATE_ARRAY(&intern->array);
1359 		} else {
1360 			spl_array_set_array(object, intern, array, 0L, 1);
1361 		}
1362 
1363 		if (*p != ';') {
1364 			goto outexcept;
1365 		}
1366 		++p;
1367 	}
1368 
1369 	/* members */
1370 	if (*p!= 'm' || *++p != ':') {
1371 		goto outexcept;
1372 	}
1373 	++p;
1374 
1375 	members = var_tmp_var(&var_hash);
1376 	if (!php_var_unserialize(members, &p, s + buf_len, &var_hash) || Z_TYPE_P(members) != IS_ARRAY) {
1377 		goto outexcept;
1378 	}
1379 
1380 	/* copy members */
1381 	object_properties_load(&intern->std, Z_ARRVAL_P(members));
1382 
1383 	/* done reading $serialized */
1384 	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1385 	return;
1386 
1387 outexcept:
1388 	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1389 	zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset " ZEND_LONG_FMT " of %zd bytes", (zend_long)((char*)p - buf), buf_len);
1390 	RETURN_THROWS();
1391 
1392 } /* }}} */
1393 
1394 /* {{{ */
PHP_METHOD(ArrayObject,__serialize)1395 PHP_METHOD(ArrayObject, __serialize)
1396 {
1397 	spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1398 	zval tmp;
1399 
1400 	if (zend_parse_parameters_none() == FAILURE) {
1401 		RETURN_THROWS();
1402 	}
1403 
1404 	array_init(return_value);
1405 
1406 	/* flags */
1407 	ZVAL_LONG(&tmp, (intern->ar_flags & SPL_ARRAY_CLONE_MASK));
1408 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1409 
1410 	/* storage */
1411 	if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
1412 		ZVAL_NULL(&tmp);
1413 	} else {
1414 		ZVAL_COPY(&tmp, &intern->array);
1415 	}
1416 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1417 
1418 	/* members */
1419 	ZVAL_ARR(&tmp, zend_proptable_to_symtable(
1420 		zend_std_get_properties(&intern->std), /* always_duplicate */ 1));
1421 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1422 
1423 	/* iterator class */
1424 	if (intern->ce_get_iterator == spl_ce_ArrayIterator) {
1425 		ZVAL_NULL(&tmp);
1426 	} else {
1427 		ZVAL_STR_COPY(&tmp, intern->ce_get_iterator->name);
1428 	}
1429 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1430 }
1431 /* }}} */
1432 
1433 
1434 /* {{{ */
PHP_METHOD(ArrayObject,__unserialize)1435 PHP_METHOD(ArrayObject, __unserialize)
1436 {
1437 	spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1438 	HashTable *data;
1439 	zval *flags_zv, *storage_zv, *members_zv, *iterator_class_zv;
1440 	zend_long flags;
1441 
1442 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
1443 		RETURN_THROWS();
1444 	}
1445 
1446 	flags_zv          = zend_hash_index_find(data, 0);
1447 	storage_zv        = zend_hash_index_find(data, 1);
1448 	members_zv        = zend_hash_index_find(data, 2);
1449 	iterator_class_zv = zend_hash_index_find(data, 3);
1450 
1451 	if (!flags_zv || !storage_zv || !members_zv ||
1452 			Z_TYPE_P(flags_zv) != IS_LONG || Z_TYPE_P(members_zv) != IS_ARRAY ||
1453 			(iterator_class_zv && (Z_TYPE_P(iterator_class_zv) != IS_NULL &&
1454 				Z_TYPE_P(iterator_class_zv) != IS_STRING))) {
1455 		zend_throw_exception(spl_ce_UnexpectedValueException,
1456 			"Incomplete or ill-typed serialization data", 0);
1457 		RETURN_THROWS();
1458 	}
1459 
1460 	flags = Z_LVAL_P(flags_zv);
1461 	intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1462 	intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1463 
1464 	if (flags & SPL_ARRAY_IS_SELF) {
1465 		zval_ptr_dtor(&intern->array);
1466 		ZVAL_UNDEF(&intern->array);
1467 	} else {
1468 		if (Z_TYPE_P(storage_zv) != IS_OBJECT && Z_TYPE_P(storage_zv) != IS_ARRAY) {
1469 			/* TODO Use UnexpectedValueException instead? And better error message? */
1470 			zend_throw_exception(spl_ce_InvalidArgumentException, "Passed variable is not an array or object", 0);
1471 			RETURN_THROWS();
1472 		}
1473 		spl_array_set_array(ZEND_THIS, intern, storage_zv, 0L, 1);
1474 	}
1475 
1476 	object_properties_load(&intern->std, Z_ARRVAL_P(members_zv));
1477 	if (EG(exception)) {
1478 		RETURN_THROWS();
1479 	}
1480 
1481 	if (iterator_class_zv && Z_TYPE_P(iterator_class_zv) == IS_STRING) {
1482 		zend_class_entry *ce = zend_lookup_class(Z_STR_P(iterator_class_zv));
1483 
1484 		if (!ce) {
1485 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
1486 				"Cannot deserialize ArrayObject with iterator class '%s'; no such class exists",
1487 				ZSTR_VAL(Z_STR_P(iterator_class_zv)));
1488 			RETURN_THROWS();
1489 		}
1490 
1491 		if (!instanceof_function(ce, zend_ce_iterator)) {
1492 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
1493 				"Cannot deserialize ArrayObject with iterator class '%s'; this class does not implement the Iterator interface",
1494 				ZSTR_VAL(Z_STR_P(iterator_class_zv)));
1495 			RETURN_THROWS();
1496 		}
1497 
1498 		intern->ce_get_iterator = ce;
1499 	}
1500 }
1501 /* }}} */
1502 
1503 /* {{{ */
PHP_METHOD(ArrayObject,__debugInfo)1504 PHP_METHOD(ArrayObject, __debugInfo)
1505 {
1506 	if (zend_parse_parameters_none() == FAILURE) {
1507 		RETURN_THROWS();
1508 	}
1509 
1510 	RETURN_ARR(spl_array_get_debug_info(Z_OBJ_P(ZEND_THIS)));
1511 } /* }}} */
1512 
1513 /*** ArrayIterator class ***/
1514 typedef struct _spl_array_iterator {
1515 	zend_object_iterator it;
1516 	bool by_ref;
1517 } spl_array_iterator;
1518 
spl_array_next_ex(spl_array_object * intern,HashTable * aht)1519 static zend_result spl_array_next_ex(spl_array_object *intern, HashTable *aht) /* {{{ */
1520 {
1521 	uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
1522 
1523 	zend_hash_move_forward_ex(aht, pos_ptr);
1524 	if (spl_array_is_object(intern)) {
1525 		return spl_array_skip_protected(intern, aht);
1526 	} else {
1527 		return zend_hash_has_more_elements_ex(aht, pos_ptr);
1528 	}
1529 } /* }}} */
1530 
spl_array_next(spl_array_object * intern)1531 static zend_result spl_array_next(spl_array_object *intern) /* {{{ */
1532 {
1533 	HashTable *aht = spl_array_get_hash_table(intern);
1534 
1535 	return spl_array_next_ex(intern, aht);
1536 
1537 } /* }}} */
1538 
spl_array_it_dtor(zend_object_iterator * iter)1539 static void spl_array_it_dtor(zend_object_iterator *iter) /* {{{ */
1540 {
1541 	zval_ptr_dtor(&iter->data);
1542 }
1543 /* }}} */
1544 
spl_array_it_valid(zend_object_iterator * iter)1545 static zend_result spl_array_it_valid(zend_object_iterator *iter) /* {{{ */
1546 {
1547 	spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1548 	HashTable *aht = spl_array_get_hash_table(object);
1549 	return zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, object));
1550 }
1551 /* }}} */
1552 
spl_array_it_get_current_data(zend_object_iterator * iter)1553 static zval *spl_array_it_get_current_data(zend_object_iterator *iter) /* {{{ */
1554 {
1555 	spl_array_iterator *array_iter = (spl_array_iterator*)iter;
1556 	spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1557 	HashTable *aht = spl_array_get_hash_table(object);
1558 	zval *data = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, object));
1559 	if (data && Z_TYPE_P(data) == IS_INDIRECT) {
1560 		data = Z_INDIRECT_P(data);
1561 	}
1562 	// ZEND_FE_FETCH_RW converts the value to a reference but doesn't know the source is a property.
1563 	// Typed properties must add a type source to the reference, and readonly properties must fail.
1564 	if (array_iter->by_ref
1565 	 && Z_TYPE_P(data) != IS_REFERENCE
1566 	 && Z_TYPE(object->array) == IS_OBJECT
1567 	 && !(object->ar_flags & (SPL_ARRAY_IS_SELF|SPL_ARRAY_USE_OTHER))) {
1568 		zend_string *key;
1569 		zend_hash_get_current_key_ex(aht, &key, NULL, spl_array_get_pos_ptr(aht, object));
1570 		zend_class_entry *ce = Z_OBJCE(object->array);
1571 		zend_property_info *prop_info = zend_get_property_info(ce, key, true);
1572 		ZEND_ASSERT(prop_info != ZEND_WRONG_PROPERTY_INFO);
1573 		if (EXPECTED(prop_info != NULL) && ZEND_TYPE_IS_SET(prop_info->type)) {
1574 			if (prop_info->flags & ZEND_ACC_READONLY) {
1575 				zend_throw_error(NULL,
1576 					"Cannot acquire reference to readonly property %s::$%s",
1577 					ZSTR_VAL(prop_info->ce->name), ZSTR_VAL(key));
1578 				return NULL;
1579 			}
1580 			ZVAL_NEW_REF(data, data);
1581 			ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(data), prop_info);
1582 		}
1583 	}
1584 	return data;
1585 }
1586 /* }}} */
1587 
spl_array_it_get_current_key(zend_object_iterator * iter,zval * key)1588 static void spl_array_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
1589 {
1590 	spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1591 	HashTable *aht = spl_array_get_hash_table(object);
1592 	zend_hash_get_current_key_zval_ex(aht, key, spl_array_get_pos_ptr(aht, object));
1593 }
1594 /* }}} */
1595 
spl_array_it_move_forward(zend_object_iterator * iter)1596 static void spl_array_it_move_forward(zend_object_iterator *iter) /* {{{ */
1597 {
1598 	spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1599 	HashTable *aht = spl_array_get_hash_table(object);
1600 	spl_array_next_ex(object, aht);
1601 }
1602 /* }}} */
1603 
spl_array_rewind(spl_array_object * intern)1604 static void spl_array_rewind(spl_array_object *intern) /* {{{ */
1605 {
1606 	HashTable *aht = spl_array_get_hash_table(intern);
1607 
1608 	if (intern->ht_iter == (uint32_t)-1) {
1609 		spl_array_get_pos_ptr(aht, intern);
1610 	} else {
1611 		zend_hash_internal_pointer_reset_ex(aht, spl_array_get_pos_ptr(aht, intern));
1612 		spl_array_skip_protected(intern, aht);
1613 	}
1614 }
1615 /* }}} */
1616 
spl_array_it_rewind(zend_object_iterator * iter)1617 static void spl_array_it_rewind(zend_object_iterator *iter) /* {{{ */
1618 {
1619 	spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1620 	spl_array_rewind(object);
1621 }
1622 /* }}} */
1623 
spl_array_it_get_gc(zend_object_iterator * iter,zval ** table,int * n)1624 static HashTable *spl_array_it_get_gc(zend_object_iterator *iter, zval **table, int *n)
1625 {
1626 	*n = 1;
1627 	*table = &iter->data;
1628 	return NULL;
1629 }
1630 
1631 /* iterator handler table */
1632 static const zend_object_iterator_funcs spl_array_it_funcs = {
1633 	spl_array_it_dtor,
1634 	spl_array_it_valid,
1635 	spl_array_it_get_current_data,
1636 	spl_array_it_get_current_key,
1637 	spl_array_it_move_forward,
1638 	spl_array_it_rewind,
1639 	NULL,
1640 	spl_array_it_get_gc,
1641 };
1642 
spl_array_get_iterator(zend_class_entry * ce,zval * object,int by_ref)1643 static zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1644 {
1645 	spl_array_iterator *iterator = emalloc(sizeof(spl_array_iterator));
1646 	zend_iterator_init(&iterator->it);
1647 
1648 	ZVAL_OBJ_COPY(&iterator->it.data, Z_OBJ_P(object));
1649 	iterator->it.funcs = &spl_array_it_funcs;
1650 	iterator->by_ref = by_ref;
1651 
1652 	return &iterator->it;
1653 }
1654 /* }}} */
1655 
1656 /* {{{ Constructs a new array iterator from an array or object. */
PHP_METHOD(ArrayIterator,__construct)1657 PHP_METHOD(ArrayIterator, __construct)
1658 {
1659 	zval *object = ZEND_THIS;
1660 	spl_array_object *intern;
1661 	zval *array;
1662 	zend_long ar_flags = 0;
1663 
1664 	if (ZEND_NUM_ARGS() == 0) {
1665 		return; /* nothing to do */
1666 	}
1667 
1668 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|Al", &array, &ar_flags) == FAILURE) {
1669 		RETURN_THROWS();
1670 	}
1671 
1672 	intern = Z_SPLARRAY_P(object);
1673 
1674 	ar_flags &= ~SPL_ARRAY_INT_MASK;
1675 
1676 	spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
1677 }
1678 /* }}} */
1679 
1680 /* {{{ Rewind array back to the start */
PHP_METHOD(ArrayIterator,rewind)1681 PHP_METHOD(ArrayIterator, rewind)
1682 {
1683 	zval *object = ZEND_THIS;
1684 	spl_array_object *intern = Z_SPLARRAY_P(object);
1685 
1686 	if (zend_parse_parameters_none() == FAILURE) {
1687 		RETURN_THROWS();
1688 	}
1689 
1690 	spl_array_rewind(intern);
1691 }
1692 /* }}} */
1693 
1694 /* {{{ Seek to position. */
PHP_METHOD(ArrayIterator,seek)1695 PHP_METHOD(ArrayIterator, seek)
1696 {
1697 	zend_long opos, position;
1698 	zval *object = ZEND_THIS;
1699 	spl_array_object *intern = Z_SPLARRAY_P(object);
1700 	HashTable *aht = spl_array_get_hash_table(intern);
1701 	int result;
1702 
1703 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &position) == FAILURE) {
1704 		RETURN_THROWS();
1705 	}
1706 
1707 	opos = position;
1708 
1709 	if (position >= 0) { /* negative values are not supported */
1710 		spl_array_rewind(intern);
1711 		result = SUCCESS;
1712 
1713 		while (position-- > 0 && (result = spl_array_next(intern)) == SUCCESS);
1714 
1715 		if (result == SUCCESS && zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS) {
1716 			return; /* ok */
1717 		}
1718 	}
1719 	zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", opos);
1720 } /* }}} */
1721 
1722 /* {{{ Return current array entry */
PHP_METHOD(ArrayIterator,current)1723 PHP_METHOD(ArrayIterator, current)
1724 {
1725 	zval *object = ZEND_THIS;
1726 	spl_array_object *intern = Z_SPLARRAY_P(object);
1727 	zval *entry;
1728 	HashTable *aht = spl_array_get_hash_table(intern);
1729 
1730 	if (zend_parse_parameters_none() == FAILURE) {
1731 		RETURN_THROWS();
1732 	}
1733 
1734 	if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1735 		RETURN_NULL();
1736 	}
1737 	if (Z_TYPE_P(entry) == IS_INDIRECT) {
1738 		entry = Z_INDIRECT_P(entry);
1739 		if (Z_TYPE_P(entry) == IS_UNDEF) {
1740 			RETURN_NULL();
1741 		}
1742 	}
1743 	RETURN_COPY_DEREF(entry);
1744 }
1745 /* }}} */
1746 
spl_array_iterator_key(zval * object,zval * return_value)1747 void spl_array_iterator_key(zval *object, zval *return_value) /* {{{ */
1748 {
1749 	spl_array_object *intern = Z_SPLARRAY_P(object);
1750 	HashTable *aht = spl_array_get_hash_table(intern);
1751 
1752 	zend_hash_get_current_key_zval_ex(aht, return_value, spl_array_get_pos_ptr(aht, intern));
1753 }
1754 /* }}} */
1755 
1756 /* {{{ Return current array key */
PHP_METHOD(ArrayIterator,key)1757 PHP_METHOD(ArrayIterator, key)
1758 {
1759 	if (zend_parse_parameters_none() == FAILURE) {
1760 		RETURN_THROWS();
1761 	}
1762 
1763 	spl_array_iterator_key(ZEND_THIS, return_value);
1764 } /* }}} */
1765 
1766 /* {{{ Move to next entry */
PHP_METHOD(ArrayIterator,next)1767 PHP_METHOD(ArrayIterator, next)
1768 {
1769 	zval *object = ZEND_THIS;
1770 	spl_array_object *intern = Z_SPLARRAY_P(object);
1771 	HashTable *aht = spl_array_get_hash_table(intern);
1772 
1773 	if (zend_parse_parameters_none() == FAILURE) {
1774 		RETURN_THROWS();
1775 	}
1776 
1777 	spl_array_next_ex(intern, aht);
1778 }
1779 /* }}} */
1780 
1781 /* {{{ Check whether array contains more entries */
PHP_METHOD(ArrayIterator,valid)1782 PHP_METHOD(ArrayIterator, valid)
1783 {
1784 	zval *object = ZEND_THIS;
1785 	spl_array_object *intern = Z_SPLARRAY_P(object);
1786 	HashTable *aht = spl_array_get_hash_table(intern);
1787 
1788 	if (zend_parse_parameters_none() == FAILURE) {
1789 		RETURN_THROWS();
1790 	}
1791 
1792 	RETURN_BOOL(zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS);
1793 }
1794 /* }}} */
1795 
1796 /*** RecursiveArrayIterator methods ***/
1797 
1798 /* {{{ Check whether current element has children (e.g. is an array) */
PHP_METHOD(RecursiveArrayIterator,hasChildren)1799 PHP_METHOD(RecursiveArrayIterator, hasChildren)
1800 {
1801 	zval *object = ZEND_THIS, *entry;
1802 	spl_array_object *intern = Z_SPLARRAY_P(object);
1803 	HashTable *aht = spl_array_get_hash_table(intern);
1804 
1805 	if (zend_parse_parameters_none() == FAILURE) {
1806 		RETURN_THROWS();
1807 	}
1808 
1809 	if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1810 		RETURN_FALSE;
1811 	}
1812 
1813 	if (Z_TYPE_P(entry) == IS_INDIRECT) {
1814 		entry = Z_INDIRECT_P(entry);
1815 	}
1816 
1817 	ZVAL_DEREF(entry);
1818 	RETURN_BOOL(Z_TYPE_P(entry) == IS_ARRAY || (Z_TYPE_P(entry) == IS_OBJECT && (intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) == 0));
1819 }
1820 /* }}} */
1821 
spl_instantiate_child_arg(zend_class_entry * pce,zval * retval,zval * arg1,zval * arg2)1822 static void spl_instantiate_child_arg(zend_class_entry *pce, zval *retval, zval *arg1, zval *arg2) /* {{{ */
1823 {
1824 	object_init_ex(retval, pce);
1825 	spl_array_object *new_intern = Z_SPLARRAY_P(retval);
1826 	/*
1827 	 * set new_intern->is_child is true to indicate that the object was created by
1828 	 * RecursiveArrayIterator::getChildren() method.
1829 	 */
1830 	new_intern->is_child = true;
1831 
1832 	/* find the bucket of parent object. */
1833 	new_intern->bucket = (Bucket *)((char *)(arg1) - XtOffsetOf(Bucket, val));;
1834 	zend_call_known_instance_method_with_2_params(pce->constructor, Z_OBJ_P(retval), NULL, arg1, arg2);
1835 }
1836 /* }}} */
1837 
1838 /* {{{ Create a sub iterator for the current element (same class as $this) */
PHP_METHOD(RecursiveArrayIterator,getChildren)1839 PHP_METHOD(RecursiveArrayIterator, getChildren)
1840 {
1841 	zval *object = ZEND_THIS, *entry, flags;
1842 	spl_array_object *intern = Z_SPLARRAY_P(object);
1843 	HashTable *aht = spl_array_get_hash_table(intern);
1844 
1845 	if (zend_parse_parameters_none() == FAILURE) {
1846 		RETURN_THROWS();
1847 	}
1848 
1849 	if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1850 		RETURN_NULL();
1851 	}
1852 
1853 	if (Z_TYPE_P(entry) == IS_INDIRECT) {
1854 		entry = Z_INDIRECT_P(entry);
1855 	}
1856 
1857 	ZVAL_DEREF(entry);
1858 	if (Z_TYPE_P(entry) == IS_OBJECT) {
1859 		if ((intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) != 0) {
1860 			RETURN_NULL();
1861 		}
1862 		if (instanceof_function(Z_OBJCE_P(entry), Z_OBJCE_P(ZEND_THIS))) {
1863 			RETURN_OBJ_COPY(Z_OBJ_P(entry));
1864 		}
1865 	}
1866 
1867 	ZVAL_LONG(&flags, intern->ar_flags);
1868 	spl_instantiate_child_arg(Z_OBJCE_P(ZEND_THIS), return_value, entry, &flags);
1869 }
1870 /* }}} */
1871 
1872 /* {{{ PHP_MINIT_FUNCTION(spl_array) */
PHP_MINIT_FUNCTION(spl_array)1873 PHP_MINIT_FUNCTION(spl_array)
1874 {
1875 	spl_ce_ArrayObject = register_class_ArrayObject(zend_ce_aggregate, zend_ce_arrayaccess, zend_ce_serializable, zend_ce_countable);
1876 	spl_ce_ArrayObject->create_object = spl_array_object_new;
1877 	spl_ce_ArrayObject->default_object_handlers = &spl_handler_ArrayObject;
1878 
1879 	memcpy(&spl_handler_ArrayObject, &std_object_handlers, sizeof(zend_object_handlers));
1880 
1881 	spl_handler_ArrayObject.offset = XtOffsetOf(spl_array_object, std);
1882 
1883 	spl_handler_ArrayObject.clone_obj = spl_array_object_clone;
1884 	spl_handler_ArrayObject.read_dimension = spl_array_read_dimension;
1885 	spl_handler_ArrayObject.write_dimension = spl_array_write_dimension;
1886 	spl_handler_ArrayObject.unset_dimension = spl_array_unset_dimension;
1887 	spl_handler_ArrayObject.has_dimension = spl_array_has_dimension;
1888 	spl_handler_ArrayObject.count_elements = spl_array_object_count_elements;
1889 
1890 	spl_handler_ArrayObject.get_properties_for = spl_array_get_properties_for;
1891 	spl_handler_ArrayObject.get_gc = spl_array_get_gc;
1892 	spl_handler_ArrayObject.read_property = spl_array_read_property;
1893 	spl_handler_ArrayObject.write_property = spl_array_write_property;
1894 	spl_handler_ArrayObject.get_property_ptr_ptr = spl_array_get_property_ptr_ptr;
1895 	spl_handler_ArrayObject.has_property = spl_array_has_property;
1896 	spl_handler_ArrayObject.unset_property = spl_array_unset_property;
1897 
1898 	spl_handler_ArrayObject.compare = spl_array_compare_objects;
1899 	spl_handler_ArrayObject.free_obj = spl_array_object_free_storage;
1900 
1901 	spl_ce_ArrayIterator = register_class_ArrayIterator(spl_ce_SeekableIterator, zend_ce_arrayaccess, zend_ce_serializable, zend_ce_countable);
1902 	spl_ce_ArrayIterator->create_object = spl_array_object_new;
1903 	spl_ce_ArrayIterator->default_object_handlers = &spl_handler_ArrayObject;
1904 	spl_ce_ArrayIterator->get_iterator = spl_array_get_iterator;
1905 
1906 	spl_ce_RecursiveArrayIterator = register_class_RecursiveArrayIterator(spl_ce_ArrayIterator, spl_ce_RecursiveIterator);
1907 	spl_ce_RecursiveArrayIterator->create_object = spl_array_object_new;
1908 	spl_ce_RecursiveArrayIterator->get_iterator = spl_array_get_iterator;
1909 
1910 	return SUCCESS;
1911 }
1912 /* }}} */
1913