[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:26:15 PDT 2023


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

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. 

>From 4a668481a1a17512ee412c31b452c114626f8cde Mon Sep 17 00:00:00 2001
From: jeffreytan81 <jeffreytan at fb.com>
Date: Mon, 16 Oct 2023 14:21:40 -0700
Subject: [PATCH] Guard against invalid variant index

---
 lldb/examples/synthetic/gnu_libstdcpp.py | 5 +++++
 1 file changed, 5 insertions(+)

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



More information about the lldb-commits mailing list