You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2016/08/10 08:00:51 UTC

groovy git commit: GROOVY-7902: fixing collate() infinite loop when step equals to zero (closes #378)

Repository: groovy
Updated Branches:
  refs/heads/master 1667899cb -> a472efe53


GROOVY-7902: fixing collate() infinite loop when step equals to zero (closes #378)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/a472efe5
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/a472efe5
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/a472efe5

Branch: refs/heads/master
Commit: a472efe53853c857e9b542f1843e2b9527859a98
Parents: 1667899
Author: fmamud <tr...@gmail.com>
Authored: Fri Aug 5 00:29:28 2016 -0300
Committer: paulk <pa...@asert.com.au>
Committed: Wed Aug 10 17:59:46 2016 +1000

----------------------------------------------------------------------
 .../org/codehaus/groovy/runtime/DefaultGroovyMethods.java     | 2 ++
 src/test/groovy/CollateTest.groovy                            | 7 +++++++
 2 files changed, 9 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/a472efe5/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index a971c62..8b4212c 100644
--- a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -3077,6 +3077,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
      * @param step          the number of elements to step through for each sub-list
      * @param keepRemainder if true, any remaining elements are returned as sub-lists.  Otherwise they are discarded
      * @return a List containing the data collated into sub-lists
+     * @throws IllegalArgumentException if the step is zero.
      * @since 2.4.0
      */
     public static <T> List<List<T>> collate(Iterable<T> self, int size, int step, boolean keepRemainder) {
@@ -3085,6 +3086,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
         if (size <= 0) {
             answer.add(selfList);
         } else {
+            if (step == 0) throw new IllegalArgumentException("step cannot be zero");
             for (int pos = 0; pos < selfList.size() && pos > -1; pos += step) {
                 if (!keepRemainder && pos > selfList.size() - size) {
                     break ;

http://git-wip-us.apache.org/repos/asf/groovy/blob/a472efe5/src/test/groovy/CollateTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/CollateTest.groovy b/src/test/groovy/CollateTest.groovy
index e08172a..ca27560 100644
--- a/src/test/groovy/CollateTest.groovy
+++ b/src/test/groovy/CollateTest.groovy
@@ -91,6 +91,13 @@ class CollateTest extends GroovyTestCase {
     assert [ 1, 2, 3 ].collate( 2, -1 ) == [[ 1, 2 ]]
   }
 
+  void testZeroedStep() {
+    String message = shouldFail (IllegalArgumentException) {
+      [ 1, 2, 3 ].collate( 2, 0 )
+    }
+    assert message == 'step cannot be zero'
+  }
+
   void testChaining() {
     def list = 1..15
     def expected = [ [ [ 1, 2, 3],  [ 4, 5, 6] ],