[flang-commits] [flang] [flang][Lower] Fix UB in location handling (PR #177944)
Tom Eccles via flang-commits
flang-commits at lists.llvm.org
Mon Jan 26 04:44:00 PST 2026
https://github.com/tblah created https://github.com/llvm/llvm-project/pull/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
>From 80ad4d878dae37a1100a0e125ed7681701ff1143 Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Mon, 26 Jan 2026 12:36:13 +0000
Subject: [PATCH] [flang][Lower] Fix UB in location handling
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
---
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 5eefd5472e143..b4c344ba4254e 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) {
More information about the flang-commits
mailing list