Skip to content

WIP #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

WIP #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Zend/tests/gh10085_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
GH-10085: Assertion in add_function_array()
--FILE--
<?php
$i = [[], 0];
$ref = &$i;
$i[0] += $ref;
var_dump($i);
?>
--EXPECT--
array(2) {
[0]=>
array(2) {
[0]=>
array(0) {
}
[1]=>
int(0)
}
[1]=>
int(0)
}
25 changes: 25 additions & 0 deletions Zend/tests/gh10085_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
GH-10085: Assertion in add_function_array()
--FILE--
<?php
$tmp = [0];
unset($tmp[0]);
$i = [$tmp, 0];
unset($tmp);
$ref = &$i;
$i[0] += $ref;
var_dump($i);
?>
--EXPECT--
array(2) {
[0]=>
array(2) {
[0]=>
array(0) {
}
[1]=>
int(0)
}
[1]=>
int(0)
}
8 changes: 6 additions & 2 deletions Zend/zend_operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -1045,10 +1045,14 @@ static zend_never_inline void ZEND_FASTCALL add_function_array(zval *result, zva
}
if (result != op1) {
ZVAL_ARR(result, zend_array_dup(Z_ARR_P(op1)));
zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), zval_add_ref, 0);
} else {
SEPARATE_ARRAY(result);
zval tmp;
ZVAL_COPY_VALUE(&tmp, result);
SEPARATE_ARRAY(&tmp);
zend_hash_merge(Z_ARRVAL_P(&tmp), Z_ARRVAL_P(op2), zval_add_ref, 0);
ZVAL_COPY_VALUE(result, &tmp);
}
zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), zval_add_ref, 0);
}
/* }}} */

Expand Down
18 changes: 16 additions & 2 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -1154,12 +1154,23 @@ ZEND_VM_HANDLER(27, ZEND_ASSIGN_DIM_OP, VAR|CV, CONST|TMPVAR|UNUSED|NEXT|CV, OP)
zval *var_ptr;
zval *value, *container, *dim;
HashTable *ht;
zend_array *value_array;

SAVE_OPLINE();
container = GET_OP1_OBJ_ZVAL_PTR_PTR_UNDEF(BP_VAR_RW);

if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
ZEND_VM_C_LABEL(assign_dim_op_array):
value = get_op_data_zval_ptr_r((opline+1)->op1_type, (opline+1)->op1);
if (UNEXPECTED(Z_ISREF_P(value) && Z_TYPE_P(Z_REFVAL_P(value)) == IS_ARRAY && Z_ARRVAL_P(Z_REFVAL_P(value)) == Z_ARRVAL_P(container) && !(GC_FLAGS(Z_ARRVAL_P(container)) & GC_IMMUTABLE))) {
/* The binary OP would normally deref the reference, so an increase in RC would only be done later.
* We need to do this here already to do a correct array separation in case the value is related to the array.
* The only case where this would be problematic is when the container and value are the same array. */
value_array = Z_ARR_P(Z_REFVAL_P(value));
GC_ADDREF(value_array);
} else {
value_array = NULL;
}
SEPARATE_ARRAY(container);
ht = Z_ARRVAL_P(container);
ZEND_VM_C_LABEL(assign_dim_op_new_array):
Expand All @@ -1181,8 +1192,6 @@ ZEND_VM_C_LABEL(assign_dim_op_new_array):
}
}

value = get_op_data_zval_ptr_r((opline+1)->op1_type, (opline+1)->op1);

do {
if (OP2_TYPE != IS_UNUSED && UNEXPECTED(Z_ISREF_P(var_ptr))) {
zend_reference *ref = Z_REF_P(var_ptr);
Expand All @@ -1193,6 +1202,9 @@ ZEND_VM_C_LABEL(assign_dim_op_new_array):
}
}
zend_binary_op(var_ptr, var_ptr, value OPLINE_CC);
if (UNEXPECTED(value_array)) {
GC_DTOR_NO_REF(value_array);
}
} while (0);

if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
Expand Down Expand Up @@ -1232,6 +1244,8 @@ ZEND_VM_C_LABEL(assign_dim_op_new_array):
ZEND_VM_C_GOTO(assign_dim_op_ret_null);
}
}
value = get_op_data_zval_ptr_r((opline+1)->op1_type, (opline+1)->op1);
value_array = NULL;
ZEND_VM_C_GOTO(assign_dim_op_new_array);
} else {
dim = GET_OP2_ZVAL_PTR(BP_VAR_R);
Expand Down
Loading