ID:               43484
 User updated by:  robin_fernandes at uk dot ibm dot com
 Reported By:      robin_fernandes at uk dot ibm dot com
 Status:           Bogus
 Bug Type:         Scripting Engine problem
 Operating System: Windows
 PHP Version:      5.3CVS-2007-12-03 (snap)
 New Comment:

Hi Johannes,

Thanks for looking into this. I understand your comment, but I can't
quite see how it relates to the behaviour described in the bug report.

The bug describes how, using call_user_func_array(), one can force a
function defined with pass-by-value args to take those args by reference
- even with call time pass by reference DISABLED in php.ini.

Note that this odd behaviour is now documented (thanks to bug 43079).
But the documentation writer suggested raising a functional bug, because
the behaviour is potentially dangerous.


Here is a simplified example:
<?php
 function byValue($a) {
   $a = 'This assignment should not affect global scope!';
 }

 $args = array('original');
 $ref = &$args[0]; //dummy reference - remove to change behaviour
 
 call_user_func_array('byValue', $args);
 var_dump($args); 
?>

---( Expected Output )---
array(1) {
  [0]=>
  string(8) "original"
}

---( Actual Output )---
array(1) {
  [0]=>
  &string(47) "This assignment should not affect global scope!"
}


Previous Comments:
------------------------------------------------------------------------

[2008-03-08 22:56:39] [EMAIL PROTECTED]

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

 $args = array('original.a', 'original.b');

This puts a copy of the value 'original.a' into that array.

------------------------------------------------------------------------

[2007-12-03 14:09:12] robin_fernandes at uk dot ibm dot com

Description:
------------
This issue was originally raised as documentation bug 43079. Raising as
Scripting Engine problem as suggested by vrana at php dot net.

The pass-by-value/pass-by-ref behaviour of call_user_func_array() is
not intuitive (see the user contributed notes on the documentation page:
http://php.net/call_user_func_array ).

It appears that the way in which an argument is passed depends not on
the target function signature, but rather on whether its entry in
$param_arr is referenced or not.

Specifically, it is possible to force an argument to be passed by
reference to a function which expects a pass-by-value argument, even
with call time pass by reference DISABLED in php.ini.

Reproduced on php5.3 and php6 snaps on Windows.

Reproduce code:
---------------
<?php
 function byRef(&$a, &$b) {
        $a = 'changed.a';
        $b = 'changed.b';
 }
 
 function byVal($a, $b) {
        $a = 'changed.a';
        $b = 'changed.b';
 }

 //Currently, this forces a pass-by-ref function to take args by val:
 $args = array('original.a', 'original.b');
 call_user_func_array('byRef', $args);
 var_dump($args);

 //Currently, this forces a pass-by-val function to take 1 arg by ref.
 //This works even with call-time pass-by-ref DISABLED in php.ini.
 $args = array('original.a', 'original.b');
 $ref = &$args[0];
 call_user_func_array('byVal', $args);
 var_dump($args); 
?>

Expected result:
----------------
array(2) {
  [0]=>
  string(9) "changed.a"
  [1]=>
  string(9) "changed.b"
}
array(2) {
  [0]=>
  &string(10) "original.a"
  [1]=>
  string(10) "original.b"
}

Actual result:
--------------
array(2) {
  [0]=>
  string(10) "original.a"
  [1]=>
  string(10) "original.b"
}
array(2) {
  [0]=>
  &string(9) "changed.a"
  [1]=>
  string(10) "original.b"
}


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=43484&edit=1

Reply via email to