Skip to content

Commit

Permalink
CronSequenceGenerator prevents stack overflow in case of inverted range
Browse files Browse the repository at this point in the history
Issue: SPR-14462
  • Loading branch information
jhoeller committed Jul 14, 2016
1 parent 6d91d54 commit e431624
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public CronSequenceGenerator(String expression, TimeZone timeZone) {
parse(expression);
}


/**
* Return the cron pattern that this sequence generator has been built for.
*/
Expand Down Expand Up @@ -378,6 +379,10 @@ private int[] getRange(String field, int min, int max) {
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;
}

Expand All @@ -388,6 +393,7 @@ private int[] getRange(String field, int min, int max) {
* fields separated by single spaces.
* @param expression the expression to evaluate
* @return {@code true} if the given expression is a valid cron expression
* @since 4.3
*/
public static boolean isValidExpression(String expression) {
String[] fields = StringUtils.tokenizeToStringArray(expression, " ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ 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));
}

@Test
public void validExpression() {
assertTrue(CronSequenceGenerator.isValidExpression("0 */2 1-4 * * *"));
Expand Down

0 comments on commit e431624

Please sign in to comment.