Skip to content

Commit 9be5404

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

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 27 additions & 6 deletions
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.
@@ -25,34 +25,55 @@
2525
/**
2626
* @author Juergen Hoeller
2727
*/
28+
@SuppressWarnings("deprecation")
2829
public class CronSequenceGeneratorTests {
2930

3031
@Test
31-
public void testAt50Seconds() {
32+
public void at50Seconds() {
3233
assertEquals(new Date(2012, 6, 2, 1, 0),
3334
new CronSequenceGenerator("*/15 * 1-4 * * *").next(new Date(2012, 6, 1, 9, 53, 50)));
3435
}
3536

3637
@Test
37-
public void testAt0Seconds() {
38+
public void at0Seconds() {
3839
assertEquals(new Date(2012, 6, 2, 1, 0),
3940
new CronSequenceGenerator("*/15 * 1-4 * * *").next(new Date(2012, 6, 1, 9, 53)));
4041
}
4142

4243
@Test
43-
public void testAt0Minutes() {
44+
public void at0Minutes() {
4445
assertEquals(new Date(2012, 6, 2, 1, 0),
4546
new CronSequenceGenerator("0 */2 1-4 * * *").next(new Date(2012, 6, 1, 9, 0)));
4647
}
4748

4849
@Test(expected = IllegalArgumentException.class)
49-
public void testWith0Increment() {
50+
public void with0Increment() {
5051
new CronSequenceGenerator("*/0 * * * * *").next(new Date(2012, 6, 1, 9, 0));
5152
}
5253

5354
@Test(expected = IllegalArgumentException.class)
54-
public void testWithNegativeIncrement() {
55+
public void withNegativeIncrement() {
5556
new CronSequenceGenerator("*/-1 * * * * *").next(new Date(2012, 6, 1, 9, 0));
5657
}
5758

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+
5879
}

0 commit comments

Comments
 (0)