https://github.com/llvmbot created 
https://github.com/llvm/llvm-project/pull/177965

Backport 9be7c10

Requested by: @tblah

>From 16e2fa0594dc9962bdd8a505608d4c023858bf54 Mon Sep 17 00:00:00 2001
From: Tom Eccles <[email protected]>
Date: Mon, 26 Jan 2026 14:06:46 +0000
Subject: [PATCH] [flang][Lower] Fix UB in location handling (#177944)

Previously `prov` received the address of a variable allocated in stack
memory (the contents of `include`). `prov` would then access that memory
outside of the lifetime of that stack allocation: leading to UB.

This only manifested on thinLTO builds. No added test because
flang/test/Lower/location.f90 covers it (when thinLTO is enabled) and
there are bots guarding the thin-lto configuration.

Fixes #156629
Fixes #176404

(cherry picked from commit 9be7c1037f26146e469c85061d6685a9172c5de9)
---
 flang/lib/Lower/Bridge.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 2d9947530cae8..1f35153928500 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -1117,7 +1117,7 @@ class FirConverter : public 
Fortran::lower::AbstractConverter {
                                                       
fir::LocationKind::Base));
 
         // Gather include location information if any.
-        Fortran::parser::ProvenanceRange *prov = &*provenance;
+        std::optional<Fortran::parser::ProvenanceRange> prov = provenance;
         while (prov) {
           if (std::optional<Fortran::parser::ProvenanceRange> include =
                   cooked->allSources().GetInclusionInfo(*prov)) {
@@ -1127,9 +1127,9 @@ class FirConverter : public 
Fortran::lower::AbstractConverter {
               locAttrs.push_back(fir::LocationKindAttr::get(
                   &getMLIRContext(), fir::LocationKind::Inclusion));
             }
-            prov = &*include;
+            prov = include;
           } else {
-            prov = nullptr;
+            prov.reset();
           }
         }
         if (locs.size() > 1) {

_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to