xref: /php-src/ext/spl/spl_fixedarray.c (revision c8b45aa5)
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   | Author: Antony Dovgal <tony@daylessday.org>                          |
14   |         Etienne Kneuss <colder@php.net>                              |
15   +----------------------------------------------------------------------+
16 */
17 
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21 
22 #include "php.h"
23 #include "zend_interfaces.h"
24 #include "zend_exceptions.h"
25 #include "zend_attributes.h"
26 
27 #include "spl_fixedarray_arginfo.h"
28 #include "spl_fixedarray.h"
29 #include "spl_exceptions.h"
30 #include "ext/json/php_json.h" /* For php_json_serializable_ce */
31 
32 static zend_object_handlers spl_handler_SplFixedArray;
33 PHPAPI zend_class_entry *spl_ce_SplFixedArray;
34 
35 /* Check if the object is an instance of a subclass of SplFixedArray that overrides method's implementation.
36  * Expect subclassing SplFixedArray to be rare and check that first. */
37 #define HAS_FIXEDARRAY_ARRAYACCESS_OVERRIDE(object, method) UNEXPECTED((object)->ce != spl_ce_SplFixedArray && (object)->ce->arrayaccess_funcs_ptr->method->common.scope != spl_ce_SplFixedArray)
38 
39 typedef struct _spl_fixedarray {
40 	zend_long size;
41 	/* It is possible to resize this, so this can't be combined with the object */
42 	zval *elements;
43 	/* If positive, it's a resize within a resize and the value gives the desired size. If -1, it's not. */
44 	zend_long cached_resize;
45 } spl_fixedarray;
46 
47 typedef struct _spl_fixedarray_object {
48 	spl_fixedarray          array;
49 	zend_function          *fptr_count;
50 	zend_object             std;
51 } spl_fixedarray_object;
52 
53 typedef struct _spl_fixedarray_it {
54 	zend_object_iterator intern;
55 	zend_long            current;
56 } spl_fixedarray_it;
57 
spl_fixed_array_from_obj(zend_object * obj)58 static spl_fixedarray_object *spl_fixed_array_from_obj(zend_object *obj)
59 {
60 	return (spl_fixedarray_object*)((char*)(obj) - XtOffsetOf(spl_fixedarray_object, std));
61 }
62 
63 #define Z_SPLFIXEDARRAY_P(zv)  spl_fixed_array_from_obj(Z_OBJ_P((zv)))
64 
65 /* Helps enforce the invariants in debug mode:
66  *   - if size == 0, then elements == NULL
67  *   - if size > 0, then elements != NULL
68  *   - size is not less than 0
69  */
spl_fixedarray_empty(spl_fixedarray * array)70 static bool spl_fixedarray_empty(spl_fixedarray *array)
71 {
72 	if (array->elements) {
73 		ZEND_ASSERT(array->size > 0);
74 		return false;
75 	}
76 	ZEND_ASSERT(array->size == 0);
77 	return true;
78 }
79 
spl_fixedarray_default_ctor(spl_fixedarray * array)80 static void spl_fixedarray_default_ctor(spl_fixedarray *array)
81 {
82 	array->size = 0;
83 	array->elements = NULL;
84 	array->cached_resize = -1;
85 }
86 
87 /* Initializes the range [from, to) to null. Does not dtor existing elements. */
spl_fixedarray_init_elems(spl_fixedarray * array,zend_long from,zend_long to)88 static void spl_fixedarray_init_elems(spl_fixedarray *array, zend_long from, zend_long to)
89 {
90 	ZEND_ASSERT(from <= to);
91 	zval *begin = array->elements + from, *end = array->elements + to;
92 
93 	while (begin != end) {
94 		ZVAL_NULL(begin++);
95 	}
96 }
97 
spl_fixedarray_init_non_empty_struct(spl_fixedarray * array,zend_long size)98 static void spl_fixedarray_init_non_empty_struct(spl_fixedarray *array, zend_long size)
99 {
100 	array->size = 0; /* reset size in case ecalloc() fails */
101 	array->elements = size ? safe_emalloc(size, sizeof(zval), 0) : NULL;
102 	array->size = size;
103 	array->cached_resize = -1;
104 }
105 
spl_fixedarray_init(spl_fixedarray * array,zend_long size)106 static void spl_fixedarray_init(spl_fixedarray *array, zend_long size)
107 {
108 	if (size > 0) {
109 		spl_fixedarray_init_non_empty_struct(array, size);
110 		spl_fixedarray_init_elems(array, 0, size);
111 	} else {
112 		spl_fixedarray_default_ctor(array);
113 	}
114 }
115 
116 /* Copies the range [begin, end) into the fixedarray, beginning at `offset`.
117  * Does not dtor the existing elements.
118  */
spl_fixedarray_copy_range(spl_fixedarray * array,zend_long offset,zval * begin,zval * end)119 static void spl_fixedarray_copy_range(spl_fixedarray *array, zend_long offset, zval *begin, zval *end)
120 {
121 	ZEND_ASSERT(offset >= 0);
122 	ZEND_ASSERT(array->size - offset >= end - begin);
123 
124 	zval *to = &array->elements[offset];
125 	while (begin != end) {
126 		ZVAL_COPY(to++, begin++);
127 	}
128 }
129 
spl_fixedarray_copy_ctor(spl_fixedarray * to,spl_fixedarray * from)130 static void spl_fixedarray_copy_ctor(spl_fixedarray *to, spl_fixedarray *from)
131 {
132 	zend_long size = from->size;
133 	spl_fixedarray_init(to, size);
134 	if (size != 0) {
135 		zval *begin = from->elements, *end = from->elements + size;
136 		spl_fixedarray_copy_range(to, 0, begin, end);
137 	}
138 }
139 
140 /* Destructs the elements in the range [from, to).
141  * Caller is expected to bounds check.
142  */
spl_fixedarray_dtor_range(spl_fixedarray * array,zend_long from,zend_long to)143 static void spl_fixedarray_dtor_range(spl_fixedarray *array, zend_long from, zend_long to)
144 {
145 	array->size = from;
146 	zval *begin = array->elements + from, *end = array->elements + to;
147 	while (begin != end) {
148 		zval_ptr_dtor(begin++);
149 	}
150 }
151 
152 /* Destructs and frees contents but not the array itself.
153  * If you want to re-use the array then you need to re-initialize it.
154  */
spl_fixedarray_dtor(spl_fixedarray * array)155 static void spl_fixedarray_dtor(spl_fixedarray *array)
156 {
157 	if (!spl_fixedarray_empty(array)) {
158 		zval *begin = array->elements, *end = array->elements + array->size;
159 		array->elements = NULL;
160 		array->size = 0;
161 		while (begin != end) {
162 			zval_ptr_dtor(--end);
163 		}
164 		efree(begin);
165 	}
166 }
167 
spl_fixedarray_resize(spl_fixedarray * array,zend_long size)168 static void spl_fixedarray_resize(spl_fixedarray *array, zend_long size)
169 {
170 	if (size == array->size) {
171 		/* nothing to do */
172 		return;
173 	}
174 
175 	/* first initialization */
176 	if (array->size == 0) {
177 		spl_fixedarray_init(array, size);
178 		return;
179 	}
180 
181 	if (UNEXPECTED(array->cached_resize >= 0)) {
182 		/* We're already resizing, so just remember the desired size.
183 		 * The resize will happen later. */
184 		array->cached_resize = size;
185 		return;
186 	}
187 	array->cached_resize = size;
188 
189 	/* clearing the array */
190 	if (size == 0) {
191 		spl_fixedarray_dtor(array);
192 		array->elements = NULL;
193 		array->size = 0;
194 	} else if (size > array->size) {
195 		array->elements = safe_erealloc(array->elements, size, sizeof(zval), 0);
196 		spl_fixedarray_init_elems(array, array->size, size);
197 		array->size = size;
198 	} else { /* size < array->size */
199 		/* Size set in spl_fixedarray_dtor_range() */
200 		spl_fixedarray_dtor_range(array, size, array->size);
201 		array->elements = erealloc(array->elements, sizeof(zval) * size);
202 	}
203 
204 	/* If resized within the destructor, take the last resize command and perform it */
205 	zend_long cached_resize = array->cached_resize;
206 	array->cached_resize = -1;
207 	if (cached_resize != size) {
208 		spl_fixedarray_resize(array, cached_resize);
209 	}
210 }
211 
spl_fixedarray_object_get_gc(zend_object * obj,zval ** table,int * n)212 static HashTable* spl_fixedarray_object_get_gc(zend_object *obj, zval **table, int *n)
213 {
214 	spl_fixedarray_object *intern = spl_fixed_array_from_obj(obj);
215 	HashTable *ht = zend_std_get_properties(obj);
216 
217 	*table = intern->array.elements;
218 	*n = (int)intern->array.size;
219 
220 	return ht;
221 }
222 
spl_fixedarray_object_get_properties_for(zend_object * obj,zend_prop_purpose purpose)223 static HashTable* spl_fixedarray_object_get_properties_for(zend_object *obj, zend_prop_purpose purpose)
224 {
225 	/* This has __serialize, so the purpose is not ZEND_PROP_PURPOSE_SERIALIZE, which would expect a non-null return value */
226 	ZEND_ASSERT(purpose != ZEND_PROP_PURPOSE_SERIALIZE);
227 
228 	const spl_fixedarray_object *intern = spl_fixed_array_from_obj(obj);
229 	/*
230 	 * SplFixedArray can be subclassed or have dynamic properties (With or without AllowDynamicProperties in subclasses).
231 	 * Instances of subclasses with declared properties may have properties but not yet have a property table.
232 	 */
233 	HashTable *source_properties = obj->properties ? obj->properties : (obj->ce->default_properties_count ? zend_std_get_properties(obj) : NULL);
234 
235 	const zend_long size = intern->array.size;
236 	if (size == 0 && (!source_properties || !zend_hash_num_elements(source_properties))) {
237 		return NULL;
238 	}
239 	zval *const elements = intern->array.elements;
240 	HashTable *ht = zend_new_array(size);
241 
242 	for (zend_long i = 0; i < size; i++) {
243 		Z_TRY_ADDREF_P(&elements[i]);
244 		zend_hash_next_index_insert(ht, &elements[i]);
245 	}
246 	if (source_properties && zend_hash_num_elements(source_properties) > 0) {
247 		zend_long nkey;
248 		zend_string *skey;
249 		zval *value;
250 		ZEND_HASH_MAP_FOREACH_KEY_VAL_IND(source_properties, nkey, skey, value) {
251 			Z_TRY_ADDREF_P(value);
252 			if (skey) {
253 				zend_hash_add_new(ht, skey, value);
254 			} else {
255 				zend_hash_index_update(ht, nkey, value);
256 			}
257 		} ZEND_HASH_FOREACH_END();
258 	}
259 
260 	return ht;
261 }
262 
spl_fixedarray_object_free_storage(zend_object * object)263 static void spl_fixedarray_object_free_storage(zend_object *object)
264 {
265 	spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
266 	spl_fixedarray_dtor(&intern->array);
267 	zend_object_std_dtor(&intern->std);
268 }
269 
spl_fixedarray_object_new_ex(zend_class_entry * class_type,zend_object * orig,bool clone_orig)270 static zend_object *spl_fixedarray_object_new_ex(zend_class_entry *class_type, zend_object *orig, bool clone_orig)
271 {
272 	spl_fixedarray_object *intern;
273 	zend_class_entry      *parent = class_type;
274 	bool                   inherited = false;
275 
276 	intern = zend_object_alloc(sizeof(spl_fixedarray_object), parent);
277 
278 	zend_object_std_init(&intern->std, class_type);
279 	object_properties_init(&intern->std, class_type);
280 
281 	if (orig && clone_orig) {
282 		spl_fixedarray_object *other = spl_fixed_array_from_obj(orig);
283 		spl_fixedarray_copy_ctor(&intern->array, &other->array);
284 	}
285 
286 	while (parent) {
287 		if (parent == spl_ce_SplFixedArray) {
288 			break;
289 		}
290 
291 		parent = parent->parent;
292 		inherited = true;
293 	}
294 
295 	ZEND_ASSERT(parent);
296 
297 	if (UNEXPECTED(inherited)) {
298 		/* Find count() method */
299 		zend_function *fptr_count = zend_hash_find_ptr(&class_type->function_table, ZSTR_KNOWN(ZEND_STR_COUNT));
300 		if (fptr_count->common.scope == parent) {
301 			fptr_count = NULL;
302 		}
303 		intern->fptr_count = fptr_count;
304 	}
305 
306 	return &intern->std;
307 }
308 
spl_fixedarray_new(zend_class_entry * class_type)309 static zend_object *spl_fixedarray_new(zend_class_entry *class_type)
310 {
311 	return spl_fixedarray_object_new_ex(class_type, NULL, 0);
312 }
313 
spl_fixedarray_object_clone(zend_object * old_object)314 static zend_object *spl_fixedarray_object_clone(zend_object *old_object)
315 {
316 	zend_object *new_object = spl_fixedarray_object_new_ex(old_object->ce, old_object, 1);
317 
318 	zend_objects_clone_members(new_object, old_object);
319 
320 	return new_object;
321 }
322 
spl_offset_convert_to_long(zval * offset)323 static zend_long spl_offset_convert_to_long(zval *offset) /* {{{ */
324 {
325 	try_again:
326 	switch (Z_TYPE_P(offset)) {
327 		case IS_STRING: {
328 			zend_ulong index;
329 			if (ZEND_HANDLE_NUMERIC(Z_STR_P(offset), index)) {
330 				return (zend_long) index;
331 			}
332 			break;
333 		}
334 		case IS_DOUBLE:
335 			return zend_dval_to_lval_safe(Z_DVAL_P(offset));
336 		case IS_LONG:
337 			return Z_LVAL_P(offset);
338 		case IS_FALSE:
339 			return 0;
340 		case IS_TRUE:
341 			return 1;
342 		case IS_REFERENCE:
343 			offset = Z_REFVAL_P(offset);
344 			goto try_again;
345 		case IS_RESOURCE:
346 			zend_use_resource_as_offset(offset);
347 			return Z_RES_HANDLE_P(offset);
348 	}
349 
350 	/* Use SplFixedArray name from the CE */
351 	zend_illegal_container_offset(spl_ce_SplFixedArray->name, offset, BP_VAR_R);
352 	return 0;
353 }
354 
spl_fixedarray_object_read_dimension_helper(spl_fixedarray_object * intern,zval * offset)355 static zval *spl_fixedarray_object_read_dimension_helper(spl_fixedarray_object *intern, zval *offset)
356 {
357 	zend_long index;
358 
359 	/* we have to return NULL on error here to avoid memleak because of
360 	 * ZE duplicating uninitialized_zval_ptr */
361 	if (!offset) {
362 		zend_throw_error(NULL, "[] operator not supported for SplFixedArray");
363 		return NULL;
364 	}
365 
366 	index = spl_offset_convert_to_long(offset);
367 	if (EG(exception)) {
368 		return NULL;
369 	}
370 
371 	if (index < 0 || index >= intern->array.size) {
372 		zend_throw_exception(spl_ce_OutOfBoundsException, "Index invalid or out of range", 0);
373 		return NULL;
374 	} else {
375 		return &intern->array.elements[index];
376 	}
377 }
378 
379 static int spl_fixedarray_object_has_dimension(zend_object *object, zval *offset, int check_empty);
380 
spl_fixedarray_object_read_dimension(zend_object * object,zval * offset,int type,zval * rv)381 static zval *spl_fixedarray_object_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
382 {
383 	if (type == BP_VAR_IS && !spl_fixedarray_object_has_dimension(object, offset, 0)) {
384 		return &EG(uninitialized_zval);
385 	}
386 
387 	if (HAS_FIXEDARRAY_ARRAYACCESS_OVERRIDE(object, zf_offsetget)) {
388 		zval tmp;
389 		if (!offset) {
390 			ZVAL_NULL(&tmp);
391 			offset = &tmp;
392 		}
393 		zend_call_known_instance_method_with_1_params(object->ce->arrayaccess_funcs_ptr->zf_offsetget, object, rv, offset);
394 		if (!Z_ISUNDEF_P(rv)) {
395 			return rv;
396 		}
397 		return &EG(uninitialized_zval);
398 	}
399 
400 	spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
401 	return spl_fixedarray_object_read_dimension_helper(intern, offset);
402 }
403 
spl_fixedarray_object_write_dimension_helper(spl_fixedarray_object * intern,zval * offset,zval * value)404 static void spl_fixedarray_object_write_dimension_helper(spl_fixedarray_object *intern, zval *offset, zval *value)
405 {
406 	zend_long index;
407 
408 	if (!offset) {
409 		/* '$array[] = value' syntax is not supported */
410 		zend_throw_error(NULL, "[] operator not supported for SplFixedArray");
411 		return;
412 	}
413 
414 	index = spl_offset_convert_to_long(offset);
415 	if (EG(exception)) {
416 		return;
417 	}
418 
419 	if (index < 0 || index >= intern->array.size) {
420 		zend_throw_exception(spl_ce_OutOfBoundsException, "Index invalid or out of range", 0);
421 		return;
422 	} else {
423 		/* Fix #81429 */
424 		zval *ptr = &(intern->array.elements[index]);
425 		zval tmp;
426 		ZVAL_COPY_VALUE(&tmp, ptr);
427 		ZVAL_COPY_DEREF(ptr, value);
428 		zval_ptr_dtor(&tmp);
429 	}
430 }
431 
spl_fixedarray_object_write_dimension(zend_object * object,zval * offset,zval * value)432 static void spl_fixedarray_object_write_dimension(zend_object *object, zval *offset, zval *value)
433 {
434 	if (HAS_FIXEDARRAY_ARRAYACCESS_OVERRIDE(object, zf_offsetset)) {
435 		zval tmp;
436 
437 		if (!offset) {
438 			ZVAL_NULL(&tmp);
439 			offset = &tmp;
440 		}
441 		zend_call_known_instance_method_with_2_params(object->ce->arrayaccess_funcs_ptr->zf_offsetset, object, NULL, offset, value);
442 		return;
443 	}
444 
445 	spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
446 	spl_fixedarray_object_write_dimension_helper(intern, offset, value);
447 }
448 
spl_fixedarray_object_unset_dimension_helper(spl_fixedarray_object * intern,zval * offset)449 static void spl_fixedarray_object_unset_dimension_helper(spl_fixedarray_object *intern, zval *offset)
450 {
451 	zend_long index;
452 
453 	index = spl_offset_convert_to_long(offset);
454 	if (EG(exception)) {
455 		return;
456 	}
457 
458 	if (index < 0 || index >= intern->array.size) {
459 		zend_throw_exception(spl_ce_OutOfBoundsException, "Index invalid or out of range", 0);
460 		return;
461 	} else {
462 		zval_ptr_dtor(&(intern->array.elements[index]));
463 		ZVAL_NULL(&intern->array.elements[index]);
464 	}
465 }
466 
spl_fixedarray_object_unset_dimension(zend_object * object,zval * offset)467 static void spl_fixedarray_object_unset_dimension(zend_object *object, zval *offset)
468 {
469 	if (UNEXPECTED(HAS_FIXEDARRAY_ARRAYACCESS_OVERRIDE(object, zf_offsetunset))) {
470 		zend_call_known_instance_method_with_1_params(object->ce->arrayaccess_funcs_ptr->zf_offsetunset, object, NULL, offset);
471 		return;
472 	}
473 
474 	spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
475 	spl_fixedarray_object_unset_dimension_helper(intern, offset);
476 }
477 
spl_fixedarray_object_has_dimension_helper(spl_fixedarray_object * intern,zval * offset,bool check_empty)478 static bool spl_fixedarray_object_has_dimension_helper(spl_fixedarray_object *intern, zval *offset, bool check_empty)
479 {
480 	zend_long index;
481 
482 	index = spl_offset_convert_to_long(offset);
483 	if (EG(exception)) {
484 		return false;
485 	}
486 
487 	if (index < 0 || index >= intern->array.size) {
488 		return false;
489 	}
490 
491 	if (check_empty) {
492 		return zend_is_true(&intern->array.elements[index]);
493 	}
494 
495 	return Z_TYPE(intern->array.elements[index]) != IS_NULL;
496 }
497 
spl_fixedarray_object_has_dimension(zend_object * object,zval * offset,int check_empty)498 static int spl_fixedarray_object_has_dimension(zend_object *object, zval *offset, int check_empty)
499 {
500 	if (HAS_FIXEDARRAY_ARRAYACCESS_OVERRIDE(object, zf_offsetexists)) {
501 		zval rv;
502 
503 		zend_call_known_instance_method_with_1_params(object->ce->arrayaccess_funcs_ptr->zf_offsetexists, object, &rv, offset);
504 		bool result = zend_is_true(&rv);
505 		zval_ptr_dtor(&rv);
506 		return result;
507 	}
508 
509 	spl_fixedarray_object *intern = spl_fixed_array_from_obj(object);
510 
511 	return spl_fixedarray_object_has_dimension_helper(intern, offset, check_empty);
512 }
513 
spl_fixedarray_object_count_elements(zend_object * object,zend_long * count)514 static zend_result spl_fixedarray_object_count_elements(zend_object *object, zend_long *count)
515 {
516 	spl_fixedarray_object *intern;
517 
518 	intern = spl_fixed_array_from_obj(object);
519 	if (UNEXPECTED(intern->fptr_count)) {
520 		zval rv;
521 		zend_call_known_instance_method_with_0_params(intern->fptr_count, object, &rv);
522 		if (!Z_ISUNDEF(rv)) {
523 			*count = zval_get_long(&rv);
524 			zval_ptr_dtor(&rv);
525 		} else {
526 			*count = 0;
527 		}
528 	} else {
529 		*count = intern->array.size;
530 	}
531 	return SUCCESS;
532 }
533 
PHP_METHOD(SplFixedArray,__construct)534 PHP_METHOD(SplFixedArray, __construct)
535 {
536 	zval *object = ZEND_THIS;
537 	spl_fixedarray_object *intern;
538 	zend_long size = 0;
539 
540 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &size) == FAILURE) {
541 		RETURN_THROWS();
542 	}
543 
544 	if (size < 0) {
545 		zend_argument_value_error(1, "must be greater than or equal to 0");
546 		RETURN_THROWS();
547 	}
548 
549 	intern = Z_SPLFIXEDARRAY_P(object);
550 
551 	if (!spl_fixedarray_empty(&intern->array)) {
552 		/* called __construct() twice, bail out */
553 		return;
554 	}
555 
556 	spl_fixedarray_init(&intern->array, size);
557 }
558 
PHP_METHOD(SplFixedArray,__wakeup)559 PHP_METHOD(SplFixedArray, __wakeup)
560 {
561 	spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
562 	HashTable *intern_ht = zend_std_get_properties(Z_OBJ_P(ZEND_THIS));
563 	zval *data;
564 
565 	if (zend_parse_parameters_none() == FAILURE) {
566 		RETURN_THROWS();
567 	}
568 
569 	if (intern->array.size == 0) {
570 		int index = 0;
571 		int size = zend_hash_num_elements(intern_ht);
572 
573 		spl_fixedarray_init(&intern->array, size);
574 
575 		ZEND_HASH_FOREACH_VAL(intern_ht, data) {
576 			ZVAL_COPY(&intern->array.elements[index], data);
577 			index++;
578 		} ZEND_HASH_FOREACH_END();
579 
580 		/* Remove the unserialised properties, since we now have the elements
581 		 * within the spl_fixedarray_object structure. */
582 		zend_hash_clean(intern_ht);
583 	}
584 }
585 
PHP_METHOD(SplFixedArray,__serialize)586 PHP_METHOD(SplFixedArray, __serialize)
587 {
588 	spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
589 	zval *current;
590 	zend_string *key;
591 
592 	if (zend_parse_parameters_none() == FAILURE) {
593 		RETURN_THROWS();
594 	}
595 
596 	HashTable *ht = zend_std_get_properties(&intern->std);
597 	uint32_t num_properties = zend_hash_num_elements(ht);
598 	array_init_size(return_value, intern->array.size + num_properties);
599 
600 	/* elements */
601 	for (zend_long i = 0; i < intern->array.size; i++) {
602 		current = &intern->array.elements[i];
603 		zend_hash_next_index_insert(Z_ARRVAL_P(return_value), current);
604 		Z_TRY_ADDREF_P(current);
605 	}
606 
607 	/* members */
608 	ZEND_HASH_FOREACH_STR_KEY_VAL_IND(ht, key, current) {
609 		/* If the properties table was already rebuild, it will also contain the
610 		 * array elements. The array elements are already added in the above loop.
611 		 * We can detect array elements by the fact that their key == NULL. */
612 		if (key != NULL) {
613 			zend_hash_add_new(Z_ARRVAL_P(return_value), key, current);
614 			Z_TRY_ADDREF_P(current);
615 		}
616 	} ZEND_HASH_FOREACH_END();
617 }
618 
PHP_METHOD(SplFixedArray,__unserialize)619 PHP_METHOD(SplFixedArray, __unserialize)
620 {
621 	spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
622 	HashTable *data;
623 	zval members_zv, *elem;
624 	zend_string *key;
625 	zend_long size;
626 
627 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
628 		RETURN_THROWS();
629 	}
630 
631 	if (intern->array.size == 0) {
632 		size = zend_hash_num_elements(data);
633 		spl_fixedarray_init_non_empty_struct(&intern->array, size);
634 		if (!size) {
635 			return;
636 		}
637 		array_init(&members_zv);
638 
639 		intern->array.size = 0;
640 		ZEND_HASH_FOREACH_STR_KEY_VAL(data, key, elem) {
641 			if (key == NULL) {
642 				ZVAL_COPY(&intern->array.elements[intern->array.size], elem);
643 				intern->array.size++;
644 			} else {
645 				Z_TRY_ADDREF_P(elem);
646 				zend_hash_add(Z_ARRVAL(members_zv), key, elem);
647 			}
648 		} ZEND_HASH_FOREACH_END();
649 
650 		if (intern->array.size != size) {
651 			if (intern->array.size) {
652 				intern->array.elements = erealloc(intern->array.elements, sizeof(zval) * intern->array.size);
653 			} else {
654 				efree(intern->array.elements);
655 				intern->array.elements = NULL;
656 			}
657 		}
658 
659 		object_properties_load(&intern->std, Z_ARRVAL(members_zv));
660 		zval_ptr_dtor(&members_zv);
661 	}
662 }
663 
PHP_METHOD(SplFixedArray,count)664 PHP_METHOD(SplFixedArray, count)
665 {
666 	zval *object = ZEND_THIS;
667 	spl_fixedarray_object *intern;
668 
669 	if (zend_parse_parameters_none() == FAILURE) {
670 		RETURN_THROWS();
671 	}
672 
673 	intern = Z_SPLFIXEDARRAY_P(object);
674 	RETURN_LONG(intern->array.size);
675 }
676 
PHP_METHOD(SplFixedArray,toArray)677 PHP_METHOD(SplFixedArray, toArray)
678 {
679 	spl_fixedarray_object *intern;
680 
681 	if (zend_parse_parameters_none() == FAILURE) {
682 		RETURN_THROWS();
683 	}
684 
685 	intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
686 
687 	if (!spl_fixedarray_empty(&intern->array)) {
688 		array_init(return_value);
689 		for (zend_long i = 0; i < intern->array.size; i++) {
690 			zend_hash_index_update(Z_ARRVAL_P(return_value), i, &intern->array.elements[i]);
691 			Z_TRY_ADDREF(intern->array.elements[i]);
692 		}
693 	} else {
694 		RETURN_EMPTY_ARRAY();
695 	}
696 }
697 
PHP_METHOD(SplFixedArray,fromArray)698 PHP_METHOD(SplFixedArray, fromArray)
699 {
700 	zval *data;
701 	spl_fixedarray array;
702 	spl_fixedarray_object *intern;
703 	int num;
704 	bool save_indexes = 1;
705 
706 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|b", &data, &save_indexes) == FAILURE) {
707 		RETURN_THROWS();
708 	}
709 
710 	num = zend_hash_num_elements(Z_ARRVAL_P(data));
711 
712 	if (num > 0 && save_indexes) {
713 		zval *element;
714 		zend_string *str_index;
715 		zend_ulong num_index, max_index = 0;
716 		zend_long tmp;
717 
718 		ZEND_HASH_FOREACH_KEY(Z_ARRVAL_P(data), num_index, str_index) {
719 			if (str_index != NULL || (zend_long)num_index < 0) {
720 				zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "array must contain only positive integer keys");
721 				RETURN_THROWS();
722 			}
723 
724 			if (num_index > max_index) {
725 				max_index = num_index;
726 			}
727 		} ZEND_HASH_FOREACH_END();
728 
729 		tmp = max_index + 1;
730 		if (tmp <= 0) {
731 			zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "integer overflow detected");
732 			RETURN_THROWS();
733 		}
734 		spl_fixedarray_init(&array, tmp);
735 
736 		ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), num_index, element) {
737 			ZVAL_COPY_DEREF(&array.elements[num_index], element);
738 		} ZEND_HASH_FOREACH_END();
739 
740 	} else if (num > 0 && !save_indexes) {
741 		zval *element;
742 		zend_long i = 0;
743 
744 		spl_fixedarray_init(&array, num);
745 
746 		ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(data), element) {
747 			ZVAL_COPY_DEREF(&array.elements[i], element);
748 			i++;
749 		} ZEND_HASH_FOREACH_END();
750 	} else {
751 		spl_fixedarray_init(&array, 0);
752 	}
753 
754 	object_init_ex(return_value, spl_ce_SplFixedArray);
755 
756 	intern = Z_SPLFIXEDARRAY_P(return_value);
757 	intern->array = array;
758 }
759 
PHP_METHOD(SplFixedArray,getSize)760 PHP_METHOD(SplFixedArray, getSize)
761 {
762 	zval *object = ZEND_THIS;
763 	spl_fixedarray_object *intern;
764 
765 	if (zend_parse_parameters_none() == FAILURE) {
766 		RETURN_THROWS();
767 	}
768 
769 	intern = Z_SPLFIXEDARRAY_P(object);
770 	RETURN_LONG(intern->array.size);
771 }
772 
PHP_METHOD(SplFixedArray,setSize)773 PHP_METHOD(SplFixedArray, setSize)
774 {
775 	zval *object = ZEND_THIS;
776 	spl_fixedarray_object *intern;
777 	zend_long size;
778 
779 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) {
780 		RETURN_THROWS();
781 	}
782 
783 	if (size < 0) {
784 		zend_argument_value_error(1, "must be greater than or equal to 0");
785 		RETURN_THROWS();
786 	}
787 
788 	intern = Z_SPLFIXEDARRAY_P(object);
789 
790 	spl_fixedarray_resize(&intern->array, size);
791 	RETURN_TRUE;
792 }
793 
794 /* Returns whether the requested $index exists. */
PHP_METHOD(SplFixedArray,offsetExists)795 PHP_METHOD(SplFixedArray, offsetExists)
796 {
797 	zval                  *zindex;
798 	spl_fixedarray_object  *intern;
799 
800 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
801 		RETURN_THROWS();
802 	}
803 
804 	intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
805 
806 	RETURN_BOOL(spl_fixedarray_object_has_dimension_helper(intern, zindex, 0));
807 }
808 
809 /* Returns the value at the specified $index. */
PHP_METHOD(SplFixedArray,offsetGet)810 PHP_METHOD(SplFixedArray, offsetGet)
811 {
812 	zval *zindex, *value;
813 	spl_fixedarray_object  *intern;
814 
815 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
816 		RETURN_THROWS();
817 	}
818 
819 	intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
820 	value = spl_fixedarray_object_read_dimension_helper(intern, zindex);
821 
822 	if (value) {
823 		RETURN_COPY_DEREF(value);
824 	} else {
825 		RETURN_NULL();
826 	}
827 }
828 
829 /* Sets the value at the specified $index to $newval. */
PHP_METHOD(SplFixedArray,offsetSet)830 PHP_METHOD(SplFixedArray, offsetSet)
831 {
832 	zval                  *zindex, *value;
833 	spl_fixedarray_object  *intern;
834 
835 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &zindex, &value) == FAILURE) {
836 		RETURN_THROWS();
837 	}
838 
839 	intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
840 	spl_fixedarray_object_write_dimension_helper(intern, zindex, value);
841 
842 }
843 
844 /* Unsets the value at the specified $index. */
PHP_METHOD(SplFixedArray,offsetUnset)845 PHP_METHOD(SplFixedArray, offsetUnset)
846 {
847 	zval                  *zindex;
848 	spl_fixedarray_object  *intern;
849 
850 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
851 		RETURN_THROWS();
852 	}
853 
854 	intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
855 	spl_fixedarray_object_unset_dimension_helper(intern, zindex);
856 
857 }
858 
859 /* Create a new iterator from a SplFixedArray instance. */
PHP_METHOD(SplFixedArray,getIterator)860 PHP_METHOD(SplFixedArray, getIterator)
861 {
862 	if (zend_parse_parameters_none() == FAILURE) {
863 		RETURN_THROWS();
864 	}
865 
866 	zend_create_internal_iterator_zval(return_value, ZEND_THIS);
867 }
868 
PHP_METHOD(SplFixedArray,jsonSerialize)869 PHP_METHOD(SplFixedArray, jsonSerialize)
870 {
871 	ZEND_PARSE_PARAMETERS_NONE();
872 
873 	spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
874 	array_init_size(return_value, intern->array.size);
875 	for (zend_long i = 0; i < intern->array.size; i++) {
876 		zend_hash_next_index_insert_new(Z_ARR_P(return_value), &intern->array.elements[i]);
877 		Z_TRY_ADDREF(intern->array.elements[i]);
878 	}
879 }
880 
spl_fixedarray_it_dtor(zend_object_iterator * iter)881 static void spl_fixedarray_it_dtor(zend_object_iterator *iter)
882 {
883 	zval_ptr_dtor(&iter->data);
884 }
885 
spl_fixedarray_it_rewind(zend_object_iterator * iter)886 static void spl_fixedarray_it_rewind(zend_object_iterator *iter)
887 {
888 	((spl_fixedarray_it*)iter)->current = 0;
889 }
890 
spl_fixedarray_it_valid(zend_object_iterator * iter)891 static zend_result spl_fixedarray_it_valid(zend_object_iterator *iter)
892 {
893 	spl_fixedarray_it     *iterator = (spl_fixedarray_it*)iter;
894 	spl_fixedarray_object *object   = Z_SPLFIXEDARRAY_P(&iter->data);
895 
896 	if (iterator->current >= 0 && iterator->current < object->array.size) {
897 		return SUCCESS;
898 	}
899 
900 	return FAILURE;
901 }
902 
spl_fixedarray_it_get_current_data(zend_object_iterator * iter)903 static zval *spl_fixedarray_it_get_current_data(zend_object_iterator *iter)
904 {
905 	zval zindex, *data;
906 	spl_fixedarray_it     *iterator = (spl_fixedarray_it*)iter;
907 	spl_fixedarray_object *object   = Z_SPLFIXEDARRAY_P(&iter->data);
908 
909 	ZVAL_LONG(&zindex, iterator->current);
910 	data = spl_fixedarray_object_read_dimension_helper(object, &zindex);
911 
912 	if (data == NULL) {
913 		data = &EG(uninitialized_zval);
914 	}
915 	return data;
916 }
917 
spl_fixedarray_it_get_current_key(zend_object_iterator * iter,zval * key)918 static void spl_fixedarray_it_get_current_key(zend_object_iterator *iter, zval *key)
919 {
920 	ZVAL_LONG(key, ((spl_fixedarray_it*)iter)->current);
921 }
922 
spl_fixedarray_it_move_forward(zend_object_iterator * iter)923 static void spl_fixedarray_it_move_forward(zend_object_iterator *iter)
924 {
925 	((spl_fixedarray_it*)iter)->current++;
926 }
927 
928 /* iterator handler table */
929 static const zend_object_iterator_funcs spl_fixedarray_it_funcs = {
930 	spl_fixedarray_it_dtor,
931 	spl_fixedarray_it_valid,
932 	spl_fixedarray_it_get_current_data,
933 	spl_fixedarray_it_get_current_key,
934 	spl_fixedarray_it_move_forward,
935 	spl_fixedarray_it_rewind,
936 	NULL,
937 	NULL, /* get_gc */
938 };
939 
spl_fixedarray_get_iterator(zend_class_entry * ce,zval * object,int by_ref)940 static zend_object_iterator *spl_fixedarray_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
941 {
942 	spl_fixedarray_it *iterator;
943 
944 	if (by_ref) {
945 		zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
946 		return NULL;
947 	}
948 
949 	iterator = emalloc(sizeof(spl_fixedarray_it));
950 
951 	zend_iterator_init((zend_object_iterator*)iterator);
952 
953 	ZVAL_OBJ_COPY(&iterator->intern.data, Z_OBJ_P(object));
954 	iterator->intern.funcs = &spl_fixedarray_it_funcs;
955 
956 	return &iterator->intern;
957 }
958 
PHP_MINIT_FUNCTION(spl_fixedarray)959 PHP_MINIT_FUNCTION(spl_fixedarray)
960 {
961 	spl_ce_SplFixedArray = register_class_SplFixedArray(
962 		zend_ce_aggregate, zend_ce_arrayaccess, zend_ce_countable, php_json_serializable_ce);
963 	spl_ce_SplFixedArray->create_object = spl_fixedarray_new;
964 	spl_ce_SplFixedArray->default_object_handlers = &spl_handler_SplFixedArray;
965 	spl_ce_SplFixedArray->get_iterator = spl_fixedarray_get_iterator;
966 
967 	memcpy(&spl_handler_SplFixedArray, &std_object_handlers, sizeof(zend_object_handlers));
968 
969 	spl_handler_SplFixedArray.offset          = XtOffsetOf(spl_fixedarray_object, std);
970 	spl_handler_SplFixedArray.clone_obj       = spl_fixedarray_object_clone;
971 	spl_handler_SplFixedArray.read_dimension  = spl_fixedarray_object_read_dimension;
972 	spl_handler_SplFixedArray.write_dimension = spl_fixedarray_object_write_dimension;
973 	spl_handler_SplFixedArray.unset_dimension = spl_fixedarray_object_unset_dimension;
974 	spl_handler_SplFixedArray.has_dimension   = spl_fixedarray_object_has_dimension;
975 	spl_handler_SplFixedArray.count_elements  = spl_fixedarray_object_count_elements;
976 	spl_handler_SplFixedArray.get_properties_for = spl_fixedarray_object_get_properties_for;
977 	spl_handler_SplFixedArray.get_gc          = spl_fixedarray_object_get_gc;
978 	spl_handler_SplFixedArray.free_obj        = spl_fixedarray_object_free_storage;
979 
980 	return SUCCESS;
981 }
982