Skip to content

[flang] Fix crash in statement function semantics (bug #80532) #82702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2024

Conversation

klausler
Copy link
Contributor

When statement function expressions are analyzed, ensure that the semantics context has a valid location set, otherwise a type spec (like "integer::") can lead to a crash.

Fixes #80532.

When statement function expressions are analyzed, ensure that
the semantics context has a valid location set, otherwise
a type spec (like "integer::") can lead to a crash.

Fixes llvm#80532.
@klausler klausler requested a review from psteinfeld February 22, 2024 23:04
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Feb 22, 2024
@llvmbot
Copy link
Member

llvmbot commented Feb 22, 2024

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

When statement function expressions are analyzed, ensure that the semantics context has a valid location set, otherwise a type spec (like "integer::") can lead to a crash.

Fixes #80532.


Full diff: https://github.com/llvm/llvm-project/pull/82702.diff

2 Files Affected:

  • (modified) flang/lib/Semantics/expression.cpp (+2-1)
  • (modified) flang/lib/Semantics/resolve-names.cpp (+1)
diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index 8d817f077880b9..b957f773816b1b 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -4613,8 +4613,9 @@ evaluate::Expr<evaluate::SubscriptInteger> AnalyzeKindSelector(
     SemanticsContext &context, common::TypeCategory category,
     const std::optional<parser::KindSelector> &selector) {
   evaluate::ExpressionAnalyzer analyzer{context};
+  CHECK(context.location().has_value());
   auto restorer{
-      analyzer.GetContextualMessages().SetLocation(context.location().value())};
+      analyzer.GetContextualMessages().SetLocation(*context.location())};
   return analyzer.AnalyzeKindSelector(category, selector);
 }
 
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 36deab969456d0..04e465acf12466 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -8247,6 +8247,7 @@ void ResolveNamesVisitor::FinishSpecificationPart(
     if (const auto *statement{std::get_if<
             parser::Statement<common::Indirection<parser::StmtFunctionStmt>>>(
             &decl.u)}) {
+      messageHandler().set_currStmtSource(statement->source);
       AnalyzeStmtFunctionStmt(statement->statement.value());
     }
   }

Copy link
Contributor

@psteinfeld psteinfeld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All builds and tests correctly and looks good.

@klausler klausler merged commit 8f80d46 into llvm:main Mar 1, 2024
@klausler klausler deleted the bug80532 branch March 1, 2024 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Flang] flang-new crashes when DO-iterator exists inside iterator of the same name
3 participants