[llvm] [llvm][utils] Improve the StringRef summary provider (PR #162298)

Ebuka Ezike via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 7 08:07:40 PDT 2025


https://github.com/da-viper created https://github.com/llvm/llvm-project/pull/162298

- check the length of data before casting as `char[N]` because the will cause lldb to allocate `N` bytes of memory.

>From e3f090bed709bef5ba7fc4317c7b1d3f20cce967 Mon Sep 17 00:00:00 2001
From: Ebuka Ezike <yerimyah1 at gmail.com>
Date: Tue, 7 Oct 2025 16:06:49 +0100
Subject: [PATCH] [llvm][utils] Improve the StringRef summary provider

- check the length of data before casting as `char[N]` because the will cause lldb to allocate `N` bytes of memory.
---
 llvm/utils/lldbDataFormatters.py | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/llvm/utils/lldbDataFormatters.py b/llvm/utils/lldbDataFormatters.py
index 5e553caa4446d..06cb8cbb1e068 100644
--- a/llvm/utils/lldbDataFormatters.py
+++ b/llvm/utils/lldbDataFormatters.py
@@ -197,6 +197,13 @@ def StringRefSummaryProvider(valobj, internal_dict):
         return '""'
 
     data = data_pointer.deref
+    # StringRef may be uninitialized with length exceeding available memory,
+    # potentially causing bad_alloc exceptions. Limit the length to max string summary setting.
+    limit_obj = (
+        valobj.GetTarget().GetDebugger().GetSetting("target.max-string-summary-length")
+    )
+    if limit_obj:
+        length = min(length, limit_obj.GetUnsignedIntegerValue())
     # Get a char[N] type, from the underlying char type.
     array_type = data.type.GetArrayType(length)
     # Cast the char* string data to a char[N] array.



More information about the llvm-commits mailing list