Created
September 26, 2018 10:36
-
-
Save juan-leon/0c33d86b1bda88b6281fdcf451dc1192 to your computer and use it in GitHub Desktop.
Objects cannot access his private attributes anymore while handling error, in PHP 7.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php -n | |
<?php | |
ini_set("display_errors", 1); | |
error_reporting(E_ALL); | |
class Foo { | |
private $dummy1 = 1; | |
private $dummy2 = 2; | |
function unsetProperty() { | |
// This will create a "Undefined property" error when using reflection | |
// on that object and iterating over properties. This error is to be | |
// expected, and I am triggering it only to trigger the bug in the error | |
// handling. | |
unset($this->dummy2); | |
} | |
} | |
class ErrorHandler { | |
private $private; | |
function setup() { | |
set_error_handler( | |
function ($errno, $errstr, $errfile, $errline) { | |
$this->handleError($errno, $errstr, $errfile, $errline); | |
return true; | |
} | |
); | |
} | |
private function handleError($errno, $errstr, $errfile, $errline, $errmodule = null) { | |
echo "Dealing with error $errstr, $errfile, $errline\n"; | |
// This attribute is no longer accessible in this object. Same for other | |
// objects and their private attributes once we reach in this state. | |
$this->private; | |
echo "Never executed in PHP7.2; there is a fatal error in the line above\n"; | |
} | |
} | |
function reproduceBug(Foo $f) { | |
$f->unsetProperty(); | |
$exception = new Exception("Die you proof of concept"); | |
$frames = $exception->getTrace(); | |
// 1st stack frame contains the Foo object as 1st argument | |
$foo = $frames[0]['args'][0]; | |
$reflect = new ReflectionObject($foo); | |
$properties = $reflect->getProperties(); | |
foreach ($properties as $property) { | |
$property->setAccessible(true); | |
$propertyKey = $property->getName(); | |
// This will trigger an error for "dummy2" property | |
$value = $property->getValue($foo); | |
echo "property $propertyKey value is $value\n"; | |
} | |
echo "This line is not reached in PHP7.2; but it is reached in PHP 7.0\n"; | |
} | |
$errorHandler = new ErrorHandler(); | |
$errorHandler->setup(); | |
reproduceBug(new Foo()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is a simpler version showing the same issue here: https://gist.github.com/juan-leon/cd52f9c9e7394d0897764fb2197e624b