Skip to content

Commit 45623f2

Browse files
committed
Use try/finally to restore error handlers
Signed-off-by: Alexander M. Turek <[email protected]>
1 parent c1857b9 commit 45623f2

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

Firewall/ContextListener.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -298,29 +298,28 @@ protected function refreshUser(TokenInterface $token): ?TokenInterface
298298

299299
private function safelyUnserialize(string $serializedToken)
300300
{
301-
$e = $token = null;
301+
$token = null;
302302
$prevUnserializeHandler = ini_set('unserialize_callback_func', __CLASS__.'::handleUnserializeCallback');
303303
$prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) use (&$prevErrorHandler) {
304304
if (__FILE__ === $file) {
305-
throw new \ErrorException($msg, 0x37313bc, $type, $file, $line);
305+
throw new \ErrorException($msg, 0x37313BC, $type, $file, $line);
306306
}
307307

308308
return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
309309
});
310310

311311
try {
312312
$token = unserialize($serializedToken);
313-
} catch (\Throwable $e) {
314-
}
315-
restore_error_handler();
316-
ini_set('unserialize_callback_func', $prevUnserializeHandler);
317-
if ($e) {
318-
if (!$e instanceof \ErrorException || 0x37313bc !== $e->getCode()) {
313+
} catch (\ErrorException $e) {
314+
if (0x37313BC !== $e->getCode()) {
319315
throw $e;
320316
}
321317
if ($this->logger) {
322318
$this->logger->warning('Failed to unserialize the security token from the session.', ['key' => $this->sessionKey, 'received' => $serializedToken, 'exception' => $e]);
323319
}
320+
} finally {
321+
restore_error_handler();
322+
ini_set('unserialize_callback_func', $prevUnserializeHandler);
324323
}
325324

326325
return $token;
@@ -388,7 +387,7 @@ private static function hasUserChanged($originalUser, TokenInterface $refreshedT
388387
*/
389388
public static function handleUnserializeCallback(string $class)
390389
{
391-
throw new \ErrorException('Class not found: '.$class, 0x37313bc);
390+
throw new \ErrorException('Class not found: '.$class, 0x37313BC);
392391
}
393392

394393
/**

0 commit comments

Comments
 (0)