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

[ErrorHandler] Never throw on warnings triggered by assert() and set assert.exception=1 in Debug::enable() #35645

Merged
merged 1 commit into from Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/ErrorHandler/Debug.php
Expand Up @@ -29,6 +29,11 @@ public static function enable(): ErrorHandler
ini_set('display_errors', 1);
}

ini_set('zend.assertions', 1);
ini_set('assert.active', 1);
ini_set('assert.warning', 0);
ini_set('assert.exception', 1);

DebugClassLoader::enable();

return ErrorHandler::register(new ErrorHandler(new BufferingLogger(), true));
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/ErrorHandler/ErrorHandler.php
Expand Up @@ -413,6 +413,11 @@ public function handleError(int $type, string $message, string $file, int $line)
$throw = $this->thrownErrors & $type & $level;
$type &= $level | $this->screamedErrors;

// Never throw on warnings triggered by assert()
if (E_WARNING === $type && 'a' === $message[0] && 0 === strncmp($message, 'assert(): ', 10)) {
$throw = 0;
}

if (!$type || (!$log && !$throw)) {
return !$silenced && $type && $log;
}
Expand Down
35 changes: 35 additions & 0 deletions src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php
Expand Up @@ -615,4 +615,39 @@ public function errorHandlerWhenLoggingProvider(): iterable
}
}
}

public function testAssertQuietEval()
{
$ini = [
ini_set('zend.assertions', 1),
ini_set('assert.active', 1),
ini_set('assert.bail', 0),
ini_set('assert.warning', 1),
ini_set('assert.callback', null),
ini_set('assert.exception', 0),
];

$logger = new BufferingLogger();
$handler = new ErrorHandler($logger);
$handler = ErrorHandler::register($handler);

try {
\assert(false);
} finally {
restore_error_handler();
restore_exception_handler();

ini_set('zend.assertions', $ini[0]);
ini_set('assert.active', $ini[1]);
ini_set('assert.bail', $ini[2]);
ini_set('assert.warning', $ini[3]);
ini_set('assert.callback', $ini[4]);
ini_set('assert.exception', $ini[5]);
}

$logs = $logger->cleanLogs();

$this->assertSame('warning', $logs[0][0]);
$this->assertSame('Warning: assert(): assert(false) failed', $logs[0][1]);
}
}