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

fix nil pointer dereference when no callback passed to provider #129

Merged
merged 1 commit into from Jan 13, 2022

Conversation

robinbraemer
Copy link
Contributor

When doing

k.Load(env.Provider("", ".", nil), nil)

koanf always wraps the nil callback cb

func Provider(prefix, delim string, cb func(s string) string) *Env {
return &Env{
prefix: prefix,
delim: delim,
cb: func(key string, value string) (string, interface{}) {
return cb(key), value
},
}
}

and would panic because

if e.cb != nil {
is always true but the wrapped cb is nil.

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x10ae7a7]

goroutine 6 [running]:
testing.tRunner.func1.2({0x14e6300, 0x24c6d40})
        /home/robin/go/go1.17.1/src/testing/testing.go:1209 +0x24e
testing.tRunner.func1()
        /home/robin/go/go1.17.1/src/testing/testing.go:1212 +0x218
panic({0x14e6300, 0x24c6d40})
        /home/robin/go/go1.17.1/src/runtime/panic.go:1038 +0x215
github.com/knadh/koanf/providers/env.Provider.func1({0xc000040000, 0x19}, {0xc000040015, 0x4})
        /home/robin/go/pkg/mod/github.com/knadh/koanf@v1.3.3/providers/env/env.go:38 +0x27
github.com/knadh/koanf/providers/env.(*Env).Read(0xc0002c0270)
        /home/robin/go/pkg/mod/github.com/knadh/koanf@v1.3.3/providers/env/env.go:82 +0x1a3
github.com/knadh/koanf.(*Koanf).Load(0x0, {0x191c880, 0xc0002c0270}, {0x0, 0x0})
        /home/robin/go/pkg/mod/github.com/knadh/koanf@v1.3.3/koanf.go:101 +0x9e

@knadh knadh merged commit c961b5a into knadh:master Jan 13, 2022
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

Successfully merging this pull request may close these issues.

None yet

2 participants