From f1d49ae003110f513b68bc75c67e752346db5f78 Mon Sep 17 00:00:00 2001 From: everzet Date: Thu, 6 Jun 2013 16:18:02 +0100 Subject: [PATCH 1/2] [Console] Ensure proper exit code on Exception - Prevent negative and 0 exit code from being used on exception - Prevent 255+ exit code from being used Fixes #8183, #8202 --- src/Symfony/Component/Console/Application.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index ad6b85368f5a3..a2bd9390fb1b5 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -113,15 +113,11 @@ public function run(InputInterface $input = null, OutputInterface $output = null } else { $this->renderException($e, $output); } - $statusCode = $e->getCode(); - $statusCode = is_numeric($statusCode) && $statusCode ? (int) $statusCode : 1; + $statusCode = max(1, min(254, intval($e->getCode()))); } if ($this->autoExit) { - if ($statusCode > 255) { - $statusCode = 255; - } // @codeCoverageIgnoreStart exit($statusCode); // @codeCoverageIgnoreEnd From 80f7e688c1cbb371a165cb87ac547a0a845af7a6 Mon Sep 17 00:00:00 2001 From: everzet Date: Thu, 6 Jun 2013 16:30:25 +0100 Subject: [PATCH 2/2] [Console] Add missing test cases for application exit code --- .../Console/Tests/ApplicationTest.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index 6a51742f62f7c..2615e8e02e6e4 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -531,6 +531,51 @@ public function testRunReturnsExitCodeOneForExceptionCodeZero() $this->assertSame(1, $exitCode, '->run() returns exit code 1 when exception code is 0'); } + public function testRunReturnsExitCodeOneForExceptionCodeNegative() + { + $exception = new \Exception('', -20); + + $application = $this->getMock('Symfony\Component\Console\Application', array('doRun')); + $application->setAutoExit(false); + $application->expects($this->once()) + ->method('doRun') + ->will($this->throwException($exception)); + + $exitCode = $application->run(new ArrayInput(array()), new NullOutput()); + + $this->assertSame(1, $exitCode, '->run() returns exit code 1 when exception code is negative'); + } + + public function testRunReturnsExitCodeOneForExceptionCodeNonInteger() + { + $exception = new \Exception('', false); + + $application = $this->getMock('Symfony\Component\Console\Application', array('doRun')); + $application->setAutoExit(false); + $application->expects($this->once()) + ->method('doRun') + ->will($this->throwException($exception)); + + $exitCode = $application->run(new ArrayInput(array()), new NullOutput()); + + $this->assertSame(1, $exitCode, '->run() returns exit code 1 when exception code is not integer'); + } + + public function testRunReturnsExitCode254ForExceptionCodeHigher254() + { + $exception = new \Exception('', 404); + + $application = $this->getMock('Symfony\Component\Console\Application', array('doRun')); + $application->setAutoExit(false); + $application->expects($this->once()) + ->method('doRun') + ->will($this->throwException($exception)); + + $exitCode = $application->run(new ArrayInput(array()), new NullOutput()); + + $this->assertSame(254, $exitCode, '->run() returns exit code 254 when exception code is 254+'); + } + /** * @expectedException \LogicException * @dataProvider getAddingAlreadySetDefinitionElementData