Skip to content

Commit

Permalink
Merge pull request #1500 from daniel-beck/JENKINS-25897
Browse files Browse the repository at this point in the history
[FIXED JENKINS-25897] Add range check for H(X-Y) syntax
  • Loading branch information
olivergondza committed Dec 23, 2014
2 parents 546e0cf + b090751 commit a89bcdd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion core/src/main/grammar/crontab.g
Expand Up @@ -110,7 +110,7 @@ throws ANTLRException
}
| ("H" "(")=> "H" "(" s=token "-" e=token ")" ( "/" d=token )?
{
bits = doHash(s,e,d);
bits = doHash(s,e,d,field);
}
| "H" ( "/" d=token )?
{
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/hudson/scheduler/BaseParser.java
Expand Up @@ -96,10 +96,12 @@ protected long doHash(int step, int field) throws ANTLRException {
int u = UPPER_BOUNDS[field];
if (field==2) u = 28; // day of month can vary depending on month, so to make life simpler, just use [1,28] that's always safe
if (field==4) u = 6; // Both 0 and 7 of day of week are Sunday. For better distribution, limit upper bound to 6
return doHash(LOWER_BOUNDS[field], u, step);
return doHash(LOWER_BOUNDS[field], u, step, field);
}

protected long doHash(int s, int e, int step) throws ANTLRException {
protected long doHash(int s, int e, int step, int field) throws ANTLRException {
rangeCheck(s, field);
rangeCheck(e, field);
if (step > e - s + 1) {
error(Messages.BaseParser_OutOfRange(step, 1, e - s + 1));
throw new AssertionError();
Expand Down
21 changes: 21 additions & 0 deletions core/src/test/java/hudson/scheduler/CronTabTest.java
Expand Up @@ -280,4 +280,25 @@ public int next(int n) {
assertEquals("[35, 56]", times.toString());
}

@Test public void rangeBoundsCheckOK() throws Exception {
new CronTab("H(0-59) H(0-23) H(1-31) H(1-12) H(0-7)");
}

@Test public void rangeBoundsCheckFailHour() throws Exception {
try {
new CronTab("H H(12-24) * * *");
fail();
} catch (ANTLRException e) {
// ok
}
}

@Test public void rangeBoundsCheckFailMinute() throws Exception {
try {
new CronTab("H(33-66) * * * *");
fail();
} catch (ANTLRException e) {
// ok
}
}
}

0 comments on commit a89bcdd

Please sign in to comment.