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

via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 16 01:12:41 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-debuginfo

Author: Romain Thomas (romainthomas)

<details>
<summary>Changes</summary>

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)


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


1 Files Affected:

- (modified) llvm/lib/DebugInfo/DWARF/DWARFDie.cpp (+6-2) 


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

``````````

</details>


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


More information about the llvm-commits mailing list