Skip to content
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

[flang] Fix crash in fuzzed input program #122193

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

klausler
Copy link
Contributor

@klausler klausler commented Jan 8, 2025

Fixes #121971.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Jan 8, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 8, 2025

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

Fixes #121971.


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

2 Files Affected:

  • (modified) flang/lib/Evaluate/shape.cpp (+6-6)
  • (added) flang/test/Semantics/bug121971.f90 (+10)
diff --git a/flang/lib/Evaluate/shape.cpp b/flang/lib/Evaluate/shape.cpp
index c7b2156a3de17a..f9d169e752cae5 100644
--- a/flang/lib/Evaluate/shape.cpp
+++ b/flang/lib/Evaluate/shape.cpp
@@ -566,13 +566,13 @@ MaybeExtentExpr GetExtent(const Subscript &subscript, const NamedEntity &base,
                 MaybeExtentExpr{triplet.stride()});
           },
           [&](const IndirectSubscriptIntegerExpr &subs) -> MaybeExtentExpr {
-            if (auto shape{GetShape(subs.value())}) {
-              if (GetRank(*shape) > 0) {
-                CHECK(GetRank(*shape) == 1); // vector-valued subscript
-                return std::move(shape->at(0));
-              }
+            if (auto shape{GetShape(subs.value())};
+                shape && GetRank(*shape) == 1) {
+              // vector-valued subscript
+              return std::move(shape->at(0));
+            } else {
+              return std::nullopt;
             }
-            return std::nullopt;
           },
       },
       subscript.u);
diff --git a/flang/test/Semantics/bug121971.f90 b/flang/test/Semantics/bug121971.f90
new file mode 100644
index 00000000000000..4192f6de2ec1f7
--- /dev/null
+++ b/flang/test/Semantics/bug121971.f90
@@ -0,0 +1,10 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+subroutine subr(a,b,n,m)
+  real n,m
+!ERROR: Must have INTEGER type, but is REAL(4)
+!ERROR: Must have INTEGER type, but is REAL(4)
+  integer a(n,m)
+!ERROR: Rank of left-hand side is 2, but right-hand side has rank 1
+!ERROR: Subscript expression has rank 2 greater than 1
+  a = a(a,j)
+end

Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

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

LGTM

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] fatal internal error: CHECK(GetRank(*shape) == 1) failed in flang/lib/Evaluate/shape.cpp
4 participants