Skip to content

Commit e431624

Browse files
committed
CronSequenceGenerator prevents stack overflow in case of inverted range
Issue: SPR-14462
1 parent 6d91d54 commit e431624

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

spring-context/src/main/java/org/springframework/scheduling/support/CronSequenceGenerator.java

+6
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public CronSequenceGenerator(String expression, TimeZone timeZone) {
9595
parse(expression);
9696
}
9797

98+
9899
/**
99100
* Return the cron pattern that this sequence generator has been built for.
100101
*/
@@ -378,6 +379,10 @@ private int[] getRange(String field, int min, int max) {
378379
throw new IllegalArgumentException("Range less than minimum (" + min + "): '" +
379380
field + "' in expression \"" + this.expression + "\"");
380381
}
382+
if (result[0] > result[1]) {
383+
throw new IllegalArgumentException("Invalid inverted range: '" + field +
384+
"' in expression \"" + this.expression + "\"");
385+
}
381386
return result;
382387
}
383388

@@ -388,6 +393,7 @@ private int[] getRange(String field, int min, int max) {
388393
* fields separated by single spaces.
389394
* @param expression the expression to evaluate
390395
* @return {@code true} if the given expression is a valid cron expression
396+
* @since 4.3
391397
*/
392398
public static boolean isValidExpression(String expression) {
393399
String[] fields = StringUtils.tokenizeToStringArray(expression, " ");

spring-context/src/test/java/org/springframework/scheduling/support/CronSequenceGeneratorTests.java

+20
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ public void withNegativeIncrement() {
5656
new CronSequenceGenerator("*/-1 * * * * *").next(new Date(2012, 6, 1, 9, 0));
5757
}
5858

59+
@Test(expected = IllegalArgumentException.class)
60+
public void withInvertedMinuteRange() {
61+
new CronSequenceGenerator("* 6-5 * * * *").next(new Date(2012, 6, 1, 9, 0));
62+
}
63+
64+
@Test(expected = IllegalArgumentException.class)
65+
public void withInvertedHourRange() {
66+
new CronSequenceGenerator("* * 6-5 * * *").next(new Date(2012, 6, 1, 9, 0));
67+
}
68+
69+
@Test
70+
public void withSameMinuteRange() {
71+
new CronSequenceGenerator("* 6-6 * * * *").next(new Date(2012, 6, 1, 9, 0));
72+
}
73+
74+
@Test
75+
public void withSameHourRange() {
76+
new CronSequenceGenerator("* * 6-6 * * *").next(new Date(2012, 6, 1, 9, 0));
77+
}
78+
5979
@Test
6080
public void validExpression() {
6181
assertTrue(CronSequenceGenerator.isValidExpression("0 */2 1-4 * * *"));

0 commit comments

Comments
 (0)