php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Patch getarraycopy-skip-object-protected-private-properties for SPL related Bug #55211

Patch version 2011-07-25 06:33 UTC

Return to Bug #55211 | Download this patch
This patch renders other patches obsolete

Obsolete patches:

Patch Revisions:

Developer: laruence@php.net

Index: ext/spl/spl_array.c
===================================================================
--- ext/spl/spl_array.c	(revision 313657)
+++ ext/spl/spl_array.c	(working copy)
@@ -719,10 +719,39 @@
 SPL_METHOD(Array, getArrayCopy)
 {
 	zval *object = getThis(), *tmp;
+	HashTable *origin;
 	spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
-    
-    array_init(return_value);
-	zend_hash_copy(HASH_OF(return_value), spl_array_get_hash_table(intern, 0 TSRMLS_CC), (copy_ctor_func_t) zval_add_ref, &tmp, sizeof(zval*));
+
+	origin = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
+
+	array_init(return_value);
+
+	if (Z_TYPE_P(intern->array) == IS_OBJECT
+			&& zend_hash_num_elements(origin)) {
+		char *key;
+		long  key_len, num_key;
+		zval **ppzval;
+		HashPosition pos;
+		for (zend_hash_internal_pointer_reset_ex(origin, &pos);
+				zend_hash_get_current_data_ex(origin, (void **)&ppzval, &pos) == SUCCESS;
+				zend_hash_move_forward_ex(origin, &pos)) { 
+			switch (zend_hash_get_current_key_ex(origin, &key, (uint *)&key_len, &num_key, 0, &pos)) {
+				case HASH_KEY_IS_STRING:
+					if (!key_len || key[0]) {
+						/* skip protected/private properties */
+						zval_add_ref(ppzval);
+						zend_hash_update(Z_ARRVAL_P(return_value), key, key_len, ppzval, sizeof(zval *), NULL);
+					}
+					break;
+				case HASH_KEY_IS_LONG:
+					zval_add_ref(ppzval);
+					zend_hash_index_update(Z_ARRVAL_P(return_value), num_key, ppzval, sizeof(zval *), NULL);
+					break;
+			}			
+		}
+	} else {
+		zend_hash_copy(Z_ARRVAL_P(return_value), origin, (copy_ctor_func_t) zval_add_ref, &tmp, sizeof(zval*));
+	}
 } /* }}} */
 
 static HashTable *spl_array_get_properties(zval *object TSRMLS_DC) /* {{{ */
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Apr 02 00:01:23 2020 UTC