Skip to content

Commit 071c62c

Browse files
committed
[lldb] Modernize sprintf in FormatEntity.cpp
Avoid buffer overflows with large indexes, and spurious nul characters with small ones.
1 parent 46200de commit 071c62c

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

lldb/source/Core/FormatEntity.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -618,20 +618,16 @@ static bool DumpRegister(Stream &s, StackFrame *frame, RegisterKind reg_kind,
618618
static ValueObjectSP ExpandIndexedExpression(ValueObject *valobj, size_t index,
619619
bool deref_pointer) {
620620
Log *log = GetLog(LLDBLog::DataFormatters);
621-
const char *ptr_deref_format = "[%d]";
622-
std::string ptr_deref_buffer(10, 0);
623-
::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
624-
LLDB_LOGF(log, "[ExpandIndexedExpression] name to deref: %s",
625-
ptr_deref_buffer.c_str());
621+
std::string name_to_deref = llvm::formatv("[{0}]", index);
622+
LLDB_LOG(log, "[ExpandIndexedExpression] name to deref: {0}", name_to_deref);
626623
ValueObject::GetValueForExpressionPathOptions options;
627624
ValueObject::ExpressionPathEndResultType final_value_type;
628625
ValueObject::ExpressionPathScanEndReason reason_to_stop;
629626
ValueObject::ExpressionPathAftermath what_next =
630627
(deref_pointer ? ValueObject::eExpressionPathAftermathDereference
631628
: ValueObject::eExpressionPathAftermathNothing);
632629
ValueObjectSP item = valobj->GetValueForExpressionPath(
633-
ptr_deref_buffer.c_str(), &reason_to_stop, &final_value_type, options,
634-
&what_next);
630+
name_to_deref, &reason_to_stop, &final_value_type, options, &what_next);
635631
if (!item) {
636632
LLDB_LOGF(log,
637633
"[ExpandIndexedExpression] ERROR: why stopping = %d,"

0 commit comments

Comments
 (0)