[Lldb-commits] [lldb] Detect against invalid variant index for LibStdC++ std::variant data formatters (PR #69253)

via lldb-commits lldb-commits at lists.llvm.org
Mon Oct 16 14:28:24 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: None (jeffreytan81)

<details>
<summary>Changes</summary>

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. 

---
Full diff: https://github.com/llvm/llvm-project/pull/69253.diff


1 Files Affected:

- (modified) lldb/examples/synthetic/gnu_libstdcpp.py (+5) 


``````````diff
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()} "
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/69253


More information about the lldb-commits mailing list