Coverage Report

Created: 2026-10-28 06:20

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