10000 bug #20377 [Console] Fix infinite loop on missing input (chalasr) · symfony/symfony@74e65e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 74e65e1

Browse files
committed
bug #20377 [Console] Fix infinite loop on missing input (chalasr)
This PR was merged into the 2.7 branch. Discussion ---------- [Console] Fix infinite loop on missing input | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20277 | License | MIT | Doc PR | n/a This fixes the infinite loop occurring when no input is provided for a question which has a validator and no max attempts (`null`), e.g. when using `SymfonyStyle::ask()` which automatically adds a validator. Commits ------- e64de1e [Console] Fix infinite loop on missing input
2 parents 4eb003b + e64de1e commit 74e65e1

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Exception;
13+
14+
/**
15+
* @author Jérôme Tamarelle <jerome@tamarelle.net>
16+
*/
17+
class RuntimeException extends \RuntimeException
18+
{
19+
}

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
1818
use Symfony\Component\Console\Question\Question;
1919
use Symfony\Component\Console\Question\ChoiceQuestion;
20+
use Symfony\Component\Console\Exception\RuntimeException;
2021

2122
/**
2223
* The QuestionHelper class provides helpers to interact with the user.
@@ -134,7 +135,7 @@ public function doAsk(OutputInterface $output, Question $question)
134135
if (false === $ret) {
135136
$ret = fgets($inputStream, 4096);
136137
if (false === $ret) {
137-
throw new \RuntimeException('Aborted');
138+
throw new RuntimeException('Aborted');
138139
}
139140
$ret = trim($ret);
140141
}
@@ -354,7 +355,7 @@ private function getHiddenResponse(OutputInterface $output, $inputStream)
354355
shell_exec(sprintf('stty %s', $sttyMode));
355356

356357
if (false === $value) {
357-
throw new \RuntimeException('Aborted');
358+
throw new RuntimeException('Aborted');
358359
}
359360

360361
$value = trim($value);
@@ -372,7 +373,7 @@ private function getHiddenResponse(OutputInterface $output, $inputStream)
372373
return $value;
373374
}
374375

375-
throw new \RuntimeException('Unable to hide the response.');
376+
throw new RuntimeException('Unable to hide the response.');
376377
}
377378

378379
/**
@@ -397,6 +398,8 @@ private function validateAttempts($interviewer, OutputInterface $output, Questio
397398

398399
try {
399400
return call_user_func($question->getValidator(), $interviewer());
401+
} catch (RuntimeException $e) {
402+
throw $e;
400403
} catch (\Exception $error) {
401404
}
402405
}

src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,37 @@ public function testChoiceOutputFormattingQuestionForUtf8Keys()
402402
$dialog->ask($this->createInputInterfaceMock(), $output, $question);
403403
}
404404

405+
/**
406+
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
407+
* @expectedExceptionMessage Aborted
408+
*/
409+
public function testAskThrowsExceptionOnMissingInput()
410+
{
411+
$dialog = new QuestionHelper();
412+
$dialog->setInputStream($this->getInputStream(''));
413+
414+
$dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), new Question('What\'s your name?'));
415+
}
416+
417+
/**
418+
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
419+
* @expectedExceptionMessage Aborted
420+
*/
421+
public function testAskThrowsExceptionOnMissingInputWithValidator()
422+
{
423+
$dialog = new QuestionHelper();
424+
$dialog->setInputStream($this->getInputStream(''));
425+
426+
$question = new Question('What\'s your name?');
427+
$question->setValidator(function () {
428+
if (!$value) {
429+
throw new \Exception('A value is required.');
430+
}
431+
});
432+
433+
$dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
434+
}
435+
405436
protected function getInputStream($input)
406437
{
407438
$stream = fopen('php://memory', 'r+', false);

src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ public function testAskEscapeLabel()
101101
$this->assertOutputContains('Do you want a \?', $output);
102102
}
103103

104+
/**
105+
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
106+
* @expectedExceptionMessage Aborted
107+
*/
108+
public function testAskThrowsExceptionOnMissingInput()
109+
{
110+
$dialog = new SymfonyQuestionHelper();
111+
112+
$dialog->setInputStream($this->getInputStream(''));
113+
$dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), new Question('What\'s your name?'));
114+
}
115+
104116
protected function getInputStream($input)
105117
{
106118
$stream = fopen('php://memory', 'r+', false);

0 commit comments

Comments
 (0)
0