Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic on Invalid Input Instead of Returning Error #10

Open
twsiyuan opened this issue Apr 19, 2024 · 0 comments
Open

Panic on Invalid Input Instead of Returning Error #10

twsiyuan opened this issue Apr 19, 2024 · 0 comments

Comments

@twsiyuan
Copy link

Description:

I encountered a runtime panic instead of a handled error return when passing an invalid cron expression "1 1 1 x 1" to the ToDescription method. The program should return an error according to best practices rather than causing a panic.

Steps to Reproduce:

Use the following Go code snippet or Playground:

package main

import (
	"fmt"
	"github.com/lnquy/cron"
)

func main() {
	desc, err := cron.NewDescriptor()
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	text, err := desc.ToDescription("1 1 1 x 1", cron.Locale_en)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Println(text)
}

Expected Result:

The method ToDescription should return an error indicating that the input is invalid.

Actual Result:

The program panics with the message:

panic: runtime error: index out of range [-1]

goroutine 1 [running]:
github.com/lnquy/cron.(*ExpressionDescriptor).getMonthDescription.func1({0x4f9b9b?, 0x4f9b9b?})
	/tmp/gopath3485488474/pkg/mod/github.com/lnquy/cron@v1.1.1/cron.go:277 +0x57
github.com/lnquy/cron.getSegmentDescription({0x4f9b9b, 0x1}, {0x0?, 0x0?}, 0xc000121cb8, 0xc000121ca0, 0xc000121c88, 0xc000121c70, {0x535438, 0xc000010120})
	/tmp/gopath3485488474/pkg/mod/github.com/lnquy/cron@v1.1.1/cron.go:490 +0x139
github.com/lnquy/cron.(*ExpressionDescriptor).getMonthDescription(0xc0000164e0?, {0xc000119340, 0x7, 0x7?}, {0x535438, 0xc000010120})
	/tmp/gopath3485488474/pkg/mod/github.com/lnquy/cron@v1.1.1/cron.go:272 +0x169
github.com/lnquy/cron.(*ExpressionDescriptor).ToDescription(0xc0000164e0, {0x4f9b95?, 0x0?}, {0x4f908c, 0x2})
	/tmp/gopath3485488474/pkg/mod/github.com/lnquy/cron@v1.1.1/cron.go:84 +0x1e5
main.main()
	/tmp/sandbox593502589/prog.go:16 +0x46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant