From 44152ce4019f58337806f09bf0bf37d7f9e2a6bc Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 14 Jul 2016 21:11:28 +0200 Subject: [PATCH] CronSequenceGenerator prevents stack overflow in case of inverted range Issue: SPR-14462 (cherry picked from commit da59b4d) --- .../support/CronSequenceGenerator.java | 6 +++- .../support/CronSequenceGeneratorTests.java | 32 +++++++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/scheduling/support/CronSequenceGenerator.java b/spring-context/src/main/java/org/springframework/scheduling/support/CronSequenceGenerator.java index f53894c7943..f20bb57c15d 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/support/CronSequenceGenerator.java +++ b/spring-context/src/main/java/org/springframework/scheduling/support/CronSequenceGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -379,6 +379,10 @@ public class CronSequenceGenerator { throw new IllegalArgumentException("Range less than minimum (" + min + "): '" + field + "' in expression \"" + this.expression + "\""); } + if (result[0] > result[1]) { + throw new IllegalArgumentException("Invalid inverted range: '" + field + + "' in expression \"" + this.expression + "\""); + } return result; } diff --git a/spring-context/src/test/java/org/springframework/scheduling/support/CronSequenceGeneratorTests.java b/spring-context/src/test/java/org/springframework/scheduling/support/CronSequenceGeneratorTests.java index 6c4df6b01ee..9bc71fb4653 100644 --- a/spring-context/src/test/java/org/springframework/scheduling/support/CronSequenceGeneratorTests.java +++ b/spring-context/src/test/java/org/springframework/scheduling/support/CronSequenceGeneratorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,31 +29,51 @@ import static org.junit.Assert.*; public class CronSequenceGeneratorTests { @Test - public void testAt50Seconds() { + public void at50Seconds() { assertEquals(new Date(2012, 6, 2, 1, 0), new CronSequenceGenerator("*/15 * 1-4 * * *").next(new Date(2012, 6, 1, 9, 53, 50))); } @Test - public void testAt0Seconds() { + public void at0Seconds() { assertEquals(new Date(2012, 6, 2, 1, 0), new CronSequenceGenerator("*/15 * 1-4 * * *").next(new Date(2012, 6, 1, 9, 53))); } @Test - public void testAt0Minutes() { + public void at0Minutes() { assertEquals(new Date(2012, 6, 2, 1, 0), new CronSequenceGenerator("0 */2 1-4 * * *").next(new Date(2012, 6, 1, 9, 0))); } @Test(expected = IllegalArgumentException.class) - public void testWith0Increment() { + public void with0Increment() { new CronSequenceGenerator("*/0 * * * * *").next(new Date(2012, 6, 1, 9, 0)); } @Test(expected = IllegalArgumentException.class) - public void testWithNegativeIncrement() { + public void withNegativeIncrement() { new CronSequenceGenerator("*/-1 * * * * *").next(new Date(2012, 6, 1, 9, 0)); } + @Test(expected = IllegalArgumentException.class) + public void withInvertedMinuteRange() { + new CronSequenceGenerator("* 6-5 * * * *").next(new Date(2012, 6, 1, 9, 0)); + } + + @Test(expected = IllegalArgumentException.class) + public void withInvertedHourRange() { + new CronSequenceGenerator("* * 6-5 * * *").next(new Date(2012, 6, 1, 9, 0)); + } + + @Test + public void withSameMinuteRange() { + new CronSequenceGenerator("* 6-6 * * * *").next(new Date(2012, 6, 1, 9, 0)); + } + + @Test + public void withSameHourRange() { + new CronSequenceGenerator("* * 6-6 * * *").next(new Date(2012, 6, 1, 9, 0)); + } + }