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

bug: serpent.UrlOf may cause a panic when parsing url from env #14

Open
johnstcn opened this issue May 3, 2024 · 1 comment
Open

bug: serpent.UrlOf may cause a panic when parsing url from env #14

johnstcn opened this issue May 3, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@johnstcn
Copy link
Member

johnstcn commented May 3, 2024

It appears that when parsing a URL from the environment, serpent does not dereference it properly when embedded in a struct:

FROBNICATE_URL=http://foo.bar go run main.go 
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x10083be74]

goroutine 1 [running]:
github.com/coder/serpent.(*URL).Set(0x0, {0x1400001608f?, 0x100849e0a?})
	/Users/cian/go/pkg/mod/github.com/coder/[email protected]/values.go:277 +0x34
github.com/coder/serpent.(*OptionSet).ParseEnv(0x140000ba280, {0x1400027a000, 0x23, 0x10064b324?})
	/Users/cian/go/pkg/mod/github.com/coder/[email protected]/option.go:287 +0x240
github.com/coder/serpent.(*Invocation).run(0x140001d4200, 0x1400025fca8)
	/Users/cian/go/pkg/mod/github.com/coder/[email protected]/command.go:289 +0x58
github.com/coder/serpent.(*Invocation).Run(0x140001d4200)
	/Users/cian/go/pkg/mod/github.com/coder/[email protected]/command.go:536 +0x128
main.main()
	/Users/cian/tmp/serpent-url-panic/main.go:40 +0x268
exit status 2

Sample Go program to reproduce: https://go.dev/play/p/K9uNgbvGM0J

@joobisb
Copy link
Contributor

joobisb commented Nov 17, 2024

@johnstcn I did take a look at this.

This could be fixed if we defined it as a value type instead of a pointer. If we use url.URL, it is initialized with its zero value and prevents nil pointer dereference, but if we use a pointer type *url.URL we have to explicitly allocate otherwise it remains nil causing this error.

Something like this, wdyt?

type Options struct {
	URL url.URL
}

{
Name:        "url",
Value:       serpent.URLOf(&o.URL),
 ...                   
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants