[llvm] Make sure that the `std::optional<>` result is checked before being accessed (PR #116479)

Romain Thomas via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 16 01:11:45 PST 2024


https://github.com/romainthomas created https://github.com/llvm/llvm-project/pull/116479

In some cases (c.f. the attached binary), `getAsSectionOffset()` is failing and thus, the inline de-referencing
is wrong.  

[D_test.bin.zip](https://github.com/user-attachments/files/17784093/D_test.bin.zip)


>From dbb1ca2f04552566285294a910fa500d0be9c074 Mon Sep 17 00:00:00 2001
From: Romain Thomas <me at romainthomas.fr>
Date: Sat, 16 Nov 2024 09:45:48 +0100
Subject: [PATCH] Make sure that the `std::optional<>` result is checked before
 being accessed.

---
 llvm/lib/DebugInfo/DWARF/DWARFDie.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp
index c1cd587877de0c..a14e4451d5dba8 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp
@@ -394,9 +394,13 @@ Expected<DWARFAddressRangesVector> DWARFDie::getAddressRanges() const {
 
   std::optional<DWARFFormValue> Value = find(DW_AT_ranges);
   if (Value) {
+    std::optional<uint64_t> SecOff = Value->getAsSectionOffset();
+    if (!SecOff) {
+      return DWARFAddressRangesVector();
+    }
     if (Value->getForm() == DW_FORM_rnglistx)
-      return U->findRnglistFromIndex(*Value->getAsSectionOffset());
-    return U->findRnglistFromOffset(*Value->getAsSectionOffset());
+      return U->findRnglistFromIndex(*SecOff);
+    return U->findRnglistFromOffset(*SecOff);
   }
   return DWARFAddressRangesVector();
 }



More information about the llvm-commits mailing list