Skip to content

Commit 5a7f9af

Browse files
committed
Merge branch 'PHP-7.3' into PHP-7.4
* PHP-7.3: Detect self-addition of array more accurately Deindirect source elements in zend_hash_merge
2 parents 9a2e5cf + 3c4dd73 commit 5a7f9af

File tree

4 files changed

+44
-14
lines changed

4 files changed

+44
-14
lines changed

Zend/tests/array_add_indirect.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Array addition should not add INDIRECT elements
3+
--FILE--
4+
<?php
5+
6+
$x = 1;
7+
$ary = ['y' => 1];
8+
$ary += $GLOBALS;
9+
var_dump($ary['x']);
10+
$x = 2;
11+
var_dump($ary['x']);
12+
13+
?>
14+
--EXPECT--
15+
int(1)
16+
int(1)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Add $GLOBALS to itself
3+
--FILE--
4+
<?php
5+
$GLOBALS += $GLOBALS;
6+
$x = $GLOBALS + $GLOBALS;
7+
?>
8+
===DONE===
9+
--EXPECT--
10+
===DONE===

Zend/zend_hash.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,7 +2124,7 @@ ZEND_API void ZEND_FASTCALL zend_hash_merge(HashTable *target, HashTable *source
21242124
{
21252125
uint32_t idx;
21262126
Bucket *p;
2127-
zval *t;
2127+
zval *t, *s;
21282128

21292129
IS_CONSISTENT(source);
21302130
IS_CONSISTENT(target);
@@ -2133,18 +2133,20 @@ ZEND_API void ZEND_FASTCALL zend_hash_merge(HashTable *target, HashTable *source
21332133
if (overwrite) {
21342134
for (idx = 0; idx < source->nNumUsed; idx++) {
21352135
p = source->arData + idx;
2136-
if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2137-
if (UNEXPECTED(Z_TYPE(p->val) == IS_INDIRECT) &&
2138-
UNEXPECTED(Z_TYPE_P(Z_INDIRECT(p->val)) == IS_UNDEF)) {
2139-
continue;
2136+
s = &p->val;
2137+
if (UNEXPECTED(Z_TYPE_P(s) == IS_INDIRECT)) {
2138+
s = Z_INDIRECT_P(s);
2139+
}
2140+
if (UNEXPECTED(Z_TYPE_P(s) == IS_UNDEF)) {
2141+
continue;
21402142
}
21412143
if (p->key) {
2142-
t = _zend_hash_add_or_update_i(target, p->key, &p->val, HASH_UPDATE | HASH_UPDATE_INDIRECT);
2144+
t = _zend_hash_add_or_update_i(target, p->key, s, HASH_UPDATE | HASH_UPDATE_INDIRECT);
21432145
if (pCopyConstructor) {
21442146
pCopyConstructor(t);
21452147
}
21462148
} else {
2147-
t = zend_hash_index_update(target, p->h, &p->val);
2149+
t = zend_hash_index_update(target, p->h, s);
21482150
if (pCopyConstructor) {
21492151
pCopyConstructor(t);
21502152
}
@@ -2153,18 +2155,20 @@ ZEND_API void ZEND_FASTCALL zend_hash_merge(HashTable *target, HashTable *source
21532155
} else {
21542156
for (idx = 0; idx < source->nNumUsed; idx++) {
21552157
p = source->arData + idx;
2156-
if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2157-
if (UNEXPECTED(Z_TYPE(p->val) == IS_INDIRECT) &&
2158-
UNEXPECTED(Z_TYPE_P(Z_INDIRECT(p->val)) == IS_UNDEF)) {
2159-
continue;
2158+
s = &p->val;
2159+
if (UNEXPECTED(Z_TYPE_P(s) == IS_INDIRECT)) {
2160+
s = Z_INDIRECT_P(s);
2161+
}
2162+
if (UNEXPECTED(Z_TYPE_P(s) == IS_UNDEF)) {
2163+
continue;
21602164
}
21612165
if (p->key) {
2162-
t = _zend_hash_add_or_update_i(target, p->key, &p->val, HASH_ADD | HASH_UPDATE_INDIRECT);
2166+
t = _zend_hash_add_or_update_i(target, p->key, s, HASH_ADD | HASH_UPDATE_INDIRECT);
21632167
if (t && pCopyConstructor) {
21642168
pCopyConstructor(t);
21652169
}
21662170
} else {
2167-
t = zend_hash_index_add(target, p->h, &p->val);
2171+
t = zend_hash_index_add(target, p->h, s);
21682172
if (t && pCopyConstructor) {
21692173
pCopyConstructor(t);
21702174
}

Zend/zend_operators.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ ZEND_API zend_string* ZEND_FASTCALL zval_try_get_string_func(zval *op) /* {{{ */
934934

935935
static zend_never_inline void ZEND_FASTCALL add_function_array(zval *result, zval *op1, zval *op2) /* {{{ */
936936
{
937-
if ((result == op1) && (result == op2)) {
937+
if (result == op1 && Z_ARR_P(op1) == Z_ARR_P(op2)) {
938938
/* $a += $a */
939939
return;
940940
}

0 commit comments

Comments
 (0)