[flang-commits] [PATCH] D122590: [flang] prevent undefined behavior in character MAXLOC folding

Jean Perier via Phabricator via flang-commits flang-commits at lists.llvm.org
Mon Mar 28 08:54:48 PDT 2022


jeanPerier created this revision.
jeanPerier added a reviewer: klausler.
jeanPerier added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a project: All.
jeanPerier requested review of this revision.

When folding MAXLOC/MINLOC, the current element being compared was moved twice
in row in case it became the new extremum. With intrinsic types, it
made no difference (std::move is a no-op for them), but for characters
where the string storage is actually moved, it caused the new extremum to
be set to the empty string, leading to wrong results.

Note: I could have left the first std::move relating to logical Findloc, but it
brings nothing and makes the code less auditable, so I also removed it.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122590

Files:
  flang/lib/Evaluate/fold-integer.cpp
  flang/test/Evaluate/fold-findloc.f90


Index: flang/test/Evaluate/fold-findloc.f90
===================================================================
--- flang/test/Evaluate/fold-findloc.f90
+++ flang/test/Evaluate/fold-findloc.f90
@@ -60,4 +60,10 @@
   logical, parameter :: test_xi3b = all(maxloc(ia3, back=.true.) == [0,0,0])
   logical, parameter :: test_xi3c = all(maxloc(ia3, dim=2) == reshape([0,0,0,0],shape=[2,2]))
   logical, parameter :: test_xi3d = all(maxloc(ia3, dim=2, back=.true.) == reshape([0,0,0,0],shape=[2,2]))
+
+  character(1), parameter :: a(4) = ['a', 'b', 'a', 'b']
+  logical, parameter :: test_char1 = all(maxloc(a).eq.[2])
+  logical, parameter :: test_char2 = all(minloc(a).eq.[1])
+  logical, parameter :: test_char3 = all(maxloc(a, back=.true.).eq.[4])
+  logical, parameter :: test_char4 = all(minloc(a, back=.true.).eq.[3])
 end module
Index: flang/lib/Evaluate/fold-integer.cpp
===================================================================
--- flang/lib/Evaluate/fold-integer.cpp
+++ flang/lib/Evaluate/fold-integer.cpp
@@ -331,14 +331,12 @@
       if constexpr (T::category == TypeCategory::Logical) {
         // array(at) .EQV. value?
         static_assert(WHICH == WhichLocation::Findloc);
-        cmp.emplace(
-            ConvertToType<LogicalResult>(Expr<T>{LogicalOperation<T::kind>{
-                LogicalOperator::Eqv, Expr<T>{Constant<T>{std::move(element)}},
-                Expr<T>{Constant<T>{*value}}}}));
+        cmp.emplace(ConvertToType<LogicalResult>(
+            Expr<T>{LogicalOperation<T::kind>{LogicalOperator::Eqv,
+                Expr<T>{Constant<T>{element}}, Expr<T>{Constant<T>{*value}}}}));
       } else { // compare array(at) to value
-        cmp.emplace(
-            PackageRelation(relation, Expr<T>{Constant<T>{std::move(element)}},
-                Expr<T>{Constant<T>{*value}}));
+        cmp.emplace(PackageRelation(relation, Expr<T>{Constant<T>{element}},
+            Expr<T>{Constant<T>{*value}}));
       }
       Expr<LogicalResult> folded{Fold(context_, std::move(*cmp))};
       result = GetScalarConstantValue<LogicalResult>(folded).value().IsTrue();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122590.418607.patch
Type: text/x-patch
Size: 2109 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220328/cdaaa5ba/attachment-0001.bin>


More information about the flang-commits mailing list