cli-menu icon indicating copy to clipboard operation
cli-menu copied to clipboard

Pressing backspace in an empty text input crashes the menu system

Open tbannister opened this issue 2 years ago • 8 comments

To duplicate: Open a new text input using v4.3.0 of cli-menu and press the backspace key until there the placeholder text has been deleted, then press it once more. Expected result: Nothing happens. Actual result: Fatal error: Uncaught TypeError: Argument 2 passed to PhpSchool\CliMenu\Input\InputIO::drawInput() must be of the type string, bool given, called in /.../vendor/php-school/cli-menu/src/Input/InputIO.php on line 80 and defined in /.../vendor/php-school/cli-menu/src/Input/InputIO.php on line 205 Also, after the crash the terminal window stops echoing input. Environment: CentOS 7.9, running PHP 7.4.20

Sample code: '$result = $menu->askText() ->setPromptText($prompt.' :') ->ask();'

I can replicate this behavior with the text input sample code in the examples directory.

tbannister avatar Jan 05 '23 19:01 tbannister

Certainly sounds like a bug, feel free to send a PR with a fix, otherwise I'm not quite sure when we'll get around to fixing. Regarding the echoing not working, we could maybe catch any errors or register a shutdown function to do similar to what \PhpSchool\CliMenu\CliMenu::tearDownTerminal does. What do you think?

AydinHassan avatar Jan 05 '23 19:01 AydinHassan

I've been looking into the root cause, and I've found it, before PHP 8, substr returns false in certain circumstances. The type of the return value from the substr call on line 78 of InputIO.php isn't checked. A relatively simple solution should work, something like: 'if (!is_string($inputValue)) { $inputValue = '' }'

And, yes it would probably be a good idea to have a shutdown function that restores the terminal.

tbannister avatar Jan 05 '23 19:01 tbannister

Sounds good to me :)

AydinHassan avatar Jan 05 '23 20:01 AydinHassan

#267

tbannister avatar Jan 05 '23 20:01 tbannister

Hmm. Actually, this is already non-issue on the dev-master branch, rather than the most recently published version. It's already been fixed there.

tbannister avatar Jan 05 '23 20:01 tbannister

Ok I've reverted it. I'll prepare a new release next week with the existing fix.

AydinHassan avatar Jan 05 '23 21:01 AydinHassan

In version 4.3 this problem still exists.

dvlpr91 avatar Feb 08 '23 04:02 dvlpr91

It's fixed in master, just not released

AydinHassan avatar Feb 08 '23 06:02 AydinHassan