Reject intervals that are out of order

If an interval if out of order, it ends up emitting 0000-00-00 00:00:00
as the "next" time, which is undesirable. This patch updates Parse to
simply reject such intervals.

Since the rejection reason might not be super obvious (the range 6-7 is
actually translated to 6-0, which makes it invalid), let's also print
the original vs. normalized form for clarity.

See: https://github.com/aptible/supercronic/issues/63
This commit is contained in:
Thomas Orozco 2020-11-14 14:36:16 +00:00
parent ee361fc97a
commit cff9279a98
2 changed files with 16 additions and 0 deletions

View File

@ -438,6 +438,9 @@ func genericFieldParse(s string, desc fieldDescriptor) ([]*cronDirective, error)
directive.first = desc.atoi(snormal[pairs[2]:pairs[3]]) directive.first = desc.atoi(snormal[pairs[2]:pairs[3]])
directive.last = desc.atoi(snormal[pairs[4]:pairs[5]]) directive.last = desc.atoi(snormal[pairs[4]:pairs[5]])
directive.step = 1 directive.step = 1
if directive.last < directive.first {
return nil, fmt.Errorf("invalid interval %s (normalized to %d-%d)", snormal, directive.first, directive.last)
}
directives = append(directives, &directive) directives = append(directives, &directive)
continue continue
} }

View File

@ -305,6 +305,19 @@ func TestInterval_Interval60Issue(t *testing.T) {
} }
} }
// Issue: https://github.com/aptible/supercronic/issues/63
func TestRange_OutOfOrder(t *testing.T) {
_, err := Parse("45 4 * * 6-7")
if err == nil {
t.Errorf("parsing with range 6-7 should return err")
}
_, err = Parse("45 4 * * 6-5")
if err == nil {
t.Errorf("parsing with range 6-5 should return err")
}
}
/******************************************************************************/ /******************************************************************************/
func TestTooManyFields(t *testing.T) { func TestTooManyFields(t *testing.T) {