Coverage Report

Created: 2025-12-14 06:05

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
0
static inline spl_array_object *spl_array_from_obj(zend_object *obj) /* {{{ */ {
59
0
  return (spl_array_object*)((char*)(obj) - XtOffsetOf(spl_array_object, std));
60
0
}
61
/* }}} */
62
63
0
#define Z_SPLARRAY_P(zv)  spl_array_from_obj(Z_OBJ_P((zv)))
64
65
0
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
0
  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
0
  } 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
0
  } else if (Z_TYPE(intern->array) == IS_ARRAY) {
75
0
    return &Z_ARRVAL(intern->array);
76
0
  } else {
77
0
    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
0
    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
0
    ZEND_ASSERT(!zend_lazy_object_must_init(obj));
91
    /* rebuild properties */
92
0
    zend_std_get_properties_ex(obj);
93
0
    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
0
    return &obj->properties;
100
0
  }
101
0
}
102
/* }}} */
103
104
0
static inline HashTable *spl_array_get_hash_table(spl_array_object* intern) { /* {{{ */
105
0
  return *spl_array_get_hash_table_ptr(intern);
106
0
}
107
/* }}} */
108
109
static inline bool spl_array_is_object(spl_array_object *intern) /* {{{ */
110
0
{
111
0
  while (intern->ar_flags & SPL_ARRAY_USE_OTHER) {
112
0
    intern = Z_SPLARRAY_P(&intern->array);
113
0
  }
114
0
  return (intern->ar_flags & SPL_ARRAY_IS_SELF) || Z_TYPE(intern->array) == IS_OBJECT;
115
0
}
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
0
{
122
0
  intern->ht_iter = zend_hash_iterator_add(ht, zend_hash_get_current_pos(ht));
123
0
  zend_hash_internal_pointer_reset_ex(ht, &EG(ht_iterators)[intern->ht_iter].pos);
124
0
  spl_array_skip_protected(intern, ht);
125
0
}
126
/* }}} */
127
128
static zend_always_inline uint32_t *spl_array_get_pos_ptr(HashTable *ht, spl_array_object* intern) /* {{{ */
129
0
{
130
0
  if (UNEXPECTED(intern->ht_iter == (uint32_t)-1)) {
131
0
    spl_array_create_ht_iter(ht, intern);
132
0
  }
133
0
  return &EG(ht_iterators)[intern->ht_iter].pos;
134
0
}
135
/* }}} */
136
137
/* {{{ spl_array_object_free_storage */
138
static void spl_array_object_free_storage(zend_object *object)
139
0
{
140
0
  spl_array_object *intern = spl_array_from_obj(object);
141
142
0
  if (intern->ht_iter != (uint32_t) -1) {
143
0
    zend_hash_iterator_del(intern->ht_iter);
144
0
  }
145
146
0
  if (UNEXPECTED(intern->sentinel_array)) {
147
0
    zend_array_release(intern->sentinel_array);
148
0
  }
149
150
0
  zend_object_std_dtor(&intern->std);
151
152
0
  zval_ptr_dtor(&intern->array);
153
0
}
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
0
{
159
0
  spl_array_object *intern;
160
0
  zend_class_entry *parent = class_type;
161
0
  int inherited = 0;
162
163
0
  intern = zend_object_alloc(sizeof(spl_array_object), parent);
164
165
0
  zend_object_std_init(&intern->std, class_type);
166
0
  object_properties_init(&intern->std, class_type);
167
168
0
  intern->ar_flags = 0;
169
0
  intern->is_child = false;
170
0
  intern->bucket = NULL;
171
0
  intern->ce_get_iterator = spl_ce_ArrayIterator;
172
0
  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
0
  } else {
201
0
    array_init(&intern->array);
202
0
  }
203
204
0
  while (parent) {
205
0
    if (parent == spl_ce_ArrayIterator || parent == spl_ce_RecursiveArrayIterator || parent == spl_ce_ArrayObject) {
206
0
      break;
207
0
    }
208
0
    parent = parent->parent;
209
0
    inherited = 1;
210
0
  }
211
212
0
  ZEND_ASSERT(parent);
213
214
0
  if (inherited) {
215
0
    intern->fptr_offset_get = zend_hash_str_find_ptr(&class_type->function_table, "offsetget", sizeof("offsetget") - 1);
216
0
    if (intern->fptr_offset_get->common.scope == parent) {
217
0
      intern->fptr_offset_get = NULL;
218
0
    }
219
0
    intern->fptr_offset_set = zend_hash_str_find_ptr(&class_type->function_table, "offsetset", sizeof("offsetset") - 1);
220
0
    if (intern->fptr_offset_set->common.scope == parent) {
221
0
      intern->fptr_offset_set = NULL;
222
0
    }
223
0
    intern->fptr_offset_has = zend_hash_str_find_ptr(&class_type->function_table, "offsetexists", sizeof("offsetexists") - 1);
224
0
    if (intern->fptr_offset_has->common.scope == parent) {
225
0
      intern->fptr_offset_has = NULL;
226
0
    }
227
0
    intern->fptr_offset_del = zend_hash_str_find_ptr(&class_type->function_table, "offsetunset",  sizeof("offsetunset") - 1);
228
0
    if (intern->fptr_offset_del->common.scope == parent) {
229
0
      intern->fptr_offset_del = NULL;
230
0
    }
231
    /* Find count() method */
232
0
    intern->fptr_count = zend_hash_find_ptr(&class_type->function_table, ZSTR_KNOWN(ZEND_STR_COUNT));
233
0
    if (intern->fptr_count->common.scope == parent) {
234
0
      intern->fptr_count = NULL;
235
0
    }
236
0
  }
237
238
0
  intern->ht_iter = (uint32_t)-1;
239
0
  return &intern->std;
240
0
}
241
/* }}} */
242
243
/* {{{ spl_array_object_new */
244
static zend_object *spl_array_object_new(zend_class_entry *class_type)
245
0
{
246
0
  return spl_array_object_new_ex(class_type, NULL, 0);
247
0
}
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
0
static void spl_hash_key_release(spl_hash_key *key) {
270
0
  if (key->release_key) {
271
0
    zend_string_release_ex(key->key, 0);
272
0
  }
273
0
}
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
0
{
279
0
  key->release_key = false;
280
0
try_again:
281
0
  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
0
  case IS_STRING:
287
0
    key->key = Z_STR_P(offset);
288
0
    if (ZEND_HANDLE_NUMERIC(key->key, key->h)) {
289
0
      key->key = NULL;
290
0
      break;
291
0
    }
292
0
    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
0
  case IS_DOUBLE:
299
0
    key->key = NULL;
300
0
    key->h = zend_dval_to_lval_safe(Z_DVAL_P(offset));
301
0
    break;
302
0
  case IS_FALSE:
303
0
    key->key = NULL;
304
0
    key->h = 0;
305
0
    break;
306
0
  case IS_TRUE:
307
0
    key->key = NULL;
308
0
    key->h = 1;
309
0
    break;
310
0
  case IS_LONG:
311
0
    key->key = NULL;
312
0
    key->h = Z_LVAL_P(offset);
313
0
    break;
314
0
  case IS_REFERENCE:
315
0
    ZVAL_DEREF(offset);
316
0
    goto try_again;
317
0
  default:
318
0
    return FAILURE;
319
0
  }
320
321
0
  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
0
  return SUCCESS;
326
0
}
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
0
{
331
0
  zval *retval;
332
0
  spl_hash_key key;
333
0
  HashTable *ht = spl_array_get_hash_table(intern);
334
335
0
  if (!offset || Z_ISUNDEF_P(offset) || !ht) {
336
0
    return &EG(uninitialized_zval);
337
0
  }
338
339
0
  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
0
  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
0
  if (key.key) {
351
0
    retval = zend_hash_find(ht, key.key);
352
0
    if (retval) {
353
0
      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
0
    } else {
374
0
      switch (type) {
375
0
        case BP_VAR_R:
376
0
          zend_error(E_WARNING, "Undefined array key \"%s\"", ZSTR_VAL(key.key));
377
0
          ZEND_FALLTHROUGH;
378
0
        case BP_VAR_UNSET:
379
0
        case BP_VAR_IS:
380
0
          retval = &EG(uninitialized_zval);
381
0
          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
0
      }
391
0
    }
392
0
    spl_hash_key_release(&key);
393
0
  } else {
394
0
    if ((retval = zend_hash_index_find(ht, key.h)) == NULL) {
395
0
      switch (type) {
396
0
        case BP_VAR_R:
397
0
          zend_error(E_WARNING, "Undefined array key " ZEND_LONG_FMT, key.h);
398
0
          ZEND_FALLTHROUGH;
399
0
        case BP_VAR_UNSET:
400
0
        case BP_VAR_IS:
401
0
          retval = &EG(uninitialized_zval);
402
0
          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
0
        case BP_VAR_W: {
407
0
            zval value;
408
0
          ZVAL_NULL(&value);
409
0
          retval = zend_hash_index_update(ht, key.h, &value);
410
0
         }
411
0
      }
412
0
    }
413
0
  }
414
0
  return retval;
415
0
} /* }}} */
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
0
{
421
0
  spl_array_object *intern = spl_array_from_obj(object);
422
0
  zval *ret;
423
424
0
  if (check_inherited &&
425
0
      (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
0
  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
0
  if ((type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) &&
455
0
      !Z_ISREF_P(ret) &&
456
0
      EXPECTED(ret != &EG(uninitialized_zval))) {
457
0
    ZVAL_NEW_REF(ret, ret);
458
0
  }
459
460
0
  return ret;
461
0
} /* }}} */
462
463
static zval *spl_array_read_dimension(zend_object *object, zval *offset, int type, zval *rv) /* {{{ */
464
0
{
465
0
  return spl_array_read_dimension_ex(1, object, offset, type, rv);
466
0
} /* }}} */
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
0
{
475
0
  uint32_t old_refcount = 0;
476
0
  if (is_child) {
477
0
    old_refcount = GC_REFCOUNT(ht);
478
0
    GC_SET_REFCOUNT(ht, refcount);
479
0
  }
480
481
0
  return old_refcount;
482
0
} /* }}} */
483
484
static void spl_array_write_dimension_ex(int check_inherited, zend_object *object, zval *offset, zval *value) /* {{{ */
485
0
{
486
0
  spl_array_object *intern = spl_array_from_obj(object);
487
0
  HashTable *ht;
488
0
  spl_hash_key key;
489
490
0
  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
0
  if (intern->nApplyCount > 0) {
502
0
    zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
503
0
    return;
504
0
  }
505
506
0
  Z_TRY_ADDREF_P(value);
507
508
0
  uint32_t refcount = 0;
509
0
  if (!offset || Z_TYPE_P(offset) == IS_NULL) {
510
0
    ht = spl_array_get_hash_table(intern);
511
0
    if (UNEXPECTED(ht == intern->sentinel_array)) {
512
0
      return;
513
0
    }
514
0
    refcount = spl_array_set_refcount(intern->is_child, ht, 1);
515
0
    zend_hash_next_index_insert(ht, value);
516
517
0
    if (refcount) {
518
0
      spl_array_set_refcount(intern->is_child, ht, refcount);
519
0
    }
520
0
    return;
521
0
  }
522
523
0
  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
0
  ht = spl_array_get_hash_table(intern);
530
0
  if (UNEXPECTED(ht == intern->sentinel_array)) {
531
0
    spl_hash_key_release(&key);
532
0
    return;
533
0
  }
534
0
  refcount = spl_array_set_refcount(intern->is_child, ht, 1);
535
0
  if (key.key) {
536
0
    zend_hash_update_ind(ht, key.key, value);
537
0
    spl_hash_key_release(&key);
538
0
  } else {
539
0
    zend_hash_index_update(ht, key.h, value);
540
0
  }
541
542
0
  if (refcount) {
543
0
    spl_array_set_refcount(intern->is_child, ht, refcount);
544
0
  }
545
0
} /* }}} */
546
547
static void spl_array_write_dimension(zend_object *object, zval *offset, zval *value) /* {{{ */
548
0
{
549
0
  spl_array_write_dimension_ex(1, object, offset, value);
550
0
} /* }}} */
551
552
static void spl_array_unset_dimension_ex(int check_inherited, zend_object *object, zval *offset) /* {{{ */
553
0
{
554
0
  HashTable *ht;
555
0
  spl_array_object *intern = spl_array_from_obj(object);
556
0
  spl_hash_key key;
557
558
0
  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
0
  if (intern->nApplyCount > 0) {
564
0
    zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
565
0
    return;
566
0
  }
567
568
0
  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
0
  ht = spl_array_get_hash_table(intern);
574
0
  uint32_t refcount = spl_array_set_refcount(intern->is_child, ht, 1);
575
576
0
  if (key.key) {
577
0
    zval *data = zend_hash_find(ht, key.key);
578
0
    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
0
    spl_hash_key_release(&key);
597
0
  } else {
598
0
    zend_hash_index_del(ht, key.h);
599
0
  }
600
601
0
  if (refcount) {
602
0
    spl_array_set_refcount(intern->is_child, ht, refcount);
603
0
  }
604
0
} /* }}} */
605
606
static void spl_array_unset_dimension(zend_object *object, zval *offset) /* {{{ */
607
0
{
608
0
  spl_array_unset_dimension_ex(1, object, offset);
609
0
} /* }}} */
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
0
{
616
0
  spl_array_object *intern = spl_array_from_obj(object);
617
0
  zval rv, *value = NULL, *tmp;
618
619
0
  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
0
  if (!value) {
637
0
    HashTable *ht = spl_array_get_hash_table(intern);
638
0
    spl_hash_key key;
639
640
0
    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
0
    if (key.key) {
646
0
      tmp = zend_hash_find(ht, key.key);
647
0
      spl_hash_key_release(&key);
648
0
    } else {
649
0
      tmp = zend_hash_index_find(ht, key.h);
650
0
    }
651
652
0
    if (!tmp) {
653
0
      return false;
654
0
    }
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
0
    if (check_empty == 2) {
659
0
      return true;
660
0
    }
661
662
0
    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
0
    } else {
665
0
      value = tmp;
666
0
    }
667
0
  }
668
669
  /* empty() check the value is not falsy, isset() only check it is not null */
670
0
  bool result = check_empty ? zend_is_true(value) : Z_TYPE_P(value) != IS_NULL;
671
672
0
  if (value == &rv) {
673
0
    zval_ptr_dtor(&rv);
674
0
  }
675
676
0
  return result;
677
0
} /* }}} */
678
679
static int spl_array_has_dimension(zend_object *object, zval *offset, int check_empty) /* {{{ */
680
0
{
681
0
  return spl_array_has_dimension_ex(/* check_inherited */ true, object, offset, check_empty);
682
0
} /* }}} */
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
0
{
697
0
  zval *value, *index;
698
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
699
0
    RETURN_THROWS();
700
0
  }
701
0
  value = spl_array_read_dimension_ex(0, Z_OBJ_P(ZEND_THIS), index, BP_VAR_R, return_value);
702
0
  if (value != return_value) {
703
0
    RETURN_COPY_DEREF(value);
704
0
  }
705
0
} /* }}} */
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
0
{
743
0
  zval *index;
744
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
745
0
    RETURN_THROWS();
746
0
  }
747
0
  spl_array_unset_dimension_ex(0, Z_OBJ_P(ZEND_THIS), index);
748
0
} /* }}} */
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
0
{
763
0
  spl_array_object *intern = spl_array_from_obj(object);
764
0
  HashTable *ht;
765
0
  bool dup;
766
767
0
  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
0
  switch (purpose) {
777
0
    case ZEND_PROP_PURPOSE_ARRAY_CAST:
778
0
      dup = true;
779
0
      break;
780
0
    case ZEND_PROP_PURPOSE_VAR_EXPORT:
781
0
    case ZEND_PROP_PURPOSE_JSON:
782
0
      dup = false;
783
0
      break;
784
0
    default:
785
0
      return zend_std_get_properties_for(object, purpose);
786
0
  }
787
788
0
  ht = spl_array_get_hash_table(intern);
789
0
  if (dup) {
790
0
    ht = zend_array_dup(ht);
791
0
  } else {
792
0
    GC_ADDREF(ht);
793
0
  }
794
0
  return ht;
795
0
} /* }}} */
796
797
static inline HashTable* spl_array_get_debug_info(zend_object *obj) /* {{{ */
798
0
{
799
0
  spl_array_object *intern = spl_array_from_obj(obj);
800
0
  HashTable *properties = zend_std_get_properties_ex(&intern->std);
801
802
0
  if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
803
0
    return zend_array_dup(properties);
804
0
  } else {
805
0
    HashTable *debug_info;
806
807
0
    debug_info = zend_new_array(zend_hash_num_elements(properties) + 1);
808
0
    zend_hash_copy(debug_info, properties, (copy_ctor_func_t) zval_add_ref);
809
810
0
    zval *storage = &intern->array;
811
0
    Z_TRY_ADDREF_P(storage);
812
813
0
    const zend_class_entry *base_class_ce = instanceof_function(obj->ce, spl_ce_ArrayIterator)
814
0
      ? spl_ce_ArrayIterator : spl_ce_ArrayObject;
815
816
0
    spl_set_private_debug_info_property(base_class_ce, "storage", strlen("storage"), debug_info, storage);
817
818
0
    return debug_info;
819
0
  }
820
0
}
821
/* }}} */
822
823
static HashTable *spl_array_get_gc(zend_object *obj, zval **gc_data, int *gc_data_count) /* {{{ */
824
0
{
825
0
  spl_array_object *intern = spl_array_from_obj(obj);
826
0
  *gc_data = &intern->array;
827
0
  *gc_data_count = 1;
828
0
  return zend_std_get_properties(obj);
829
0
}
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
0
{
847
0
  spl_array_object *intern = spl_array_from_obj(object);
848
849
0
  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
0
  return zend_std_write_property(object, name, value, cache_slot);
857
0
} /* }}} */
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
0
{
883
0
  spl_array_object *intern = spl_array_from_obj(object);
884
885
0
  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
0
  return zend_std_has_property(object, name, has_set_exists, cache_slot);
892
0
} /* }}} */
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
0
{
910
0
  HashTable     *ht1,
911
0
            *ht2;
912
0
  spl_array_object  *intern1,
913
0
            *intern2;
914
0
  int         result  = 0;
915
916
0
  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
0
} /* }}} */
931
932
static zend_result spl_array_skip_protected(spl_array_object *intern, HashTable *aht) /* {{{ */
933
0
{
934
0
  zend_string *string_key;
935
0
  zend_ulong num_key;
936
0
  zval *data;
937
938
0
  if (spl_array_is_object(intern)) {
939
0
    uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
940
941
0
    do {
942
0
      if (zend_hash_get_current_key_ex(aht, &string_key, &num_key, pos_ptr) == HASH_KEY_IS_STRING) {
943
0
        data = zend_hash_get_current_data_ex(aht, pos_ptr);
944
0
        if (data && Z_TYPE_P(data) == IS_INDIRECT &&
945
0
            Z_TYPE_P(data = Z_INDIRECT_P(data)) == IS_UNDEF) {
946
          /* skip */
947
0
        } else if (!ZSTR_LEN(string_key) || ZSTR_VAL(string_key)[0]) {
948
0
          return SUCCESS;
949
0
        }
950
0
      } else {
951
0
        return SUCCESS;
952
0
      }
953
0
      if (zend_hash_has_more_elements_ex(aht, pos_ptr) != SUCCESS) {
954
0
        return FAILURE;
955
0
      }
956
0
      zend_hash_move_forward_ex(aht, pos_ptr);
957
0
    } while (1);
958
0
  }
959
0
  return FAILURE;
960
0
} /* }}} */
961
962
/* {{{ spl_array_set_array */
963
0
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
0
  ZEND_ASSERT(Z_TYPE_P(array) == IS_ARRAY || Z_TYPE_P(array) == IS_OBJECT);
966
0
  zval garbage;
967
0
  ZVAL_UNDEF(&garbage);
968
0
  if (Z_TYPE_P(array) == IS_ARRAY) {
969
0
    ZVAL_COPY_VALUE(&garbage, &intern->array);
970
0
    if (Z_REFCOUNT_P(array) == 1) {
971
0
      ZVAL_COPY(&intern->array, array);
972
0
    } else {
973
      //??? TODO: try to avoid array duplication
974
0
      ZVAL_ARR(&intern->array, zend_array_dup(Z_ARR_P(array)));
975
976
0
      if (intern->is_child) {
977
0
        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
0
        ZVAL_COPY(&intern->bucket->val, &intern->array);
983
0
      }
984
0
    }
985
0
  } else {
986
0
    php_error_docref(NULL, E_DEPRECATED,
987
0
      "Using an object as a backing array for %s is deprecated, as it allows violating class constraints and invariants",
988
0
      instanceof_function(Z_OBJCE_P(object), spl_ce_ArrayIterator) ? "ArrayIterator" : "ArrayObject");
989
0
    if (UNEXPECTED(EG(exception))) {
990
0
      return;
991
0
    }
992
0
    if (Z_OBJ_HT_P(array) == &spl_handler_ArrayObject) {
993
0
      ZVAL_COPY_VALUE(&garbage, &intern->array);
994
0
      if (just_array) {
995
0
        spl_array_object *other = Z_SPLARRAY_P(array);
996
0
        ar_flags = other->ar_flags & ~SPL_ARRAY_INT_MASK;
997
0
      }
998
0
      if (Z_OBJ_P(object) == Z_OBJ_P(array)) {
999
0
        ar_flags |= SPL_ARRAY_IS_SELF;
1000
0
        ZVAL_UNDEF(&intern->array);
1001
0
      } else {
1002
0
        ar_flags |= SPL_ARRAY_USE_OTHER;
1003
0
        ZVAL_COPY(&intern->array, array);
1004
0
      }
1005
0
    } else {
1006
0
      zend_object_get_properties_t handler = Z_OBJ_HANDLER_P(array, get_properties);
1007
0
      if (handler != zend_std_get_properties || Z_OBJ_HANDLER_P(array, get_properties_for)) {
1008
0
        zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
1009
0
          "Overloaded object of type %s is not compatible with %s",
1010
0
          ZSTR_VAL(Z_OBJCE_P(array)->name), ZSTR_VAL(intern->std.ce->name));
1011
0
        ZEND_ASSERT(Z_TYPE(garbage) == IS_UNDEF);
1012
0
        return;
1013
0
      }
1014
0
      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
0
      ZVAL_COPY_VALUE(&garbage, &intern->array);
1022
0
      ZVAL_COPY(&intern->array, array);
1023
0
    }
1024
0
  }
1025
1026
0
  intern->ar_flags &= ~SPL_ARRAY_IS_SELF & ~SPL_ARRAY_USE_OTHER;
1027
0
  intern->ar_flags |= ar_flags;
1028
0
  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
0
  zval_ptr_dtor(&garbage);
1034
0
}
1035
/* }}} */
1036
1037
/* {{{ Constructs a new array object from an array or object. */
1038
PHP_METHOD(ArrayObject, __construct)
1039
0
{
1040
0
  zval *object = ZEND_THIS;
1041
0
  spl_array_object *intern;
1042
0
  zval *array;
1043
0
  zend_long ar_flags = 0;
1044
0
  zend_class_entry *ce_get_iterator = spl_ce_ArrayIterator;
1045
1046
0
  if (ZEND_NUM_ARGS() == 0) {
1047
0
    return; /* nothing to do */
1048
0
  }
1049
1050
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|AlC", &array, &ar_flags, &ce_get_iterator) == FAILURE) {
1051
0
    RETURN_THROWS();
1052
0
  }
1053
1054
0
  intern = Z_SPLARRAY_P(object);
1055
1056
0
  if (ZEND_NUM_ARGS() > 2) {
1057
0
    intern->ce_get_iterator = ce_get_iterator;
1058
0
  }
1059
1060
0
  ar_flags &= ~SPL_ARRAY_INT_MASK;
1061
1062
0
  spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
1063
0
}
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
0
{
1155
0
  HashTable *aht = spl_array_get_hash_table(intern);
1156
0
  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
0
  } else {
1170
0
    return zend_hash_num_elements(aht);
1171
0
  }
1172
0
} /* }}} */
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
0
{
1196
0
  spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1197
1198
0
  ZEND_PARSE_PARAMETERS_NONE();
1199
1200
0
  RETURN_LONG(spl_array_object_count_elements_helper(intern));
1201
0
} /* }}} */
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
0
{
1329
0
  zval *object = ZEND_THIS;
1330
0
  spl_array_object *intern = Z_SPLARRAY_P(object);
1331
1332
0
  char *buf;
1333
0
  size_t buf_len;
1334
0
  const unsigned char *p, *s;
1335
0
  php_unserialize_data_t var_hash;
1336
0
  zval *members, *zflags, *array;
1337
0
  zend_long flags;
1338
1339
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
1340
0
    RETURN_THROWS();
1341
0
  }
1342
1343
0
  if (buf_len == 0) {
1344
0
    return;
1345
0
  }
1346
1347
0
  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
0
  s = p = (const unsigned char*)buf;
1354
0
  PHP_VAR_UNSERIALIZE_INIT(var_hash);
1355
1356
0
  if (*p!= 'x' || *++p != ':') {
1357
0
    goto outexcept;
1358
0
  }
1359
0
  ++p;
1360
1361
0
  zflags = var_tmp_var(&var_hash);
1362
0
  if (!php_var_unserialize(zflags, &p, s + buf_len, &var_hash) || Z_TYPE_P(zflags) != IS_LONG) {
1363
0
    goto outexcept;
1364
0
  }
1365
1366
0
  --p; /* for ';' */
1367
0
  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
0
  if (*p != ';') {
1374
0
    goto outexcept;
1375
0
  }
1376
0
  ++p;
1377
1378
0
  if (flags & SPL_ARRAY_IS_SELF) {
1379
    /* If IS_SELF is used, the flags are not followed by an array/object */
1380
0
    intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1381
0
    intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1382
0
    zval_ptr_dtor(&intern->array);
1383
0
    ZVAL_UNDEF(&intern->array);
1384
0
  } else {
1385
0
    if (*p!='a' && *p!='O' && *p!='C' && *p!='r') {
1386
0
      goto outexcept;
1387
0
    }
1388
1389
0
    array = var_tmp_var(&var_hash);
1390
0
    if (!php_var_unserialize(array, &p, s + buf_len, &var_hash)
1391
0
        || (Z_TYPE_P(array) != IS_ARRAY && Z_TYPE_P(array) != IS_OBJECT)) {
1392
0
      goto outexcept;
1393
0
    }
1394
1395
0
    intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1396
0
    intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1397
1398
0
    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
0
    } else {
1404
0
      spl_array_set_array(object, intern, array, 0L, true);
1405
0
    }
1406
1407
0
    if (*p != ';') {
1408
0
      goto outexcept;
1409
0
    }
1410
0
    ++p;
1411
0
  }
1412
1413
  /* members */
1414
0
  if (*p!= 'm' || *++p != ':') {
1415
0
    goto outexcept;
1416
0
  }
1417
0
  ++p;
1418
1419
0
  members = var_tmp_var(&var_hash);
1420
0
  if (!php_var_unserialize(members, &p, s + buf_len, &var_hash) || Z_TYPE_P(members) != IS_ARRAY) {
1421
0
    goto outexcept;
1422
0
  }
1423
1424
  /* copy members */
1425
0
  object_properties_load(&intern->std, Z_ARRVAL_P(members));
1426
1427
  /* done reading $serialized */
1428
0
  PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1429
0
  return;
1430
1431
0
outexcept:
1432
0
  PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1433
0
  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
0
  RETURN_THROWS();
1435
1436
0
} /* }}} */
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
0
{
1479
0
  spl_array_object *intern = Z_SPLARRAY_P(ZEND_THIS);
1480
0
  HashTable *data;
1481
0
  zval *flags_zv, *storage_zv, *members_zv, *iterator_class_zv;
1482
0
  zend_long flags;
1483
1484
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
1485
0
    RETURN_THROWS();
1486
0
  }
1487
1488
0
  flags_zv          = zend_hash_index_find(data, 0);
1489
0
  storage_zv        = zend_hash_index_find(data, 1);
1490
0
  members_zv        = zend_hash_index_find(data, 2);
1491
0
  iterator_class_zv = zend_hash_index_find(data, 3);
1492
1493
0
  if (!flags_zv || !storage_zv || !members_zv ||
1494
0
      Z_TYPE_P(flags_zv) != IS_LONG || Z_TYPE_P(members_zv) != IS_ARRAY ||
1495
0
      (iterator_class_zv && (Z_TYPE_P(iterator_class_zv) != IS_NULL &&
1496
0
        Z_TYPE_P(iterator_class_zv) != IS_STRING))) {
1497
0
    zend_throw_exception(spl_ce_UnexpectedValueException,
1498
0
      "Incomplete or ill-typed serialization data", 0);
1499
0
    RETURN_THROWS();
1500
0
  }
1501
1502
0
  flags = Z_LVAL_P(flags_zv);
1503
0
  intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1504
0
  intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1505
1506
0
  if (flags & SPL_ARRAY_IS_SELF) {
1507
0
    zval_ptr_dtor(&intern->array);
1508
0
    ZVAL_UNDEF(&intern->array);
1509
0
  } else {
1510
0
    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
0
      zend_throw_exception(spl_ce_InvalidArgumentException, "Passed variable is not an array or object", 0);
1513
0
      RETURN_THROWS();
1514
0
    }
1515
0
    spl_array_set_array(ZEND_THIS, intern, storage_zv, 0L, true);
1516
0
  }
1517
1518
0
  object_properties_load(&intern->std, Z_ARRVAL_P(members_zv));
1519
0
  if (EG(exception)) {
1520
0
    RETURN_THROWS();
1521
0
  }
1522
1523
0
  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
0
}
1543
/* }}} */
1544
1545
/* {{{ */
1546
PHP_METHOD(ArrayObject, __debugInfo)
1547
0
{
1548
0
  ZEND_PARSE_PARAMETERS_NONE();
1549
1550
0
  RETURN_ARR(spl_array_get_debug_info(Z_OBJ_P(ZEND_THIS)));
1551
0
} /* }}} */
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
0
{
1561
0
  uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
1562
1563
0
  zend_hash_move_forward_ex(aht, pos_ptr);
1564
0
  if (spl_array_is_object(intern)) {
1565
0
    return spl_array_skip_protected(intern, aht);
1566
0
  } else {
1567
0
    return zend_hash_has_more_elements_ex(aht, pos_ptr);
1568
0
  }
1569
0
} /* }}} */
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
0
{
1581
0
  zval_ptr_dtor(&iter->data);
1582
0
}
1583
/* }}} */
1584
1585
static zend_result spl_array_it_valid(zend_object_iterator *iter) /* {{{ */
1586
0
{
1587
0
  spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1588
0
  HashTable *aht = spl_array_get_hash_table(object);
1589
0
  return zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, object));
1590
0
}
1591
/* }}} */
1592
1593
static zval *spl_array_it_get_current_data(zend_object_iterator *iter) /* {{{ */
1594
0
{
1595
0
  spl_array_iterator *array_iter = (spl_array_iterator*)iter;
1596
0
  spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1597
0
  HashTable *aht = spl_array_get_hash_table(object);
1598
0
  zval *data = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, object));
1599
0
  if (data && Z_TYPE_P(data) == IS_INDIRECT) {
1600
0
    data = Z_INDIRECT_P(data);
1601
0
  }
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
0
  if (array_iter->by_ref
1605
0
   && Z_TYPE_P(data) != IS_REFERENCE
1606
0
   && Z_TYPE(object->array) == IS_OBJECT
1607
0
   && !(object->ar_flags & (SPL_ARRAY_IS_SELF|SPL_ARRAY_USE_OTHER))) {
1608
0
    zend_string *key;
1609
0
    zend_hash_get_current_key_ex(aht, &key, NULL, spl_array_get_pos_ptr(aht, object));
1610
0
    zend_class_entry *ce = Z_OBJCE(object->array);
1611
0
    zend_property_info *prop_info = zend_get_property_info(ce, key, true);
1612
0
    ZEND_ASSERT(prop_info != ZEND_WRONG_PROPERTY_INFO);
1613
0
    if (EXPECTED(prop_info != NULL) && ZEND_TYPE_IS_SET(prop_info->type)) {
1614
0
      if (prop_info->flags & ZEND_ACC_READONLY) {
1615
0
        zend_throw_error(NULL,
1616
0
          "Cannot acquire reference to readonly property %s::$%s",
1617
0
          ZSTR_VAL(prop_info->ce->name), ZSTR_VAL(key));
1618
0
        return NULL;
1619
0
      }
1620
0
      ZVAL_NEW_REF(data, data);
1621
0
      ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(data), prop_info);
1622
0
    }
1623
0
  }
1624
0
  return data;
1625
0
}
1626
/* }}} */
1627
1628
static void spl_array_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
1629
0
{
1630
0
  spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1631
0
  HashTable *aht = spl_array_get_hash_table(object);
1632
0
  zend_hash_get_current_key_zval_ex(aht, key, spl_array_get_pos_ptr(aht, object));
1633
0
}
1634
/* }}} */
1635
1636
static void spl_array_it_move_forward(zend_object_iterator *iter) /* {{{ */
1637
0
{
1638
0
  spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1639
0
  HashTable *aht = spl_array_get_hash_table(object);
1640
0
  spl_array_next_ex(object, aht);
1641
0
}
1642
/* }}} */
1643
1644
static void spl_array_rewind(spl_array_object *intern) /* {{{ */
1645
0
{
1646
0
  HashTable *aht = spl_array_get_hash_table(intern);
1647
1648
0
  if (intern->ht_iter == (uint32_t)-1) {
1649
0
    spl_array_get_pos_ptr(aht, intern);
1650
0
  } else {
1651
0
    zend_hash_internal_pointer_reset_ex(aht, spl_array_get_pos_ptr(aht, intern));
1652
0
    spl_array_skip_protected(intern, aht);
1653
0
  }
1654
0
}
1655
/* }}} */
1656
1657
static void spl_array_it_rewind(zend_object_iterator *iter) /* {{{ */
1658
0
{
1659
0
  spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1660
0
  spl_array_rewind(object);
1661
0
}
1662
/* }}} */
1663
1664
static HashTable *spl_array_it_get_gc(zend_object_iterator *iter, zval **table, int *n)
1665
0
{
1666
0
  *n = 1;
1667
0
  *table = &iter->data;
1668
0
  return NULL;
1669
0
}
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
0
{
1685
0
  spl_array_iterator *iterator = emalloc(sizeof(spl_array_iterator));
1686
0
  zend_iterator_init(&iterator->it);
1687
1688
0
  ZVAL_OBJ_COPY(&iterator->it.data, Z_OBJ_P(object));
1689
0
  iterator->it.funcs = &spl_array_it_funcs;
1690
0
  iterator->by_ref = by_ref;
1691
1692
0
  return &iterator->it;
1693
0
}
1694
/* }}} */
1695
1696
/* {{{ Constructs a new array iterator from an array or object. */
1697
PHP_METHOD(ArrayIterator, __construct)
1698
0
{
1699
0
  zval *object = ZEND_THIS;
1700
0
  spl_array_object *intern;
1701
0
  zval *array;
1702
0
  zend_long ar_flags = 0;
1703
1704
0
  if (ZEND_NUM_ARGS() == 0) {
1705
0
    return; /* nothing to do */
1706
0
  }
1707
1708
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|Al", &array, &ar_flags) == FAILURE) {
1709
0
    RETURN_THROWS();
1710
0
  }
1711
1712
0
  intern = Z_SPLARRAY_P(object);
1713
1714
0
  ar_flags &= ~SPL_ARRAY_INT_MASK;
1715
1716
0
  spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
1717
0
}
1718
/* }}} */
1719
1720
/* {{{ Rewind array back to the start */
1721
PHP_METHOD(ArrayIterator, rewind)
1722
0
{
1723
0
  zval *object = ZEND_THIS;
1724
0
  spl_array_object *intern = Z_SPLARRAY_P(object);
1725
1726
0
  ZEND_PARSE_PARAMETERS_NONE();
1727
1728
0
  spl_array_rewind(intern);
1729
0
}
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
0
{
1763
0
  zval *object = ZEND_THIS;
1764
0
  spl_array_object *intern = Z_SPLARRAY_P(object);
1765
0
  zval *entry;
1766
0
  HashTable *aht = spl_array_get_hash_table(intern);
1767
1768
0
  ZEND_PARSE_PARAMETERS_NONE();
1769
1770
0
  if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1771
0
    RETURN_NULL();
1772
0
  }
1773
0
  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
0
  RETURN_COPY_DEREF(entry);
1780
0
}
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
0
{
1803
0
  zval *object = ZEND_THIS;
1804
0
  spl_array_object *intern = Z_SPLARRAY_P(object);
1805
0
  HashTable *aht = spl_array_get_hash_table(intern);
1806
1807
0
  ZEND_PARSE_PARAMETERS_NONE();
1808
1809
0
  spl_array_next_ex(intern, aht);
1810
0
}
1811
/* }}} */
1812
1813
/* {{{ Check whether array contains more entries */
1814
PHP_METHOD(ArrayIterator, valid)
1815
0
{
1816
0
  zval *object = ZEND_THIS;
1817
0
  spl_array_object *intern = Z_SPLARRAY_P(object);
1818
0
  HashTable *aht = spl_array_get_hash_table(intern);
1819
1820
0
  ZEND_PARSE_PARAMETERS_NONE();
1821
1822
0
  RETURN_BOOL(zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS);
1823
0
}
1824
/* }}} */
1825
1826
/*** RecursiveArrayIterator methods ***/
1827
1828
/* {{{ Check whether current element has children (e.g. is an array) */
1829
PHP_METHOD(RecursiveArrayIterator, hasChildren)
1830
0
{
1831
0
  zval *object = ZEND_THIS, *entry;
1832
0
  spl_array_object *intern = Z_SPLARRAY_P(object);
1833
0
  HashTable *aht = spl_array_get_hash_table(intern);
1834
1835
0
  ZEND_PARSE_PARAMETERS_NONE();
1836
1837
0
  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
0
  if (Z_TYPE_P(entry) == IS_INDIRECT) {
1842
0
    entry = Z_INDIRECT_P(entry);
1843
0
  }
1844
1845
0
  ZVAL_DEREF(entry);
1846
0
  RETURN_BOOL(Z_TYPE_P(entry) == IS_ARRAY || (Z_TYPE_P(entry) == IS_OBJECT && (intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) == 0));
1847
0
}
1848
/* }}} */
1849
1850
static void spl_instantiate_child_arg(zend_class_entry *pce, zval *retval, zval *arg1, zval *arg2) /* {{{ */
1851
0
{
1852
0
  object_init_ex(retval, pce);
1853
0
  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
0
  new_intern->is_child = true;
1859
1860
  /* find the bucket of parent object. */
1861
0
  new_intern->bucket = (Bucket *)((char *)(arg1) - XtOffsetOf(Bucket, val));;
1862
0
  zend_call_known_instance_method_with_2_params(pce->constructor, Z_OBJ_P(retval), NULL, arg1, arg2);
1863
0
}
1864
/* }}} */
1865
1866
/* {{{ Create a sub iterator for the current element (same class as $this) */
1867
PHP_METHOD(RecursiveArrayIterator, getChildren)
1868
0
{
1869
0
  zval *object = ZEND_THIS, *entry, flags;
1870
0
  spl_array_object *intern = Z_SPLARRAY_P(object);
1871
0
  HashTable *aht = spl_array_get_hash_table(intern);
1872
1873
0
  ZEND_PARSE_PARAMETERS_NONE();
1874
1875
0
  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
0
  if (Z_TYPE_P(entry) == IS_INDIRECT) {
1880
0
    entry = Z_INDIRECT_P(entry);
1881
0
  }
1882
1883
0
  ZVAL_DEREF(entry);
1884
0
  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
0
  ZVAL_LONG(&flags, intern->ar_flags);
1894
0
  spl_instantiate_child_arg(Z_OBJCE_P(ZEND_THIS), return_value, entry, &flags);
1895
0
}
1896
/* }}} */
1897
1898
/* {{{ PHP_MINIT_FUNCTION(spl_array) */
1899
PHP_MINIT_FUNCTION(spl_array)
1900
2
{
1901
2
  spl_ce_ArrayObject = register_class_ArrayObject(zend_ce_aggregate, zend_ce_arrayaccess, zend_ce_serializable, zend_ce_countable);
1902
2
  spl_ce_ArrayObject->create_object = spl_array_object_new;
1903
2
  spl_ce_ArrayObject->default_object_handlers = &spl_handler_ArrayObject;
1904
1905
2
  memcpy(&spl_handler_ArrayObject, &std_object_handlers, sizeof(zend_object_handlers));
1906
1907
2
  spl_handler_ArrayObject.offset = XtOffsetOf(spl_array_object, std);
1908
1909
2
  spl_handler_ArrayObject.clone_obj = spl_array_object_clone;
1910
2
  spl_handler_ArrayObject.read_dimension = spl_array_read_dimension;
1911
2
  spl_handler_ArrayObject.write_dimension = spl_array_write_dimension;
1912
2
  spl_handler_ArrayObject.unset_dimension = spl_array_unset_dimension;
1913
2
  spl_handler_ArrayObject.has_dimension = spl_array_has_dimension;
1914
2
  spl_handler_ArrayObject.count_elements = spl_array_object_count_elements;
1915
1916
2
  spl_handler_ArrayObject.get_properties_for = spl_array_get_properties_for;
1917
2
  spl_handler_ArrayObject.get_gc = spl_array_get_gc;
1918
2
  spl_handler_ArrayObject.read_property = spl_array_read_property;
1919
2
  spl_handler_ArrayObject.write_property = spl_array_write_property;
1920
2
  spl_handler_ArrayObject.get_property_ptr_ptr = spl_array_get_property_ptr_ptr;
1921
2
  spl_handler_ArrayObject.has_property = spl_array_has_property;
1922
2
  spl_handler_ArrayObject.unset_property = spl_array_unset_property;
1923
1924
2
  spl_handler_ArrayObject.compare = spl_array_compare_objects;
1925
2
  spl_handler_ArrayObject.free_obj = spl_array_object_free_storage;
1926
1927
2
  spl_ce_ArrayIterator = register_class_ArrayIterator(spl_ce_SeekableIterator, zend_ce_arrayaccess, zend_ce_serializable, zend_ce_countable);
1928
2
  spl_ce_ArrayIterator->create_object = spl_array_object_new;
1929
2
  spl_ce_ArrayIterator->default_object_handlers = &spl_handler_ArrayObject;
1930
2
  spl_ce_ArrayIterator->get_iterator = spl_array_get_iterator;
1931
1932
2
  spl_ce_RecursiveArrayIterator = register_class_RecursiveArrayIterator(spl_ce_ArrayIterator, spl_ce_RecursiveIterator);
1933
2
  spl_ce_RecursiveArrayIterator->create_object = spl_array_object_new;
1934
2
  spl_ce_RecursiveArrayIterator->get_iterator = spl_array_get_iterator;
1935
1936
2
  return SUCCESS;
1937
2
}
1938
/* }}} */