Skip to content

Commit ebec4d6

Browse files
authored
[flang] Fix use-after-free cases found by valgrind (#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 #121999.
1 parent b720b6c commit ebec4d6

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

flang/include/flang/Evaluate/traverse.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class Traverse {
139139
return visitor_(x.base());
140140
}
141141
Result operator()(const Triplet &x) const {
142-
return Combine(x.lower(), x.upper(), x.stride());
142+
return Combine(x.GetLower(), x.GetUpper(), x.GetStride());
143143
}
144144
Result operator()(const Subscript &x) const { return visitor_(x.u); }
145145
Result operator()(const ArrayRef &x) const {
@@ -151,7 +151,7 @@ class Traverse {
151151
}
152152
Result operator()(const DataRef &x) const { return visitor_(x.u); }
153153
Result operator()(const Substring &x) const {
154-
return Combine(x.parent(), x.lower(), x.upper());
154+
return Combine(x.parent(), x.GetLower(), x.GetUpper());
155155
}
156156
Result operator()(const ComplexPart &x) const {
157157
return visitor_(x.complex());

flang/include/flang/Evaluate/variable.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,14 @@ class Substring {
331331
}
332332

333333
Expr<SubscriptInteger> lower() const;
334+
const Expr<SubscriptInteger> *GetLower() const {
335+
return lower_.has_value() ? &lower_->value() : nullptr;
336+
}
334337
Substring &set_lower(Expr<SubscriptInteger> &&);
335338
std::optional<Expr<SubscriptInteger>> upper() const;
339+
const Expr<SubscriptInteger> *GetUpper() const {
340+
return upper_.has_value() ? &upper_->value() : nullptr;
341+
}
336342
Substring &set_upper(Expr<SubscriptInteger> &&);
337343
const Parent &parent() const { return parent_; }
338344
Parent &parent() { return parent_; }

0 commit comments

Comments
 (0)