Skip to content

Fix panic on zero-value Viper struct SetDefault/Set#2116

Closed
Bahtya wants to merge 1 commit intospf13:masterfrom
Bahtya:fix/nil-map-panic
Closed

Fix panic on zero-value Viper struct SetDefault/Set#2116
Bahtya wants to merge 1 commit intospf13:masterfrom
Bahtya:fix/nil-map-panic

Conversation

@Bahtya
Copy link
Copy Markdown

@Bahtya Bahtya commented Apr 5, 2026

Problem

Using a zero-value Viper struct (without calling New() or Init()) causes a nil map panic when calling SetDefault() or Set():

var v viper.Viper
v.SetDefault("key", "value") // panic: assignment to entry in nil map

Solution

Add nil-map guards in both SetDefault and Set to initialize the maps on first use:

if v.defaults == nil {
    v.defaults = make(map[string]interface{})
}

Fixes #2111

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


bahtya seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Using a zero-value Viper struct (without calling New() or Init())
causes a nil map panic when calling SetDefault() or Set() because
v.defaults and v.override are uninitialized.

Add nil-map guards to initialize the maps on first use.

Fixes #2111
@Bahtya Bahtya closed this by deleting the head repository Apr 6, 2026
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.

Zero-value Viper struct panics on SetDefault/Set (nil map assignment)

2 participants