[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 23 06:34:15 PST 2024


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

>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 1/2] 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();
 }

>From 394adb0fbc3e4cfa7d3bba69c142d96d9762f1fd Mon Sep 17 00:00:00 2001
From: Romain Thomas <me at romainthomas.fr>
Date: Sat, 23 Nov 2024 15:33:55 +0100
Subject: [PATCH 2/2] Fix coding style

---
 llvm/lib/DebugInfo/DWARF/DWARFDie.cpp | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp
index a14e4451d5dba8..7481b7787ccac7 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp
@@ -393,16 +393,17 @@ Expected<DWARFAddressRangesVector> DWARFDie::getAddressRanges() const {
     return DWARFAddressRangesVector{{LowPC, HighPC, Index}};
 
   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(*SecOff);
-    return U->findRnglistFromOffset(*SecOff);
-  }
-  return DWARFAddressRangesVector();
+  if (!Value)
+    return DWARFAddressRangesVector();
+
+  std::optional<uint64_t> SecOff = Value->getAsSectionOffset();
+  if (!SecOff)
+    return DWARFAddressRangesVector();
+
+  if (Value->getForm() == DW_FORM_rnglistx)
+    return U->findRnglistFromIndex(*SecOff);
+
+  return U->findRnglistFromOffset(*SecOff);
 }
 
 bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {



More information about the llvm-commits mailing list