Skip to content

Commit 44152ce

Browse files
committed
CronSequenceGenerator prevents stack overflow in case of inverted range
Issue: SPR-14462 (cherry picked from commit da59b4d)
1 parent 97d73eb commit 44152ce

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -379,6 +379,10 @@ private int[] getRange(String field, int min, int max) {
379379
throw new IllegalArgumentException("Range less than minimum (" + min + "): '" +
380380
field + "' in expression \"" + this.expression + "\"");
381381
}
382+
if (result[0] > result[1]) {
383+
throw new IllegalArgumentException("Invalid inverted range: '" + field +
384+
"' in expression \"" + this.expression + "\"");
385+
}
382386
return result;
383387
}
384388

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

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,31 +29,51 @@
2929
public class CronSequenceGeneratorTests {
3030

3131
@Test
32-
public void testAt50Seconds() {
32+
public void at50Seconds() {
3333
assertEquals(new Date(2012, 6, 2, 1, 0),
3434
new CronSequenceGenerator("*/15 * 1-4 * * *").next(new Date(2012, 6, 1, 9, 53, 50)));
3535
}
3636

3737
@Test
38-
public void testAt0Seconds() {
38+
public void at0Seconds() {
3939
assertEquals(new Date(2012, 6, 2, 1, 0),
4040
new CronSequenceGenerator("*/15 * 1-4 * * *").next(new Date(2012, 6, 1, 9, 53)));
4141
}
4242

4343
@Test
44-
public void testAt0Minutes() {
44+
public void at0Minutes() {
4545
assertEquals(new Date(2012, 6, 2, 1, 0),
4646
new CronSequenceGenerator("0 */2 1-4 * * *").next(new Date(2012, 6, 1, 9, 0)));
4747
}
4848

4949
@Test(expected = IllegalArgumentException.class)
50-
public void testWith0Increment() {
50+
public void with0Increment() {
5151
new CronSequenceGenerator("*/0 * * * * *").next(new Date(2012, 6, 1, 9, 0));
5252
}
5353

5454
@Test(expected = IllegalArgumentException.class)
55-
public void testWithNegativeIncrement() {
55+
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
}

0 commit comments

Comments
 (0)