spf13 / viper

Go configuration with fangs
MIT License
26.76k stars 2.01k forks source link

"AllKeys" causes "runtime error: invalid memory address or nil pointer dereference" #314

Open maxmeyer opened 7 years ago

maxmeyer commented 7 years ago

The use of #Unmarshal causes a nil pointer error if cfg is a #Sub.

package my_package

func NewHosts(cfg *viper.Viper) []ssh.Host {
    var hosts []ssh.Host
    cfg.Unmarshal(&hosts)
    return hosts
}

The function is called this way:

my_package.NewHosts(viper.Sub("hosts"))

Using viper as configuration reader, I got this error

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

goroutine 1 [running]:
panic(0x7ec260, 0xc420010220)
        /home/user/.local/share/gosource/go/src/runtime/panic.go:500 +0x1a1
github.com/spf13/viper.(*Viper).AllKeys(0x0, 0x0, 0xc4201347b0, 0x0)
        /home/user/.local/share/go/src/github.com/spf13/viper/viper.go:1323 +0x6c
github.com/spf13/viper.(*Viper).AllSettings(0x0, 0x40fd68)
        /home/user/.local/share/go/src/github.com/spf13/viper/viper.go:1403 +0x78
github.com/spf13/viper.(*Viper).Unmarshal(0x0, 0x7b1200, 0xc4200c9e20, 0xa, 0x7c0c20)
        /home/user/.local/share/go/src/github.com/spf13/viper/viper.go:723 +0x2f
company.org/product/mgt/config.NewHosts(0x0, 0xa, 0x0, 0x9)
        /home/user/.local/share/go/src/company.org/product/mgt/config/parse.go:51 +0x99
company.org/product/mgt/cmd.glob..func3(0xa52e20, 0xa755a8, 0x0, 0x0)
        /home/user/.local/share/go/src/company.org/product/mgt/cmd/show_status.go:23 +0x15f
github.com/spf13/cobra.(*Command).execute(0xa52e20, 0xa755a8, 0x0, 0x0, 0xa52e20, 0xa755a8)
        /home/user/.local/share/go/src/github.com/spf13/cobra/command.go:650 +0x443
github.com/spf13/cobra.(*Command).ExecuteC(0xa527c0, 0x4667db, 0x40de2c, 0xc42001c0b8)
        /home/user/.local/share/go/src/github.com/spf13/cobra/command.go:737 +0x367
github.com/spf13/cobra.(*Command).Execute(0xa527c0, 0xc42001c0b8, 0x0)
        /home/user/.local/share/go/src/github.com/spf13/cobra/command.go:695 +0x2b
company.org/product/mgt/cmd.Execute()
        /home/user/.local/share/go/src/company.org/product/mgt/cmd/root.go:26 +0x3f
main.main()
        /home/user/.local/share/go/src/company.org/product/mgt/main.go:6 +0x14
exit status 2
FATA[0002] Execution failed                              commandline="go run main.go show status" message="exit status 1" status=1
exit status 1
2017/02/27 13:41:33 exit status 1

The following line seems to cause this error: https://github.com/spf13/viper/blob/master/viper.go#L1329.

markkuit commented 5 years ago

Did a solution come up in the end? I'm still experiencing the same issue with viper v1.2.0

trevex commented 3 years ago

Still an issue on viper v1.7.1.

For everyone stumbling upon this issue. As a workaround you can use Unmarshal on the root viper instance and construct a struct inline, e.g.:

var c config
v.Unmarshal(&struct{ Sub *config }{&c})

If you need to support environments were no config file is a supported option, be careful to not run into: https://github.com/spf13/viper/issues/1023

Currently the safest option without any issues is to fallback to constructing the struct using v.Get* methods.