Skip to content

Commit 4035a8e

Browse files
committed
Fixed bug #41372 (Internal pointer of source array resets during array copying)
Fixed bug #37715 (array pointers resetting on copy)
1 parent 78932ba commit 4035a8e

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ PHP NEWS
162162
integer as sections). (Tony)
163163
- Fixed bug #41433 (DBA: configure fails to include correct db.h for db4).
164164
(Jani)
165+
- Fixed bug #41372 (Internal pointer of source array resets during array
166+
copying). (Dmitry)
165167
- Fixed bug #41350 (my_thread_global_end() error during request shutdown
166168
on Windows). (Scott, Andrey)
167169
- Fixed bug #41127 (Memory leak in ldap_{first|next}_attribute functions).
@@ -172,6 +174,7 @@ PHP NEWS
172174
apache child die). (isk at ecommerce dot com, Gopal, Tony)
173175
- Fixed bug #39291 (ldap_sasl_bind() misses the sasl_authc_id parameter).
174176
(diafour at gmail dot com, Jani)
177+
- Fixed bug #37715 (array pointers resetting on copy). (Dmitry)
175178
- Fixed bugs #36796, #36918, #41371 (stream_set_blocking() does not work).
176179
(Jani)
177180
- Fixed bug #35981 (pdo-pgsql should not use pkg-config when not present).

Zend/tests/bug37715.phpt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Bug #37715 (array pointers resetting on copy)
3+
--FILE--
4+
<?php
5+
$a = array(
6+
'a' => array(
7+
'A', 'B', 'C', 'D',
8+
),
9+
'b' => array(
10+
'AA', 'BB', 'CC', 'DD',
11+
),
12+
);
13+
14+
// Set the pointer of $a to 'b' and the pointer of 'b' to 'CC'
15+
reset($a);
16+
next($a);
17+
next($a['b']);
18+
next($a['b']);
19+
next($a['b']);
20+
21+
var_dump(key($a['b']));
22+
foreach($a as $k => $d)
23+
{
24+
}
25+
// Alternatively $c = $a; and foreachloop removal will cause identical results.
26+
var_dump(key($a['b']));
27+
--EXPECT--
28+
int(3)
29+
int(3)

Zend/tests/bug41372.phpt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Bug #41372 Internal pointer of source array resets during array copying
3+
--FILE--
4+
<?php
5+
$Foo = array('val1', 'val2', 'val3');
6+
end($Foo);
7+
echo key($Foo),"\n";
8+
$MagicInternalPointerResetter = $Foo;
9+
echo key($Foo),"\n";
10+
?>
11+
--EXPECT--
12+
2
13+
2

Zend/zend_hash.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -771,12 +771,17 @@ ZEND_API void zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_fun
771771
{
772772
Bucket *p;
773773
void *new_entry;
774+
zend_bool setTargetPointer;
774775

775776
IS_CONSISTENT(source);
776777
IS_CONSISTENT(target);
777778

779+
setTargetPointer = !target->pInternalPointer;
778780
p = source->pListHead;
779781
while (p) {
782+
if (setTargetPointer && source->pInternalPointer == p) {
783+
target->pInternalPointer = NULL;
784+
}
780785
if (p->nKeyLength) {
781786
zend_hash_quick_update(target, p->arKey, p->nKeyLength, p->h, p->pData, size, &new_entry);
782787
} else {
@@ -787,7 +792,9 @@ ZEND_API void zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_fun
787792
}
788793
p = p->pListNext;
789794
}
790-
target->pInternalPointer = target->pListHead;
795+
if (!target->pInternalPointer) {
796+
target->pInternalPointer = target->pListHead;
797+
}
791798
}
792799

793800

0 commit comments

Comments
 (0)