[llvm] [llvm-dwarfdump] Add a null-check in `prettyPrintBaseTypeRef`. (PR #93156)

via llvm-commits llvm-commits at lists.llvm.org
Thu May 23 02:03:51 PDT 2024


https://github.com/mgschossmann created https://github.com/llvm/llvm-project/pull/93156

Fixes #93104

Prevent a crash by only printing DWARFUnit-unaware information in cases in which `DWARFUnit* U` is `nullptr`.

>From d04723665f9391c6cb9db453ccb8e6eba3d5adfd Mon Sep 17 00:00:00 2001
From: mgschossmann <109181247+mgschossmann at users.noreply.github.com>
Date: Thu, 23 May 2024 10:54:00 +0200
Subject: [PATCH] [llvm-dwarfdump] Add a null-check in
 `prettyPrintBaseTypeRef`. Fixes #93104

Prevent a crash by only printing DWARFUnit-unaware information in cases
in which `DWARFUnit* U` is `nullptr`.
---
 llvm/lib/DebugInfo/DWARF/DWARFExpression.cpp | 24 ++++++++++++--------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/DebugInfo/DWARF/DWARFExpression.cpp b/llvm/lib/DebugInfo/DWARF/DWARFExpression.cpp
index 87a4fc78ceb19..0c28701360b3c 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFExpression.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFExpression.cpp
@@ -240,17 +240,21 @@ static void prettyPrintBaseTypeRef(DWARFUnit *U, raw_ostream &OS,
                                    ArrayRef<uint64_t> Operands,
                                    unsigned Operand) {
   assert(Operand < Operands.size() && "operand out of bounds");
-  auto Die = U->getDIEForOffset(U->getOffset() + Operands[Operand]);
-  if (Die && Die.getTag() == dwarf::DW_TAG_base_type) {
-    OS << " (";
-    if (DumpOpts.Verbose)
-      OS << format("0x%08" PRIx64 " -> ", Operands[Operand]);
-    OS << format("0x%08" PRIx64 ")", U->getOffset() + Operands[Operand]);
-    if (auto Name = dwarf::toString(Die.find(dwarf::DW_AT_name)))
-      OS << " \"" << *Name << "\"";
+  if (U) {
+    auto Die = U->getDIEForOffset(U->getOffset() + Operands[Operand]);
+    if (Die && Die.getTag() == dwarf::DW_TAG_base_type) {
+      OS << " (";
+      if (DumpOpts.Verbose)
+        OS << format("0x%08" PRIx64 " -> ", Operands[Operand]);
+      OS << format("0x%08" PRIx64 ")", U->getOffset() + Operands[Operand]);
+      if (auto Name = dwarf::toString(Die.find(dwarf::DW_AT_name)))
+        OS << " \"" << *Name << "\"";
+    } else {
+      OS << format(" <invalid base_type ref: 0x%" PRIx64 ">",
+                   Operands[Operand]);
+    }
   } else {
-    OS << format(" <invalid base_type ref: 0x%" PRIx64 ">",
-                 Operands[Operand]);
+    OS << format(" <base_type ref: 0x%" PRIx64 ">", Operands[Operand]);
   }
 }
 



More information about the llvm-commits mailing list