Coverage Report

Created: 2025-11-16 06:23

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