Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Objects cannot access his private attributes anymore while handling error, in PHP 7.2
#!/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());
@juan-leon

This comment has been minimized.

Copy link
Owner Author

juan-leon commented Sep 26, 2018

There is a simpler version showing the same issue here: https://gist.github.com/juan-leon/cd52f9c9e7394d0897764fb2197e624b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.