Bug #45937 [Com]: array_walk and array_walk_recursive can alter private/protected object variable

From: Date: Thu, 23 Jul 2015 08:54:25 +0000
Subject: Bug #45937 [Com]: array_walk and array_walk_recursive can alter private/protected object variable
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-194648@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=45937&edit=1

 ID:                 45937
 Comment by:         ricardo dot seromenho at gmail dot com
 Reported by:        skarab at versus-clash dot com
 Summary:            array_walk and array_walk_recursive can alter
                     private/protected object variable
 Status:             Closed
 Type:               Bug
 Package:            Arrays related
 Operating System:   Linux 2.6 / Windows XP
 PHP Version:        5.2.6
 Assigned To:        felipe
 Block user comment: N
 Private report:     N

 New Comment:

Hi. 

This is still happening, at least on PHP Version => 5.5.12


Test case
---------
class A
{
    private $b = 5;
}

function walking_objects(&$value) {
    if (is_object($value)) {
        array_walk_recursive($value, "walking_objects");
    } else {
        $value = 20;
    }
};

$a = array(new A(), 1, "c");
array_walk_recursive($a, "walking_objects");

print_r($a);

Expected Result:
Array
(
    [0] => A Object
        (
            [b:A:private] => 5
        )

    [1] => 20
    [2] => 20
)
or an exception. (but this result would be fine)

Actual Result:
Array
(
    [0] => A Object
        (
            [b:A:private] => 20
        )

    [1] => 20
    [2] => 20
)


Previous Comments:
------------------------------------------------------------------------
[2012-03-14 15:03:34] shurph at gmail dot com

In php 5.3.5 this problem enabled, too.

Details about environment:
PHP Version 5.3.5-1ubuntu7.7 with the Suhosin Patch 0.9.10
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans

Ubuntu 10.04
Linux localhost 2.6.38-13-generic-pae #56-Ubuntu SMP Tue Feb 14 14:32:30 UTC 2012 i686

------------------------------------------------------------------------
[2008-08-29 03:13:38] felipe@php.net

This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Fixed in 5_3 and HEAD. Now it works only with arrays.


Thanks.

------------------------------------------------------------------------
[2008-08-27 16:05:22] skarab at versus-clash dot com

Description:
------------
array_walk and array_walk_recursive can alter private and protected object / class variables outside
the scope of the object / class.

Reproduce code:
---------------
class Some_Class
{
    public $public = 'public string';
    protected $_protected = 'protected string';
    private $_private = 'private string';
}

function some_function(&$item, $key)
{
    $item = 'public access';
}

$Somme_Class = new Some_Class();
array_walk($Somme_Class, 'some_function');
echo '<pre>' . print_r($Somme_Class, true) . '</pre>';

$Another_Class = new Some_Class();
array_walk_recursive($Another_Class, 'some_function');
echo '<pre>' . print_r($Another_Class, true) . '</pre>';

Expected result:
----------------
Some_Class Object
(
    [public] => public access
    [_protected:protected] => protected string
    [_private:private] => protected string
)

Some_Class Object
(
    [public] => public access
    [_protected:protected] => protected string
    [_private:private] => private string
)

or an error message like : 
Fatal error: Cannot access protected property Some_Class::$_protected
or 
Warning: array_walk() expects parameter 1 to be array, object given

Actual result:
--------------
Some_Class Object
(
    [public] => public access
    [_protected:protected] => public access
    [_private:private] => public access
)

Some_Class Object
(
    [public] => public access
    [_protected:protected] => public access
    [_private:private] => public access
)



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



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


Thread (5 messages)

« previous php.bugs (#194648) next »