Skip to content

Commit

Permalink
Fix some possible bad array indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilthoniel committed Dec 3, 2023
1 parent e900c33 commit 7d3cc06
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
)

var (
ErrMalformedExpression = errors.New("expression is malformed")
ErrMalformedField = errors.New("unexpected field value")
ErrMultipleNotSpecified = errors.New("only one `?` is supported")
ErrValueOutsideRange = errors.New("values are outside the supported range")
)
Expand Down Expand Up @@ -52,5 +54,5 @@ const (
var kinds = []string{"seconds", "minutes", "hours", "days", "months", "week days"}

func (k TimeUnitKind) String() string {
return kinds[k]
return kinds[int(k)%len(kinds)]
}
15 changes: 14 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type Parser struct{}

func (p Parser) Parse(expression string) (schedule Schedule, err error) {
matches := strings.Split(expression, " ")
if len(matches) != 6 {
return schedule, ErrMalformedExpression
}

weekdays, err := p.parse(matches[5], convertWeekDay, 0, 6)
if err != nil {
Expand Down Expand Up @@ -90,6 +93,11 @@ func (Parser) parse(expr string, convFn converterFn, min, max int) (fields []exp

func parseRange(expr string, convFn converterFn) (r rangeExpr, err error) {
parts := strings.Split(expr, "-")
if len(parts) != 2 {
err = ErrMalformedField
return
}

r.from, err = convFn(parts[0])
if err != nil {
return
Expand All @@ -104,6 +112,10 @@ func parseRange(expr string, convFn converterFn) (r rangeExpr, err error) {

func parseInterval(expr string, convFn converterFn, min, max int) (i intervalExpr, err error) {
parts := strings.Split(expr, "/")
if len(parts) != 2 {
err = ErrMalformedField
return
}

i.incr, err = strconv.Atoi(parts[1])
if err != nil {
Expand Down Expand Up @@ -277,7 +289,8 @@ func (l lastWeekDayOfMonthExpr) Value(t time.Time, _ int) int {
lastDayOfMonth := findLastDayOfMonth(t)
diff := int(lastDayOfMonth.Weekday() - l.weekday)
if diff < 0 {
diff += 7
// Add a week length to get a positive value.
diff += int(time.Saturday) + 1
}
return lastDayOfMonth.Day() - diff
}
Expand Down

0 comments on commit 7d3cc06

Please sign in to comment.