Skip to content

Commit

Permalink
[LLDB] Fix 'std::out_of_range' crashing bug when file name completion…
Browse files Browse the repository at this point in the history
… using file path.

When I run a lldb command that uses filename completion, if I enter a string
that is not only a filename but also a string with a non-file name string added,
such as "./" that is relative path string , it will crash as soon as I press the
[Tab] key. For example, debugging an executable file named "hello" that is
compiled from a file named "hello.c" , and I’ll put a breakpoint on line 3 of
hello.c.

```
$ lldb ./hello
(lldb) breakpoint set --file hello.c --line 3
```

This is not a problem, but if I set "--file ./hello."  and then press [Tab] key
to complete file name, lldb crashes.

```
$ lldb ./hello
(lldb) breakpoint set --file ./hello.terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr: __pos (which is 8) > this->size() (which is 7)
```

The crash was caused because substr() (in lldb/source/Host/common/Editline.cpp)
cut out string which size is user's input string from the completion string.

I modified the code that erase the user's intput string from current line and
then add the completion string.

Differential Revision: https://reviews.llvm.org/D108817
  • Loading branch information
iam-i authored and Teemperor committed Aug 30, 2021
1 parent 21d11c8 commit ffcf571
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lldb/source/Host/common/Editline.cpp
Expand Up @@ -1006,11 +1006,11 @@ unsigned char Editline::TabCommand(int ch) {
switch (completion.GetMode()) {
case CompletionMode::Normal: {
std::string to_add = completion.GetCompletion();
to_add = to_add.substr(request.GetCursorArgumentPrefix().size());
// Terminate the current argument with a quote if it started with a quote.
if (!request.GetParsedLine().empty() && request.GetParsedArg().IsQuoted())
to_add.push_back(request.GetParsedArg().GetQuoteChar());
to_add.push_back(' ');
el_deletestr(m_editline, request.GetCursorArgumentPrefix().size());
el_insertstr(m_editline, to_add.c_str());
// Clear all the autosuggestion parts if the only single space can be completed.
if (to_add == " ")
Expand Down
3 changes: 3 additions & 0 deletions lldb/test/API/iohandler/completion/Makefile
@@ -0,0 +1,3 @@
C_SOURCES := main.c

include Makefile.rules
10 changes: 9 additions & 1 deletion lldb/test/API/iohandler/completion/TestIOHandlerCompletion.py
Expand Up @@ -20,7 +20,8 @@ class IOHandlerCompletionTest(PExpectTest):
@expectedFailureAll(oslist=['freebsd'], bugnumber='llvm.org/pr49408')
@skipIf(oslist=["linux"], archs=["arm", "aarch64"])
def test_completion(self):
self.launch(dimensions=(100,500))
self.build()
self.launch(dimensions=(100,500), executable=self.getBuildArtifact("a.out"))

# Start tab completion, go to the next page and then display all with 'a'.
self.child.send("\t\ta")
Expand Down Expand Up @@ -51,6 +52,13 @@ def test_completion(self):
self.child.send("\n")
self.expect_prompt()

# Complete a file path.
# FIXME: This should complete to './main.c' and not 'main.c'
self.child.send("breakpoint set --file ./main\t")
self.child.expect_exact("main.c")
self.child.send("\n")
self.expect_prompt()

# Start tab completion and abort showing more commands with 'n'.
self.child.send("\t")
self.child.expect_exact("More (Y/n/a)")
Expand Down

0 comments on commit ffcf571

Please sign in to comment.