Skip to content

Commit

Permalink
[flang] Fix use-after-free cases found by valgrind (llvm#122394)
Browse files Browse the repository at this point in the history
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 llvm#121999.
  • Loading branch information
klausler authored and paulhuggett committed Jan 16, 2025
1 parent b2e7a4a commit dd232f6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions flang/include/flang/Evaluate/traverse.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
Expand Down
6 changes: 6 additions & 0 deletions flang/include/flang/Evaluate/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_; }
Expand Down

0 comments on commit dd232f6

Please sign in to comment.