Skip to content

Commit 54af853

Browse files
committed
Use GetSyntheticArrayMember to subscript pointers and out of bounds index from arrays
1 parent ad204cd commit 54af853

File tree

2 files changed

+14
-59
lines changed

2 files changed

+14
-59
lines changed

lldb/include/lldb/ValueObject/DILEval.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ class Interpreter : Visitor {
5555
llvm::Expected<lldb::ValueObjectSP>
5656
Visit(const ArraySubscriptNode *node) override;
5757

58-
lldb::ValueObjectSP PointerAdd(lldb::ValueObjectSP lhs, int64_t offset);
59-
6058
// Used by the interpreter to create objects, perform casts, etc.
6159
lldb::TargetSP m_target;
6260
llvm::StringRef m_expr;

lldb/source/ValueObject/DILEval.cpp

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,6 @@
1818

1919
namespace lldb_private::dil {
2020

21-
static lldb::ValueObjectSP
22-
ArrayToPointerConversion(lldb::ValueObjectSP valobj,
23-
std::shared_ptr<ExecutionContextScope> ctx) {
24-
assert(valobj->IsArrayType() &&
25-
"an argument to array-to-pointer conversion must be an array");
26-
27-
uint64_t addr = valobj->GetLoadAddress();
28-
llvm::StringRef name = "result";
29-
ExecutionContext exe_ctx;
30-
ctx->CalculateExecutionContext(exe_ctx);
31-
return ValueObject::CreateValueObjectFromAddress(
32-
name, addr, exe_ctx,
33-
valobj->GetCompilerType().GetArrayElementType(ctx.get()).GetPointerType(),
34-
/* do_deref */ false);
35-
}
36-
3721
static lldb::ValueObjectSP LookupStaticIdentifier(
3822
VariableList &variable_list, std::shared_ptr<StackFrame> exe_scope,
3923
llvm::StringRef name_ref, llvm::StringRef unqualified_name) {
@@ -327,21 +311,6 @@ Interpreter::Visit(const UnaryOpNode *node) {
327311
m_expr, "invalid ast: unexpected binary operator", node->GetLocation());
328312
}
329313

330-
lldb::ValueObjectSP Interpreter::PointerAdd(lldb::ValueObjectSP lhs,
331-
int64_t offset) {
332-
uint64_t byte_size = 0;
333-
if (auto temp = lhs->GetCompilerType().GetPointeeType().GetByteSize(
334-
lhs->GetTargetSP().get()))
335-
byte_size = *temp;
336-
uintptr_t addr = lhs->GetValueAsUnsigned(0) + offset * byte_size;
337-
338-
llvm::StringRef name = "result";
339-
ExecutionContext exe_ctx(m_target.get(), false);
340-
return ValueObject::CreateValueObjectFromAddress(name, addr, exe_ctx,
341-
lhs->GetCompilerType(),
342-
/* do_deref */ false);
343-
}
344-
345314
llvm::Expected<lldb::ValueObjectSP>
346315
Interpreter::Visit(const ArraySubscriptNode *node) {
347316
auto lhs_or_err = Evaluate(node->lhs());
@@ -373,22 +342,21 @@ Interpreter::Visit(const ArraySubscriptNode *node) {
373342
m_expr, "array subscript is not an integer", node->GetLocation());
374343

375344
// Check to see if 'base' has a synthetic value; if so, try using that.
345+
uint64_t child_idx = index->GetValueAsUnsigned(0);
376346
if (base->HasSyntheticValue()) {
377347
lldb::ValueObjectSP synthetic = base->GetSyntheticValue();
378348
if (synthetic && synthetic != base) {
379349
uint32_t num_children = synthetic->GetNumChildrenIgnoringErrors();
380350
// Verify that the 'index' is not out-of-range for the declared type.
381-
if (index->GetValueAsSigned(0) >= num_children) {
382-
auto message =
383-
llvm::formatv("array index {0} is not valid for \"({1}) {2}\"",
384-
index->GetValueAsSigned(0),
385-
base->GetTypeName().AsCString("<invalid type>"),
386-
base->GetName().AsCString());
351+
if (child_idx >= num_children) {
352+
auto message = llvm::formatv(
353+
"array index {0} is not valid for \"({1}) {2}\"", child_idx,
354+
base->GetTypeName().AsCString("<invalid type>"),
355+
base->GetName().AsCString());
387356
return llvm::make_error<DILDiagnosticError>(m_expr, message,
388357
node->GetLocation());
389358
}
390359

391-
uint64_t child_idx = index->GetValueAsUnsigned(0);
392360
if (static_cast<uint32_t>(child_idx) <
393361
synthetic->GetNumChildrenIgnoringErrors()) {
394362
lldb::ValueObjectSP child_valobj_sp =
@@ -410,25 +378,14 @@ Interpreter::Visit(const ArraySubscriptNode *node) {
410378
m_expr, "subscript of pointer to incomplete type 'void'",
411379
node->GetLocation());
412380

413-
if (base_type.IsArrayType())
414-
base = ArrayToPointerConversion(base, m_exe_ctx_scope);
415-
416-
CompilerType item_type = base->GetCompilerType().GetPointeeType();
417-
lldb::addr_t base_addr = base->GetValueAsUnsigned(0);
418-
419-
llvm::StringRef name = "result";
420-
ExecutionContext exe_ctx(m_target.get(), false);
421-
// Create a pointer and add the index, i.e. "base + index".
422-
lldb::ValueObjectSP value =
423-
PointerAdd(ValueObject::CreateValueObjectFromAddress(
424-
name, base_addr, exe_ctx, item_type.GetPointerType(),
425-
/*do_deref=*/false),
426-
index->GetValueAsSigned(0));
427-
428-
lldb::ValueObjectSP val2 = value->Dereference(error);
429-
if (error.Fail())
430-
return error.ToError();
431-
return val2;
381+
if (base_type.IsArrayType()) {
382+
uint32_t num_children = base->GetNumChildrenIgnoringErrors();
383+
if (child_idx < num_children)
384+
return base->GetChildAtIndex(child_idx);
385+
}
386+
387+
int64_t signed_child_idx = index->GetValueAsSigned(0);
388+
return base->GetSyntheticArrayMember(signed_child_idx, true);
432389
}
433390

434391
} // namespace lldb_private::dil

0 commit comments

Comments
 (0)