From cff9279a986857e46684ca2c9fb4ec2e49f0b355 Mon Sep 17 00:00:00 2001 From: Thomas Orozco Date: Sat, 14 Nov 2020 14:36:16 +0000 Subject: [PATCH] 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 --- cronexpr_parse.go | 3 +++ cronexpr_test.go | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/cronexpr_parse.go b/cronexpr_parse.go index a9fe746..27683c6 100644 --- a/cronexpr_parse.go +++ b/cronexpr_parse.go @@ -438,6 +438,9 @@ func genericFieldParse(s string, desc fieldDescriptor) ([]*cronDirective, error) directive.first = desc.atoi(snormal[pairs[2]:pairs[3]]) directive.last = desc.atoi(snormal[pairs[4]:pairs[5]]) 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) continue } diff --git a/cronexpr_test.go b/cronexpr_test.go index 0af28c4..8bf8d4d 100644 --- a/cronexpr_test.go +++ b/cronexpr_test.go @@ -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) {