Skip to content
Closed
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
45 changes: 45 additions & 0 deletions Zend/tests/bug53432.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--TEST--
Bug #53432: Assignment via string index access on an empty string converts to array
--FILE--
<?php

$str = '';
var_dump($str[0] = 'a');
var_dump($str);

$str = '';
var_dump($str[5] = 'a');
var_dump($str);

$str = '';
var_dump($str[-1] = 'a');
var_dump($str);

$str = '';
var_dump($str['foo'] = 'a');
var_dump($str);

$str = '';
try {
var_dump($str[] = 'a');
} catch (Error $e) {
echo "Error: {$e->getMessage()}\n";
}
var_dump($str);

?>
--EXPECTF--
string(1) "a"
string(1) "a"
string(1) "a"
string(6) " a"

Warning: Illegal string offset: -1 in %s on line %d
NULL
string(0) ""

Warning: Illegal string offset 'foo' in %s on line %d
string(1) "a"
string(1) "a"
Error: [] operator not supported for strings
string(0) ""
41 changes: 9 additions & 32 deletions Zend/tests/indexing_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ foreach ($testvalues as $testvalue) {
}
echo "\n*** Indexing - Testing reference assignment with key ***\n";

$testvalues=array(null, 0, 1, true, false,'',0.1,array());
$testvalues=array(null, 0, 1, true, false,0.1,array());

foreach ($testvalues as $testvalue) {
$testvalue['foo']=&$array;
var_dump ($testvalue);
}
echo "*** Indexing - Testing value assignment no key ***\n";
$array=array(1);
$testvalues=array(null, 0, 1, true, false,'',0.1,array());
$testvalues=array(null, 0, 1, true, false,0.1,array());

foreach ($testvalues as $testvalue) {
$testvalue[]=$array;
var_dump ($testvalue);
}
echo "\n*** Indexing - Testing reference assignment no key ***\n";

$testvalues=array(null, 0, 1, true, false,'',0.1,array());
$testvalues=array(null, 0, 1, true, false,0.1,array());

foreach ($testvalues as $testvalue) {
$testvalue[]=&$array;
Expand Down Expand Up @@ -63,13 +63,11 @@ array(1) {
int(1)
}
}
array(1) {
["foo"]=>
array(1) {
[0]=>
int(1)
}
}

Warning: Illegal string offset 'foo' in %s on line %d

Notice: Array to string conversion in %s on line %d
string(1) "A"

Warning: Illegal string offset 'foo' in %s on line %d

Expand Down Expand Up @@ -110,13 +108,6 @@ array(1) {
int(1)
}
}
array(1) {
["foo"]=>
&array(1) {
[0]=>
int(1)
}
}

Warning: Cannot use a scalar value as an array in %s on line %d
float(0.1)
Expand Down Expand Up @@ -151,13 +142,6 @@ array(1) {
int(1)
}
}
array(1) {
[0]=>
array(1) {
[0]=>
int(1)
}
}

Warning: Cannot use a scalar value as an array in %s on line %d
float(0.1)
Expand Down Expand Up @@ -193,13 +177,6 @@ array(1) {
int(1)
}
}
array(1) {
[0]=>
&array(1) {
[0]=>
int(1)
}
}

Warning: Cannot use a scalar value as an array in %s on line %d
float(0.1)
Expand All @@ -211,4 +188,4 @@ array(1) {
}
}

Done
Done
38 changes: 16 additions & 22 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -2323,31 +2323,25 @@ ZEND_VM_C_LABEL(try_assign_dim_array):
zend_assign_to_object_dim(UNEXPECTED(RETURN_VALUE_USED(opline)) ? EX_VAR(opline->result.var) : NULL, object_ptr, property_name, OP_DATA_TYPE, (opline+1)->op1, execute_data);
FREE_OP2();
} else if (EXPECTED(Z_TYPE_P(object_ptr) == IS_STRING)) {
if (EXPECTED(Z_STRLEN_P(object_ptr) != 0)) {
if (OP2_TYPE == IS_UNUSED) {
zend_throw_error(NULL, "[] operator not supported for strings");
FREE_UNFETCHED_OP_DATA();
FREE_OP1_VAR_PTR();
HANDLE_EXCEPTION();
} else {
zend_long offset;

dim = GET_OP2_ZVAL_PTR(BP_VAR_R);
offset = zend_fetch_string_offset(object_ptr, dim, BP_VAR_W);
FREE_OP2();
value = GET_OP_DATA_ZVAL_PTR_DEREF(BP_VAR_R);
zend_assign_to_string_offset(object_ptr, offset, value, (UNEXPECTED(RETURN_VALUE_USED(opline)) ? EX_VAR(opline->result.var) : NULL));
FREE_OP_DATA();
}
if (OP2_TYPE == IS_UNUSED) {
zend_throw_error(NULL, "[] operator not supported for strings");
FREE_UNFETCHED_OP_DATA();
FREE_OP1_VAR_PTR();
HANDLE_EXCEPTION();
} else {
zval_ptr_dtor_nogc(object_ptr);
ZEND_VM_C_LABEL(assign_dim_convert_to_array):
ZVAL_NEW_ARR(object_ptr);
zend_hash_init(Z_ARRVAL_P(object_ptr), 8, NULL, ZVAL_PTR_DTOR, 0);
ZEND_VM_C_GOTO(try_assign_dim_array);
zend_long offset;

dim = GET_OP2_ZVAL_PTR(BP_VAR_R);
offset = zend_fetch_string_offset(object_ptr, dim, BP_VAR_W);
FREE_OP2();
value = GET_OP_DATA_ZVAL_PTR_DEREF(BP_VAR_R);
zend_assign_to_string_offset(object_ptr, offset, value, (UNEXPECTED(RETURN_VALUE_USED(opline)) ? EX_VAR(opline->result.var) : NULL));
FREE_OP_DATA();
}
} else if (EXPECTED(Z_TYPE_P(object_ptr) <= IS_FALSE)) {
ZEND_VM_C_GOTO(assign_dim_convert_to_array);
ZVAL_NEW_ARR(object_ptr);
zend_hash_init(Z_ARRVAL_P(object_ptr), 8, NULL, ZVAL_PTR_DTOR, 0);
ZEND_VM_C_GOTO(try_assign_dim_array);
} else if (OP1_TYPE == IS_VAR && UNEXPECTED(Z_ISERROR_P(object_ptr))) {
ZEND_VM_C_GOTO(assign_dim_clean);
} else {
Expand Down
Loading