Skip to content
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

[BUG]: Issue with Phalcon\Http\Message\AbstractCommon::cloneInstance #15040

Closed
sergeyklay opened this issue May 13, 2020 · 1 comment · Fixed by #15041
Closed

[BUG]: Issue with Phalcon\Http\Message\AbstractCommon::cloneInstance #15040

sergeyklay opened this issue May 13, 2020 · 1 comment · Fixed by #15041
Assignees
Labels
bug A bug report status: medium Medium

Comments

@sergeyklay
Copy link
Contributor

Currently Phalcon\Http\Message\AbstractCommon::cloneInstance clones the passed object and directly modifies object's property:

final protected function cloneInstance(var element, string property) -> var
{
var newInstance;
let newInstance = clone this,
newInstance->{property} = element;
return newInstance;
}

This is not always correct because in some cases object's property is private. For example:

/**
* Returns the path of the URL
*
* @return string
*/
private path = "" { get };

There is a PoC on PHP just to show the problem:

<?php

abstract class AbstractCommon
{
    final protected function cloneInstance($element, string $property)
    {
        $newInstance             = clone $this;
        $newInstance->{$property} = $element;

        return $newInstance;
    }
}

final class Uri extends AbstractCommon
{
    private $path = "";

    public function withPath(string $path)
    {
        return $this->cloneInstance($path, "path");
    }
}

$test = new Uri();
$test->withPath('action/reaction');

Output:

PHP Fatal error:  Uncaught Error: Cannot access private property Uri::$path in /mnt/work/phalcon/cphalcon/manual.php:8
Stack trace:
#0 /mnt/work/phalcon/cphalcon/manual.php(20): AbstractCommon->cloneInstance('action/reaction', 'path')
#1 /mnt/work/phalcon/cphalcon/manual.php(25): Uri->withPath('action/reaction')
#2 {main}
  thrown in /mnt/work/phalcon/cphalcon/manual.php on line 8

Fatal error: Uncaught Error: Cannot access private property Uri::$path in /mnt/work/phalcon/cphalcon/manual.php:8
Stack trace:
#0 /mnt/work/phalcon/cphalcon/manual.php(20): AbstractCommon->cloneInstance('action/reaction', 'path')
#1 /mnt/work/phalcon/cphalcon/manual.php(25): Uri->withPath('action/reaction')
#2 {main}
  thrown in /mnt/work/phalcon/cphalcon/manual.php on line 8
@sergeyklay sergeyklay added the bug A bug report label May 13, 2020
@niden niden self-assigned this May 13, 2020
@niden niden added the 4.0.6 label May 13, 2020
@niden niden added the status: medium Medium label May 13, 2020
@niden niden mentioned this issue May 13, 2020
5 tasks
niden added a commit that referenced this issue May 14, 2020
@sergeyklay
Copy link
Contributor Author

Fixed in 4.0.x branch

niden added a commit that referenced this issue May 16, 2020
niden added a commit that referenced this issue May 31, 2020
@niden niden moved this to Released in Phalcon v5 Aug 25, 2022
@niden niden added this to Phalcon v5 Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: medium Medium
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants