Coverage Report

Created: 2026-06-02 06:39

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