[flang-commits] [flang] 085c197 - [flang] Diagnose ridiculously large kind parameters

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Fri Mar 10 09:44:36 PST 2023


Author: Peter Klausler
Date: 2023-03-10T09:40:27-08:00
New Revision: 085c1971ec583260c3ba3e45b83c78db113a9c80

URL: https://github.com/llvm/llvm-project/commit/085c1971ec583260c3ba3e45b83c78db113a9c80
DIFF: https://github.com/llvm/llvm-project/commit/085c1971ec583260c3ba3e45b83c78db113a9c80.diff

LOG: [flang] Diagnose ridiculously large kind parameters

Kind parameters on literals (1_4, 1.0_8, 4_"Unicode") are not
being caught as errors if they're extremely large integer values
that become valid when truncated to C++'s int type.

Differential Revision: https://reviews.llvm.org/D145747

Added: 
    

Modified: 
    flang/lib/Semantics/expression.cpp
    flang/test/Semantics/kinds02.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index b5f3a07679fd..14f2b0f0f7be 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -596,23 +596,24 @@ int ExpressionAnalyzer::AnalyzeKindParam(
   if (!kindParam) {
     return defaultKind;
   }
-  return common::visit(
+  std::int64_t kind{common::visit(
       common::visitors{
-          [](std::uint64_t k) { return static_cast<int>(k); },
+          [](std::uint64_t k) { return static_cast<std::int64_t>(k); },
           [&](const parser::Scalar<
               parser::Integer<parser::Constant<parser::Name>>> &n) {
             if (MaybeExpr ie{Analyze(n)}) {
-              if (std::optional<std::int64_t> i64{ToInt64(*ie)}) {
-                int iv = *i64;
-                if (iv == *i64) {
-                  return iv;
-                }
-              }
+              return ToInt64(*ie).value_or(defaultKind);
             }
-            return defaultKind;
+            return static_cast<std::int64_t>(defaultKind);
           },
       },
-      kindParam->u);
+      kindParam->u)};
+  if (kind != static_cast<int>(kind)) {
+    Say("Unsupported type kind value (%jd)"_err_en_US,
+        static_cast<std::intmax_t>(kind));
+    kind = defaultKind;
+  }
+  return static_cast<int>(kind);
 }
 
 // Common handling of parser::IntLiteralConstant and SignedIntLiteralConstant
@@ -657,7 +658,7 @@ struct IntTypeVisitor {
   }
   ExpressionAnalyzer &analyzer;
   parser::CharBlock digits;
-  int kind;
+  std::int64_t kind;
   bool isDefaultKind;
   bool isNegated;
 };

diff  --git a/flang/test/Semantics/kinds02.f90 b/flang/test/Semantics/kinds02.f90
index b02659e701f3..13dbb803de8e 100644
--- a/flang/test/Semantics/kinds02.f90
+++ b/flang/test/Semantics/kinds02.f90
@@ -82,6 +82,8 @@
 character(len=*), parameter :: cvar10 = 4_"abcd"
 !ERROR: CHARACTER(KIND=8) is not a supported type
 character(len=*), parameter :: cvar11 = 8_"abcd"
+!ERROR: Unsupported type kind value (5000000000)
+print *, 123_5000000000
 end program
 
 subroutine s(a, b)


        


More information about the flang-commits mailing list