[flang-commits] [flang] [flang] Fix use-after-free cases found by valgrind (PR #122394)
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Thu Jan 9 16:57:34 PST 2025
https://github.com/klausler created https://github.com/llvm/llvm-project/pull/122394
The expression traversal library needs to use interfaces into triplets (and substrings) that return pointers to nested expressions, rather than optional copies of them, since at least one semantic analysis collects a set of references to some subexpression representation class instances, and those references obviously can't point to local copies of objects.
Fixes https://github.com/llvm/llvm-project/issues/121999.
>From da8814fc7844e3fd8bc27235b594ed4f4cf39777 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Thu, 9 Jan 2025 16:53:28 -0800
Subject: [PATCH] [flang] Fix use-after-free cases found by valgrind
The expression traversal library needs to use interfaces into
triplets (and substrings) that return pointers to nested
expressions, rather than optional copies of them, since at
least one semantic analysis collects a set of references to
some subexpression representation class instances, and those
references obviously can't point to local copies of objects.
Fixes https://github.com/llvm/llvm-project/issues/121999.
---
flang/include/flang/Evaluate/traverse.h | 4 ++--
flang/include/flang/Evaluate/variable.h | 6 ++++++
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/flang/include/flang/Evaluate/traverse.h b/flang/include/flang/Evaluate/traverse.h
index 90b93f6afd3515..dd38d64bff63f7 100644
--- a/flang/include/flang/Evaluate/traverse.h
+++ b/flang/include/flang/Evaluate/traverse.h
@@ -139,7 +139,7 @@ class Traverse {
return visitor_(x.base());
}
Result operator()(const Triplet &x) const {
- return Combine(x.lower(), x.upper(), x.stride());
+ return Combine(x.GetLower(), x.GetUpper(), x.GetStride());
}
Result operator()(const Subscript &x) const { return visitor_(x.u); }
Result operator()(const ArrayRef &x) const {
@@ -151,7 +151,7 @@ class Traverse {
}
Result operator()(const DataRef &x) const { return visitor_(x.u); }
Result operator()(const Substring &x) const {
- return Combine(x.parent(), x.lower(), x.upper());
+ return Combine(x.parent(), x.GetLower(), x.GetUpper());
}
Result operator()(const ComplexPart &x) const {
return visitor_(x.complex());
diff --git a/flang/include/flang/Evaluate/variable.h b/flang/include/flang/Evaluate/variable.h
index b454d37d93e57b..9b597d29813da1 100644
--- a/flang/include/flang/Evaluate/variable.h
+++ b/flang/include/flang/Evaluate/variable.h
@@ -331,8 +331,14 @@ class Substring {
}
Expr<SubscriptInteger> lower() const;
+ const Expr<SubscriptInteger> *GetLower() const {
+ return lower_.has_value() ? &lower_->value() : nullptr;
+ }
Substring &set_lower(Expr<SubscriptInteger> &&);
std::optional<Expr<SubscriptInteger>> upper() const;
+ const Expr<SubscriptInteger> *GetUpper() const {
+ return upper_.has_value() ? &upper_->value() : nullptr;
+ }
Substring &set_upper(Expr<SubscriptInteger> &&);
const Parent &parent() const { return parent_; }
Parent &parent() { return parent_; }
More information about the flang-commits
mailing list