@@ -108,20 +108,75 @@ static inline bool spl_array_is_object(spl_array_object *intern) /* {{{ */
108108}
109109/* }}} */
110110
111+ /* Returns the storage, separated from its other holders so that it can be written to. */
112+ static HashTable * spl_array_get_hash_table_for_write (spl_array_object * intern )
113+ {
114+ while (intern -> ar_flags & SPL_ARRAY_USE_OTHER ) {
115+ intern = Z_SPLARRAY_P (& intern -> array );
116+ }
117+ if (!(intern -> ar_flags & SPL_ARRAY_IS_SELF ) && Z_TYPE (intern -> array ) == IS_ARRAY ) {
118+ SEPARATE_ARRAY (& intern -> array );
119+ return Z_ARRVAL (intern -> array );
120+ }
121+ return spl_array_get_hash_table (intern );
122+ }
123+
111124static zend_result spl_array_skip_protected (spl_array_object * intern , HashTable * aht );
112125
113- static zend_never_inline void spl_array_create_ht_iter (HashTable * ht , spl_array_object * intern ) /* {{{ */
126+ static void spl_array_create_ht_iter (HashTable * ht , spl_array_object * intern ) /* {{{ */
114127{
115128 intern -> ht_iter = zend_hash_iterator_add (ht , zend_hash_get_current_pos (ht ));
116129 zend_hash_internal_pointer_reset_ex (ht , & EG (ht_iterators )[intern -> ht_iter ].pos );
117130 spl_array_skip_protected (intern , ht );
118131}
119132/* }}} */
120133
121- static zend_always_inline uint32_t * spl_array_get_pos_ptr (HashTable * ht , spl_array_object * intern ) /* {{{ */
134+ static void spl_array_update_ht_iter (HashTable * ht , spl_array_object * intern )
122135{
123- if (UNEXPECTED (intern -> ht_iter == (uint32_t )-1 )) {
136+ uint32_t idx = intern -> ht_iter ;
137+ HashPosition pos = EG (ht_iterators )[idx ].pos ;
138+ bool has_copy = false;
139+
140+ /* When the storage is separated, zend_array_dup() registers a copy of the
141+ * iterator on the new table, with its position adjusted to it. */
142+ for (uint32_t i = EG (ht_iterators )[idx ].next_copy ; i != idx ; i = EG (ht_iterators )[i ].next_copy ) {
143+ if (EG (ht_iterators )[i ].ht == ht ) {
144+ has_copy = true;
145+ break ;
146+ }
147+ }
148+
149+ /* Moves the registration to the new table, so that the previous one stops
150+ * tracking this iterator, and takes the position of the copy if there is one. */
151+ zend_hash_iterator_pos (idx , ht );
152+ if (!has_copy ) {
153+ /* The storage was replaced: the position is kept as is */
154+ EG (ht_iterators )[idx ].pos = pos ;
155+ }
156+ }
157+
158+ static zend_always_inline bool spl_array_ht_iter_is_on (const spl_array_object * intern , const HashTable * ht )
159+ {
160+ /* The registered table is compared rather than a cached pointer: it is
161+ * poisoned when the table is destroyed, so a new table allocated at the
162+ * same address cannot be mistaken for it. */
163+ return intern -> ht_iter != (uint32_t )-1 && EG (ht_iterators )[intern -> ht_iter ].ht == ht ;
164+ }
165+
166+ static zend_never_inline uint32_t * spl_array_get_pos_ptr_slow (HashTable * ht , spl_array_object * intern )
167+ {
168+ if (intern -> ht_iter == (uint32_t )-1 ) {
124169 spl_array_create_ht_iter (ht , intern );
170+ } else {
171+ spl_array_update_ht_iter (ht , intern );
172+ }
173+ return & EG (ht_iterators )[intern -> ht_iter ].pos ;
174+ }
175+
176+ static zend_always_inline uint32_t * spl_array_get_pos_ptr (HashTable * ht , spl_array_object * intern ) /* {{{ */
177+ {
178+ if (UNEXPECTED (!spl_array_ht_iter_is_on (intern , ht ))) {
179+ return spl_array_get_pos_ptr_slow (ht , intern );
125180 }
126181 return & EG (ht_iterators )[intern -> ht_iter ].pos ;
127182}
@@ -321,9 +376,9 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object
321376{
322377 zval * retval ;
323378 spl_hash_key key ;
324- HashTable * ht = spl_array_get_hash_table ( intern ) ;
379+ HashTable * ht ;
325380
326- if (!offset || Z_ISUNDEF_P (offset ) || ! ht ) {
381+ if (!offset || Z_ISUNDEF_P (offset )) {
327382 return & EG (uninitialized_zval );
328383 }
329384
@@ -338,6 +393,13 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object
338393 & EG (error_zval ) : & EG (uninitialized_zval );
339394 }
340395
396+ /* The table is fetched after get_hash_key(), whose errors may call a handler
397+ * that shares or replaces the storage. In write context, the returned zval
398+ * may be turned into a reference or written to. */
399+ ht = (type != BP_VAR_R && type != BP_VAR_IS )
400+ ? spl_array_get_hash_table_for_write (intern )
401+ : spl_array_get_hash_table (intern );
402+
341403 if (key .key ) {
342404 retval = zend_hash_find (ht , key .key );
343405 if (retval ) {
@@ -372,6 +434,8 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object
372434 break ;
373435 case BP_VAR_RW :
374436 zend_error (E_WARNING ,"Undefined array key \"%pS\"" , key .key );
437+ /* The error handler may have shared or replaced the storage */
438+ ht = spl_array_get_hash_table_for_write (intern );
375439 ZEND_FALLTHROUGH ;
376440 case BP_VAR_W : {
377441 zval value ;
@@ -393,6 +457,8 @@ static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object
393457 break ;
394458 case BP_VAR_RW :
395459 zend_error (E_WARNING , "Undefined array key " ZEND_LONG_FMT , key .h );
460+ /* The error handler may have shared or replaced the storage */
461+ ht = spl_array_get_hash_table_for_write (intern );
396462 ZEND_FALLTHROUGH ;
397463 case BP_VAR_W : {
398464 zval value ;
@@ -480,7 +546,7 @@ static void spl_array_write_dimension_ex(int check_inherited, zend_object *objec
480546
481547 Z_TRY_ADDREF_P (value );
482548 if (!offset || Z_TYPE_P (offset ) == IS_NULL ) {
483- ht = spl_array_get_hash_table (intern );
549+ ht = spl_array_get_hash_table_for_write (intern );
484550 if (UNEXPECTED (ht == intern -> sentinel_array )) {
485551 return ;
486552 }
@@ -494,7 +560,7 @@ static void spl_array_write_dimension_ex(int check_inherited, zend_object *objec
494560 return ;
495561 }
496562
497- ht = spl_array_get_hash_table (intern );
563+ ht = spl_array_get_hash_table_for_write (intern );
498564 if (UNEXPECTED (ht == intern -> sentinel_array )) {
499565 spl_hash_key_release (& key );
500566 return ;
@@ -534,7 +600,7 @@ static void spl_array_unset_dimension_ex(int check_inherited, zend_object *objec
534600 return ;
535601 }
536602
537- ht = spl_array_get_hash_table (intern );
603+ ht = spl_array_get_hash_table_for_write (intern );
538604 if (key .key ) {
539605 zval * data = zend_hash_find (ht , key .key );
540606 if (data ) {
@@ -592,14 +658,17 @@ static bool spl_array_has_dimension_ex(bool check_inherited, zend_object *object
592658 }
593659
594660 if (!value ) {
595- HashTable * ht = spl_array_get_hash_table ( intern ) ;
661+ HashTable * ht ;
596662 spl_hash_key key ;
597663
598664 if (get_hash_key (& key , intern , offset ) == FAILURE ) {
599665 zend_illegal_container_offset (object -> ce -> name , offset , BP_VAR_IS );
600666 return false;
601667 }
602668
669+ /* Fetched after get_hash_key(), whose errors may call a handler that replaces the storage */
670+ ht = spl_array_get_hash_table (intern );
671+
603672 if (key .key ) {
604673 tmp = zend_hash_find (ht , key .key );
605674 spl_hash_key_release (& key );
@@ -1562,7 +1631,9 @@ static zval *spl_array_it_get_current_data(zend_object_iterator *iter) /* {{{ */
15621631{
15631632 spl_array_iterator * array_iter = (spl_array_iterator * )iter ;
15641633 spl_array_object * object = Z_SPLARRAY_P (& iter -> data );
1565- HashTable * aht = spl_array_get_hash_table (object );
1634+ HashTable * aht = array_iter -> by_ref
1635+ ? spl_array_get_hash_table_for_write (object )
1636+ : spl_array_get_hash_table (object );
15661637 zval * data = zend_hash_get_current_data_ex (aht , spl_array_get_pos_ptr (aht , object ));
15671638 if (data && Z_TYPE_P (data ) == IS_INDIRECT ) {
15681639 data = Z_INDIRECT_P (data );
@@ -1705,7 +1776,7 @@ PHP_METHOD(ArrayIterator, seek)
17051776 zend_long opos , position ;
17061777 zval * object = ZEND_THIS ;
17071778 spl_array_object * intern = Z_SPLARRAY_P (object );
1708- HashTable * aht = spl_array_get_hash_table ( intern ) ;
1779+ HashTable * aht ;
17091780 int result ;
17101781
17111782 if (zend_parse_parameters (ZEND_NUM_ARGS (), "l" , & position ) == FAILURE ) {
@@ -1720,6 +1791,7 @@ PHP_METHOD(ArrayIterator, seek)
17201791
17211792 while (position -- > 0 && (result = spl_array_next (intern )) == SUCCESS );
17221793
1794+ aht = spl_array_get_hash_table (intern );
17231795 if (result == SUCCESS && zend_hash_has_more_elements_ex (aht , spl_array_get_pos_ptr (aht , intern )) == SUCCESS ) {
17241796 return ; /* ok */
17251797 }
0 commit comments