From bb8696195eab152a82911257a35e86f793b0d07c Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Fri, 13 Nov 2026 00:54:14 +0100 Subject: [PATCH] Keep exit codes in the valid range and honour an error exit code of 0 Only exit codes above 255 were limited. A negative return value or error code was passed to exit() as is and wrapped around, -256 even to 0, reporting success. Map every value outside 0-255 to 255. ApplicationErrorEvent::getExitCode() used ?: and so ignored setExitCode(0), while CommandErrorEvent checks for null. Use the null check in both. Co-Authored-By: Claude Opus 5.5 --- docs/overview.md | 12 +++ src/Application.php | 3 +- src/Event/ApplicationErrorEvent.php | 2 +- tests/ApplicationTest.php | 117 ++++++++++++++++++++++ tests/Event/ApplicationErrorEventTest.php | 72 +++++++++++++ 5 files changed, 204 insertions(+), 2 deletions(-) create mode 100644 tests/Event/ApplicationErrorEventTest.php diff --git a/docs/overview.md b/docs/overview.md index 59eeb9f2..b2e960af 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -110,6 +110,18 @@ there are several console specific events: - `ConsoleEvents::COMMAND_ERROR` is triggered when the application catches any `Throwable` object directly from a command, this can be used to integrate extra error handling/reporting tools - `ConsoleEvents::TERMINATE` is triggered immediately before the process is completed, developers may listen for this event to perform any actions required at the end of the process +#### Exit codes + +When the application exits on its own (the default, see `setAutoExit()`), it exits with the command's return value as +modified by `TerminateEvent::setExitCode()`. A value outside the range 0 to 255 becomes 255; a negative value would +otherwise wrap around and could even end up as 0, i.e. success. + +When a `Throwable` is not handled, the exit code is taken from the `APPLICATION_ERROR` event: the value given to +`ApplicationErrorEvent::setExitCode()`, otherwise the code of the `Throwable` or 1 if that is 0. An exit code of 0 is +turned into 1 here, since the application did fail. A listener for `COMMAND_ERROR` can call +`CommandErrorEvent::setExitCode(0)` to treat the error as handled; any other value is written to the code of the +`Throwable` (as in Symfony), which is how it reaches the `APPLICATION_ERROR` event. + ## Things to know before you build on this **Use `$defaultName`, not `#[AsCommand]`.** Symfony removed the static property in 7.0, but diff --git a/src/Application.php b/src/Application.php index caca180d..d249e34b 100644 --- a/src/Application.php +++ b/src/Application.php @@ -446,7 +446,8 @@ public function execute() } if ($this->shouldAutoExit() && isset($exitCode)) { - $exitCode = $exitCode > 255 ? 255 : $exitCode; + // Exit codes outside 0-255 are not portable, a negative one could even wrap to 0 (success) + $exitCode = $exitCode < 0 || $exitCode > 255 ? 255 : $exitCode; $this->close($exitCode); } } diff --git a/src/Event/ApplicationErrorEvent.php b/src/Event/ApplicationErrorEvent.php index 2127325f..1e571e89 100644 --- a/src/Event/ApplicationErrorEvent.php +++ b/src/Event/ApplicationErrorEvent.php @@ -73,7 +73,7 @@ public function getError(): \Throwable */ public function getExitCode(): int { - return $this->exitCode ?: (\is_int($this->error->getCode()) && $this->error->getCode() !== 0 ? $this->error->getCode() : 1); + return $this->exitCode ?? (\is_int($this->error->getCode()) && $this->error->getCode() !== 0 ? $this->error->getCode() : 1); } /** diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index a7841d62..db4968a0 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -460,6 +460,123 @@ protected function doExecute(InputInterface $input, OutputInterface $output): in $this->assertSame(1, $app->getExitCode()); } + /** + * Data provider for testExitCodeIsLimitedToTheValidRange + * + * @return array + */ + public static function dataExitCodes(): array + { + // Args: Command return value, expected exit code + return [ + 'Success' => [0, 0], + 'Highest valid code' => [255, 255], + 'Above the range' => [256, 255], + 'Negative' => [-1, 255], + 'Wrapping to 0' => [-256, 255], + ]; + } + + #[DataProvider('dataExitCodes')] + public function testExitCodeIsLimitedToTheValidRange(int $returnValue, int $expected) + { + $command = new class ($returnValue) extends AbstractCommand { + protected static $defaultName = 'exit-code'; + + private $returnValue; + + public function __construct(int $returnValue) + { + $this->returnValue = $returnValue; + + parent::__construct(); + } + + protected function doExecute(InputInterface $input, OutputInterface $output): int + { + return $this->returnValue; + } + }; + + $input = new ArrayInput( + [ + 'command' => 'exit-code', + ] + ); + $output = new NullOutput(); + + $app = $this->buildMockClosingApplication($input, $output); + + $app->addCommand($command); + $app->setAutoExit(true); + $app->execute(); + + $this->assertSame($expected, $app->getExitCode()); + } + + public function testErrorExitCodeIsLimitedToTheValidRange() + { + $command = new class () extends AbstractCommand { + protected static $defaultName = 'exception'; + + protected function doExecute(InputInterface $input, OutputInterface $output): int + { + throw new \Exception('Testing', -256); + } + }; + + $input = new ArrayInput( + [ + 'command' => 'exception', + ] + ); + $output = new NullOutput(); + + $app = $this->buildMockClosingApplication($input, $output); + + $app->addCommand($command); + $app->setAutoExit(true); + $app->execute(); + + $this->assertSame(255, $app->getExitCode()); + } + + public function testCommandErrorExitCodeIsSetByEventListener() + { + $command = new class () extends AbstractCommand { + protected static $defaultName = 'exception'; + + protected function doExecute(InputInterface $input, OutputInterface $output): int + { + throw new \Exception('Testing', 3); + } + }; + + $dispatcher = new Dispatcher(); + $dispatcher->addListener( + ConsoleEvents::COMMAND_ERROR, + function (CommandErrorEvent $event) { + $event->setExitCode(119); + } + ); + + $input = new ArrayInput( + [ + 'command' => 'exception', + ] + ); + $output = new NullOutput(); + + $app = $this->buildMockClosingApplication($input, $output); + + $app->setDispatcher($dispatcher); + $app->addCommand($command); + $app->setAutoExit(true); + $app->execute(); + + $this->assertSame(119, $app->getExitCode()); + } + public function testCommandIsNotExecutedIfEventSkipsCommand() { $command = new class () extends AbstractCommand { diff --git a/tests/Event/ApplicationErrorEventTest.php b/tests/Event/ApplicationErrorEventTest.php new file mode 100644 index 00000000..5ff3acda --- /dev/null +++ b/tests/Event/ApplicationErrorEventTest.php @@ -0,0 +1,72 @@ + [ApplicationErrorEvent::class], + 'CommandErrorEvent' => [CommandErrorEvent::class], + ]; + } + + #[DataProvider('dataEventClasses')] + public function testTheExitCodeDefaultsToTheErrorCode(string $class) + { + $event = new $class(new \RuntimeException('Testing', 42), new Application()); + + $this->assertSame(42, $event->getExitCode()); + } + + #[DataProvider('dataEventClasses')] + public function testTheExitCodeIsOneForAnErrorWithoutCode(string $class) + { + $event = new $class(new \RuntimeException('Testing'), new Application()); + + $this->assertSame(1, $event->getExitCode()); + } + + #[DataProvider('dataEventClasses')] + public function testTheExitCodeCanBeSet(string $class) + { + $event = new $class(new \RuntimeException('Testing', 42), new Application()); + $event->setExitCode(3); + + $this->assertSame(3, $event->getExitCode()); + } + + #[DataProvider('dataEventClasses')] + public function testTheExitCodeCanBeSetToZero(string $class) + { + $event = new $class(new \RuntimeException('Testing', 42), new Application()); + $event->setExitCode(0); + + $this->assertSame(0, $event->getExitCode()); + } +}