Skip to content

[Debug] work-around https://bugs.php.net/61272 #11099

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

Merged
merged 1 commit into from
Jun 12, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions src/Symfony/Component/Debug/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class ExceptionHandler
private $debug;
private $charset;
private $handler;
private $caughtOutput = 0;
private $caughtBuffer;
private $caughtLength;

public function __construct($debug = true, $charset = 'UTF-8')
{
Expand Down Expand Up @@ -94,29 +95,25 @@ public function handle(\Exception $exception)
return;
}

$caughtOutput = 0;
$caughtLength = $this->caughtLength = 0;

$this->caughtOutput = false;
ob_start(array($this, 'catchOutput'));
$this->failSafeHandle($exception);
if (false === $this->caughtOutput) {
ob_end_clean();
while (null === $this->caughtBuffer && ob_end_flush()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it a loop now ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As locally I don't want to know anything about failSafeHandle(), several ob_start() could have been started somewhere else. This loop just closes all these potentially opened buffers. Fail-safe and defensive coding practice.

// Empty loop, everything is in the condition
}
if (isset($this->caughtOutput[0])) {
if (isset($this->caughtBuffer[0])) {
ob_start(array($this, 'cleanOutput'));
echo $this->caughtOutput;
$caughtOutput = ob_get_length();
echo $this->caughtBuffer;
$caughtLength = ob_get_length();
}
$this->caughtOutput = 0;
$this->caughtBuffer = null;

try {
call_user_func($this->handler, $exception);

if ($caughtOutput) {
$this->caughtOutput = $caughtOutput;
}
$this->caughtLength = $caughtLength;
} catch (\Exception $e) {
if (!$caughtOutput) {
if (!$caughtLength) {
// All handlers failed. Let PHP handle that now.
throw $exception;
}
Expand Down Expand Up @@ -388,7 +385,7 @@ private function formatArgs(array $args)
*/
public function catchOutput($buffer)
{
$this->caughtOutput = $buffer;
$this->caughtBuffer = $buffer;

return '';
}
Expand All @@ -398,9 +395,9 @@ public function catchOutput($buffer)
*/
public function cleanOutput($buffer)
{
if ($this->caughtOutput) {
if ($this->caughtLength) {
// use substr_replace() instead of substr() for mbstring overloading resistance
$cleanBuffer = substr_replace($buffer, '', 0, $this->caughtOutput);
$cleanBuffer = substr_replace($buffer, '', 0, $this->caughtLength);
if (isset($cleanBuffer[0])) {
$buffer = $cleanBuffer;
}
Expand Down