Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Reland] Detect against invalid variant index for LibStdC++ std::variant data formatters #69614

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions lldb/examples/synthetic/gnu_libstdcpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,11 @@ def get_variant_npos_value(index_byte_size):
if index == npos_value:
return " No Value"

# Invalid index can happen when the variant is not initialized yet.
template_arg_count = data_obj.GetType().GetNumberOfTemplateArguments()
if index >= template_arg_count:
return " <Invalid>"

active_type = data_obj.GetType().GetTemplateArgumentType(index)
return f" Active Type = {active_type.GetDisplayTypeName()} "

Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7183,7 +7183,8 @@ GetNthTemplateArgument(const clang::ClassTemplateSpecializationDecl *decl,
// (including the ones preceding the parameter pack).
const auto &pack = args[last_idx];
const size_t pack_idx = idx - last_idx;
assert(pack_idx < pack.pack_size() && "parameter pack index out-of-bounds");
if (pack_idx >= pack.pack_size())
return nullptr;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change needed now that the formatter is checking the number of arguments up front?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is requested by @clayborg's comment in the original patch:

If this is what was crashing, we should modify the GetTemplateArgumentType() to not crash with an inalid index as part of this fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, we should still include this change. We shouldn't crash if the idx argument is too high.

return &pack.pack_elements()[pack_idx];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,29 @@ def test_with_run_command(self):
substrs=["v_many_types_no_value = No Value"],
)
"""

@add_test_categories(["libstdcxx"])
def test_invalid_variant_index(self):
"""Test LibStdC++ data formatter for std::variant with invalid index."""
self.build()

(self.target, self.process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
self, "// break here", lldb.SBFileSpec("main.cpp", False)
)

lldbutil.continue_to_breakpoint(self.process, bkpt)

self.expect(
"frame variable v1",
substrs=["v1 = Active Type = int {", "Value = 12", "}"],
)

var_v1 = thread.frames[0].FindVariable("v1")
var_v1_raw_obj = var_v1.GetNonSyntheticValue()
index_obj = var_v1_raw_obj.GetChildMemberWithName("_M_index")
self.assertTrue(index_obj and index_obj.IsValid())

INVALID_INDEX = "100"
index_obj.SetValueFromCString(INVALID_INDEX)

self.expect("frame variable v1", substrs=["v1 = <Invalid>"])