From b648cc9a908c22490de781dbe600459edd0ac533 Mon Sep 17 00:00:00 2001 From: Thomas Orozco Date: Wed, 1 Aug 2018 16:02:27 +0200 Subject: [PATCH] Expose ParseStrict to error on too many fields Parse currently ignores any tokens after the 7th, which means invalid cron expressions might be allowed as long as invalid tokens are found after the 7th character. In some use cases (e.g. validating cron expressions provided by a user), this might not be desirable. To allow for this use case, this adds a ParseStrict function that returns an error if too many fields are provided (it retains backwards compatibility by not touching Parse). --- cronexpr.go | 21 +++++++++++++++++++-- cronexpr_test.go | 16 ++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/cronexpr.go b/cronexpr.go index 58b518f..3bb6571 100644 --- a/cronexpr.go +++ b/cronexpr.go @@ -62,11 +62,22 @@ func MustParse(cronLine string) *Expression { /******************************************************************************/ // Parse returns a new Expression pointer. An error is returned if a malformed -// cron expression is supplied. +// cron expression is supplied. ParseStrict does the same thing, but unlike +// Parse, it will return an error if the provided cron line has too many +// tokens. // See for documentation // about what is a well-formed cron expression from this library's point of // view. + func Parse(cronLine string) (*Expression, error) { + return parse(cronLine, false) +} + +func ParseStrict(cronLine string) (*Expression, error) { + return parse(cronLine, true) +} + +func parse(cronLine string, strict bool) (*Expression, error) { // Maybe one of the built-in aliases is being used cron := cronNormalizer.Replace(cronLine) @@ -76,8 +87,14 @@ func Parse(cronLine string) (*Expression, error) { if fieldCount < 5 { return nil, fmt.Errorf("missing field(s)") } - // ignore fields beyond 7th + if fieldCount > 7 { + // In strict mode, error out + if strict { + return nil, fmt.Errorf("too many fields") + } + + // In non-strict mode, ignore fields beyond 7th fieldCount = 7 } diff --git a/cronexpr_test.go b/cronexpr_test.go index 6ccf7ab..0af28c4 100644 --- a/cronexpr_test.go +++ b/cronexpr_test.go @@ -307,6 +307,22 @@ func TestInterval_Interval60Issue(t *testing.T) { /******************************************************************************/ +func TestTooManyFields(t *testing.T) { + cronLine := "* * * * * * * foobar" + + _, err := ParseStrict(cronLine) + if err == nil { + t.Errorf("ParseStrict with too many fields should return err ") + } + + _, err = Parse(cronLine) + if err != nil { + t.Errorf("Parse with too many fields should not return err ") + } +} + +/******************************************************************************/ + var benchmarkExpressions = []string{ "* * * * *", "@hourly",