From 4a6b5478eec1a418dd427e9b84a1fcec6de317f0 Mon Sep 17 00:00:00 2001 From: xiaofan Date: Mon, 5 Dec 2016 11:12:22 +0800 Subject: [PATCH] fix interval 60 issue --- cronexpr_parse.go | 9 +++++++++ cronexpr_test.go | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/cronexpr_parse.go b/cronexpr_parse.go index 6672177..9e9d0f6 100644 --- a/cronexpr_parse.go +++ b/cronexpr_parse.go @@ -473,6 +473,9 @@ func genericFieldParse(s string, desc *fieldDescriptor) ([]*cronDirective, error directive.first = desc.min directive.last = desc.max directive.step = atoi(snormal[pairs[2]:pairs[3]]) + if directive.step < 1 || directive.step > desc.max { + return nil, fmt.Errorf("invalid interval %s", snormal) + } directives = append(directives, &directive) continue } @@ -483,6 +486,9 @@ func genericFieldParse(s string, desc *fieldDescriptor) ([]*cronDirective, error directive.first = desc.atoi(snormal[pairs[2]:pairs[3]]) directive.last = desc.max directive.step = atoi(snormal[pairs[4]:pairs[5]]) + if directive.step < 1 || directive.step > desc.max { + return nil, fmt.Errorf("invalid interval %s", snormal) + } directives = append(directives, &directive) continue } @@ -493,6 +499,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 = atoi(snormal[pairs[6]:pairs[7]]) + if directive.step < 1 || directive.step > desc.max { + return nil, fmt.Errorf("invalid interval %s", snormal) + } directives = append(directives, &directive) continue } diff --git a/cronexpr_test.go b/cronexpr_test.go index b729170..f170769 100644 --- a/cronexpr_test.go +++ b/cronexpr_test.go @@ -284,6 +284,29 @@ func TestNextN_every5min(t *testing.T) { } } +// Issue: https://github.com/gorhill/cronexpr/issues/16 +func TestInterval_Interval60Issue(t *testing.T){ + _, err := cronexpr.Parse("*/60 * * * * *") + if err == nil { + t.Errorf("parsing with interval 60 should return err") + } + + _, err = cronexpr.Parse("*/61 * * * * *") + if err == nil { + t.Errorf("parsing with interval 61 should return err") + } + + _, err = cronexpr.Parse("2/60 * * * * *") + if err == nil { + t.Errorf("parsing with interval 60 should return err") + } + + _, err = cronexpr.Parse("2-20/61 * * * * *") + if err == nil { + t.Errorf("parsing with interval 60 should return err") + } +} + /******************************************************************************/ var benchmarkExpressions = []string{