Browse Source

fix #41351 (Invalid opcode with foreach ($a[] as $b))

PHAR_1_2
Antony Dovgal 19 years ago
parent
commit
ca43e24f55
  1. 14
      Zend/tests/bug41351.phpt
  2. 14
      Zend/tests/bug41351_2.phpt
  3. 14
      Zend/tests/bug41351_3.phpt
  4. 6
      Zend/zend_compile.c

14
Zend/tests/bug41351.phpt

@ -0,0 +1,14 @@
--TEST--
Bug #41351 (Invalid opcode with foreach ($a[] as $b))
--FILE--
<?php
$a = array();
foreach($a[] as $b) {
}
echo "Done\n";
?>
--EXPECTF--
Fatal error: Cannot use [] for reading in %s on line %d

14
Zend/tests/bug41351_2.phpt

@ -0,0 +1,14 @@
--TEST--
Bug #41351 (Invalid opcode with foreach ($a[] as $b)) - 2
--FILE--
<?php
$a = array();
foreach($a[]['test'] as $b) {
}
echo "Done\n";
?>
--EXPECTF--
Fatal error: Cannot use [] for reading in %s on line %d

14
Zend/tests/bug41351_3.phpt

@ -0,0 +1,14 @@
--TEST--
Bug #41351 (Invalid opcode with foreach ($a[] as $b)) - 3
--FILE--
<?php
$a = array();
foreach($a['test'][] as $b) {
}
echo "Done\n";
?>
--EXPECTF--
Fatal error: Cannot use [] for reading in %s on line %d

6
Zend/zend_compile.c

@ -4034,7 +4034,11 @@ void zend_do_foreach_cont(znode *foreach_token, znode *open_brackets_token, znod
/* Change "write context" into "read context" */
fetch->extended_value = 0; /* reset ZEND_FE_RESET_VARIABLE */
while (fetch != end) {
(--fetch)->opcode -= 3; /* FETCH_W -> FETCH_R */
--fetch;
if (fetch->opcode == ZEND_FETCH_DIM_W && fetch->op2.op_type == IS_UNUSED) {
zend_error(E_COMPILE_ERROR, "Cannot use [] for reading");
}
fetch->opcode -= 3; /* FETCH_W -> FETCH_R */
}
/* prevent double SWITCH_FREE */
zend_stack_top(&CG(foreach_copy_stack), (void **) &foreach_copy);

Loading…
Cancel
Save