You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by dd...@apache.org on 2019/08/04 20:23:51 UTC

[freemarker] 02/03: Forward ported from 2.3-gae: ?min and ?max will now immediately stop with error when applied on a right unbounded numerical range (like 1..), as that would run forever anyway.

This is an automated email from the ASF dual-hosted git repository.

ddekany pushed a commit to branch 3
in repository https://gitbox.apache.org/repos/asf/freemarker.git

commit be9c38dfb172eeeb197541322460ebda11908859
Author: ddekany <dd...@apache.org>
AuthorDate: Sun Aug 4 22:22:17 2019 +0200

    Forward ported from 2.3-gae: ?min and ?max will now immediately stop with error when applied on a right unbounded numerical range (like 1..), as that would run forever anyway.
---
 .../java/org/apache/freemarker/core/MinMaxBITest.java    | 10 +++++++++-
 .../org/apache/freemarker/core/BuiltInsForSequences.java | 16 +++++++++++-----
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/freemarker-core-test/src/test/java/org/apache/freemarker/core/MinMaxBITest.java b/freemarker-core-test/src/test/java/org/apache/freemarker/core/MinMaxBITest.java
index ef948e2..f62eead 100644
--- a/freemarker-core-test/src/test/java/org/apache/freemarker/core/MinMaxBITest.java
+++ b/freemarker-core-test/src/test/java/org/apache/freemarker/core/MinMaxBITest.java
@@ -66,7 +66,15 @@ public class MinMaxBITest extends TemplateTest {
             }
         }
     }
-    
+
+    @Test
+    public void rightUnboundedNumericalRangeTest() throws Exception {
+        assertErrorContains("${(1..)?min}", "right-unbounded", "infinite");
+        assertErrorContains("${(1..)?max}", "right-unbounded", "infinite");
+        assertOutput("${(1..2)?min}", "1");
+        assertOutput("${(1..2)?max}", "2");
+    }
+
     private class InputMinMax {
         private final List<?> input;
         private final String minExpected;
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForSequences.java b/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForSequences.java
index 5df62f1..e22d2e1 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForSequences.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForSequences.java
@@ -238,10 +238,7 @@ class BuiltInsForSequences {
 
         @Override
         TemplateModel calculateResult(TemplateIterableModel model, Environment env) throws TemplateException {
-            if (model instanceof RightUnboundedRangeModel) {
-                throw new TemplateException(
-                        "The sequence to join was right-unbounded numerical range, thus it's infinitely long.");
-            }
+            checkNotRightUnboundedNumericalRange(model);
             return new BIMethodForIterable(model);
         }
    
@@ -798,6 +795,14 @@ class BuiltInsForSequences {
         
     }
 
+    private static void checkNotRightUnboundedNumericalRange(TemplateModel model) throws TemplateException {
+        if (model instanceof RightUnboundedRangeModel) {
+            throw new TemplateException(
+                    "The input sequence is a right-unbounded numerical range, thus, it's infinitely long, and can't " +
+                    "processed with this built-in.");
+        }
+    }
+
     private static boolean modelsEqual(
             int seqItemIndex, TemplateModel seqItem, TemplateModel searchedItem, Environment env)
             throws TemplateException {
@@ -845,7 +850,8 @@ class BuiltInsForSequences {
 
         @Override
         TemplateModel calculateResult(TemplateIterableModel model, Environment env) throws TemplateException {
-            // TODO Auto-generated method stub
+            checkNotRightUnboundedNumericalRange(model);
+
             TemplateModel best = null;
             TemplateModelIterator iter = model.iterator();
             while (iter.hasNext()) {