Skip to content

Commit cd344a4

Browse files
[LLDB] Fix completion of space-only lines in the REPL on Linux (llvm#83203)
modular/modular#1796 discovered that if you try to complete a space-only line in the REPL on Linux, LLDB crashes. I suspect that editline doesn't behave the same way on linux and on darwin, because I can't replicate this on darwin. Adding a boundary check in the completion code prevents the crash from happening.
1 parent 64422cf commit cd344a4

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

lldb/source/Host/common/Editline.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,8 +1029,11 @@ unsigned char Editline::TabCommand(int ch) {
10291029
case CompletionMode::Normal: {
10301030
std::string to_add = completion.GetCompletion();
10311031
// Terminate the current argument with a quote if it started with a quote.
1032-
if (!request.GetParsedLine().empty() && request.GetParsedArg().IsQuoted())
1032+
Args &parsedLine = request.GetParsedLine();
1033+
if (!parsedLine.empty() && request.GetCursorIndex() < parsedLine.size() &&
1034+
request.GetParsedArg().IsQuoted()) {
10331035
to_add.push_back(request.GetParsedArg().GetQuoteChar());
1036+
}
10341037
to_add.push_back(' ');
10351038
el_deletestr(m_editline, request.GetCursorArgumentPrefix().size());
10361039
el_insertstr(m_editline, to_add.c_str());

lldb/test/API/repl/clang/TestClangREPL.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import lldb
21
from lldbsuite.test.decorators import *
3-
from lldbsuite.test.lldbtest import *
42
from lldbsuite.test.lldbpexpect import PExpectTest
3+
from lldbsuite.test.lldbtest import *
54

65

76
class TestCase(PExpectTest):
@@ -17,13 +16,7 @@ def expect_repl(self, expr, substrs=[]):
1716
self.current_repl_line_number += 1
1817
self.child.expect_exact(str(self.current_repl_line_number) + ">")
1918

20-
# PExpect uses many timeouts internally and doesn't play well
21-
# under ASAN on a loaded machine..
22-
@skipIfAsan
23-
@skipIf(oslist=["linux"], archs=["arm", "aarch64"]) # Randomly fails on buildbot
24-
@skipIfEditlineSupportMissing
25-
def test_basic_completion(self):
26-
"""Test that we can complete a simple multiline expression"""
19+
def start_repl(self):
2720
self.build()
2821
self.current_repl_line_number = 1
2922

@@ -41,6 +34,14 @@ def test_basic_completion(self):
4134
self.child.send("expression --repl -l c --\n")
4235
self.child.expect_exact("1>")
4336

37+
# PExpect uses many timeouts internally and doesn't play well
38+
# under ASAN on a loaded machine..
39+
@skipIfAsan
40+
@skipIf(oslist=["linux"], archs=["arm", "aarch64"]) # Randomly fails on buildbot
41+
@skipIfEditlineSupportMissing
42+
def test_basic_completion(self):
43+
"""Test that we can complete a simple multiline expression"""
44+
self.start_repl()
4445
# Try evaluating a simple expression.
4546
self.expect_repl("3 + 3", substrs=["(int) $0 = 6"])
4647

@@ -54,3 +55,16 @@ def test_basic_completion(self):
5455
self.expect_repl("$persistent + 10", substrs=["(long) $2 = 17"])
5556

5657
self.quit()
58+
59+
# PExpect uses many timeouts internally and doesn't play well
60+
# under ASAN on a loaded machine..
61+
@skipIfAsan
62+
@skipIf(oslist=["linux"], archs=["arm", "aarch64"]) # Randomly fails on buildbot
63+
@skipIfEditlineSupportMissing
64+
def test_completion_with_space_only_line(self):
65+
"""Test that we don't crash when completing lines with spaces only"""
66+
self.start_repl()
67+
68+
self.child.send(" ")
69+
self.child.send("\t")
70+
self.expect_repl("3 + 3", substrs=["(int) $0 = 6"])

0 commit comments

Comments
 (0)