bronson / Term-ShellUI

Perl module to support a command line environment with completion and history
http://search.cpan.org/search?query=Term%3A%3AShellUI
3 stars 1 forks source link

Infinite loop on tab completion with invalid escape backslash '\' #7

Open qistoph opened 3 years ago

qistoph commented 3 years ago

ShellUI hangs when completing a command that ends with in incomplete escape sequence; just a backslash.

Example:

perl -MTerm::ShellUI -e 'new Term::ShellUI()->run()'

Then enter \<tab>, ShellUI hangs and CPU usage soars to 100%

bronson commented 3 years ago

Thanks for the great bug report! I haven't needed Term::ShellUI in years so probably won't work on this any time soon. I am happy to review and merge a patch if you look into it.

qistoph commented 3 years ago

On inspection it seems it's an issue rather with Term::Readline.

It hangs on this line in Term::ShellUI https://github.com/bronson/Term-ShellUI/blob/master/lib/Term/ShellUI.pm#L788

qistoph commented 3 years ago

Ok, was maybe a little quick on closing the issue. This fix/work-around seems to fix the issue:

diff --git a/lib/Term/ShellUI.pm b/lib/Term/ShellUI.pm
index 7d36ba9..63fe7c5 100644
--- a/lib/Term/ShellUI.pm
+++ b/lib/Term/ShellUI.pm
@@ -1557,6 +1557,9 @@ sub completion_function
     my $twice = ($self->{completeline} eq $line);
     $self->{completeline} = $line;

+    $line =~ s/([^\\])\\$/\1/g; # Strip uneven backslash at end
+    $line =~ s/^\\$//g; # Strip if it's only backslash
+
     my($tokens, $tokno, $tokoff) = $self->{parser}->parse_line($line,
         messages=>0, cursorpos=>$cursor, fixclosequote=>1);
     return unless defined($tokens);

Found another issue, with a double backslash at the end though :-)