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