-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Detect against invalid variant index for LibStdC++ std::variant data formatters #69253
Detect against invalid variant index for LibStdC++ std::variant data formatters #69253
Conversation
@llvm/pr-subscribers-lldb Author: None (jeffreytan81) Changeshttps://github.com/llvm/llvm-project/pull/68012/files added new data formatters for LibStdC++ std::variant. However, this formatter can crash if std::variant's index field has invalid value (exceeds the number of template arguments). This patch fixes the crash by ensuring the index is a valid value. Full diff: https://github.com/llvm/llvm-project/pull/69253.diff 1 Files Affected:
diff --git a/lldb/examples/synthetic/gnu_libstdcpp.py b/lldb/examples/synthetic/gnu_libstdcpp.py
index 29c926167fb440c..f778065aaca3771 100644
--- a/lldb/examples/synthetic/gnu_libstdcpp.py
+++ b/lldb/examples/synthetic/gnu_libstdcpp.py
@@ -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()} "
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a test. Should be easy to access the "_M_index" child and set its value to something invalid and then make sure the summary is "".
Sure, I can do that. |
template_arg_count = data_obj.GetType().GetNumberOfTemplateArguments() | ||
if index >= template_arg_count: | ||
return " <Invalid>" | ||
|
||
active_type = data_obj.GetType().GetTemplateArgumentType(index) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is what was crashing, we should modify the GetTemplateArgumentType() to not crash with an inalid index as part of this fix.
This seems to cause File "/home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/lang/cpp/class-template-parameter-pack/TestTemplatePackArgs.py", line 42, in test_template_argument_pack
self.assertEqual(nested_template.GetTemplateArgumentType(2).GetName(), "bool")
AssertionError: '' != 'bool' |
Same for AArch64 Ubuntu (https://lab.llvm.org/buildbot/#/builders/96/builds/47090) and Windows (https://lab.llvm.org/buildbot/#/builders/219/builds/6372) (32 bit ARM is out of action right now). Same symptom as the above comment. |
I've reverted it as the failure reason wasn't immediately obvious to me. Looks like it will reproduce quite easily. |
…ant data formatters (#69614) This is relanding of #69253. `TestTemplatePackArgs.py` is passing now. https://github.com/llvm/llvm-project/pull/68012/files added new data formatters for LibStdC++ std::variant. However, this formatter can crash if std::variant's index field has invalid value (exceeds the number of template arguments). This can happen if the current IP stops at a place std::variant is not initialized yet. This patch fixes the crash by ensuring the index is a valid value and fix GetNthTemplateArgument() to make sure it is not crashing. Co-authored-by: jeffreytan81 <[email protected]>
https://github.com/llvm/llvm-project/pull/68012/files added new data formatters for LibStdC++ std::variant.
However, this formatter can crash if std::variant's index field has invalid value (exceeds the number of template arguments).
This can happen if the current IP stops at a place std::variant is not initialized yet.
This patch fixes the crash by ensuring the index is a valid value.