Coverage Report

Created: 2025-07-23 06:33

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