Skip to content

Commit

Permalink
[LLDB] Fix crash when using tab completion on class variables (#83234)
Browse files Browse the repository at this point in the history
We weren't checking to see if the partial_path was empty before adding
completions and this led to crashes when the class object and a variable
both start with the same substring.

Fixes [#81536](llvm/llvm-project#81536)

---------

Co-authored-by: Michael Buch <[email protected]>
  • Loading branch information
svs-quic and Michael137 authored Feb 29, 2024
1 parent bd595d5 commit de55188
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
8 changes: 5 additions & 3 deletions lldb/source/Symbol/Variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,15 +509,17 @@ static void PrivateAutoCompleteMembers(
CompilerType member_compiler_type = compiler_type.GetFieldAtIndex(
i, member_name, nullptr, nullptr, nullptr);

if (partial_member_name.empty() ||
llvm::StringRef(member_name).starts_with(partial_member_name)) {
if (partial_member_name.empty()) {
request.AddCompletion((prefix_path + member_name).str());
} else if (llvm::StringRef(member_name)
.starts_with(partial_member_name)) {
if (member_name == partial_member_name) {
PrivateAutoComplete(
frame, partial_path,
prefix_path + member_name, // Anything that has been resolved
// already will be in here
member_compiler_type.GetCanonicalType(), request);
} else {
} else if (partial_path.empty()) {
request.AddCompletion((prefix_path + member_name).str());
}
}
Expand Down
6 changes: 4 additions & 2 deletions lldb/test/API/functionalities/completion/TestCompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ def test_dwim_print(self):

def do_test_variable_completion(self, command):
self.complete_from_to(f"{command} fo", f"{command} fooo")
self.complete_from_to(f"{command} fooo.", f"{command} fooo.")
self.complete_from_to(f"{command} fooo.", f"{command} fooo.t")
self.complete_from_to(f"{command} fooo.t.", f"{command} fooo.t.x")
self.complete_from_to(f"{command} fooo.dd", f"{command} fooo.dd")

self.complete_from_to(f"{command} ptr_fooo->", f"{command} ptr_fooo->")
self.complete_from_to(f"{command} ptr_fooo->", f"{command} ptr_fooo->t")
self.complete_from_to(f"{command} ptr_fooo->t", f"{command} ptr_fooo->t.x")
self.complete_from_to(f"{command} ptr_fooo->dd", f"{command} ptr_fooo->dd")

self.complete_from_to(f"{command} cont", f"{command} container")
Expand Down
13 changes: 9 additions & 4 deletions lldb/test/API/functionalities/completion/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#include <iostream>

class Baz {
public:
int x;
};

class Foo
{
public:
int Bar(int x, int y)
{
return x + y;
}
Baz t;
int temp;

int Bar(int x, int y) { return x + y; }
};

namespace { int Quux (void) { return 0; } }
Expand Down

0 comments on commit de55188

Please sign in to comment.