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

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 18 09:50:01 PST 2024


================
@@ -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();
+    }
----------------
dwblaikie wrote:

Please omit the {} from single-line if statements like this.

Might be worth making this code a bit more consistent in the way it handles non-present optionals, since currentnly the two cases right next to each other are handled differently (one with an if, the other with an if!+early return). I guess the early return might be preferable, consistent with LLVM's style preference to reduce indentation:

```
optional Value = ...
if (!Value)
  return;
optional SecOff = ...
if (!SecOff)
  return;
...
```

Also - can you include a test case that demonstrates/validates this bug/fix?

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


More information about the llvm-commits mailing list