fix: lazily initialise Viper maps so zero-value struct does not panic#2127
Open
SAY-5 wants to merge 1 commit intospf13:masterfrom
Open
fix: lazily initialise Viper maps so zero-value struct does not panic#2127SAY-5 wants to merge 1 commit intospf13:masterfrom
SAY-5 wants to merge 1 commit intospf13:masterfrom
Conversation
A Viper instance constructed via a struct literal (&viper.Viper{})
rather than viper.New() has all of its internal string-keyed maps
as nil. SetDefault and Set eventually dispatch to deepSearch, which
assigns into those maps via map[key] = value and panics at runtime
with 'assignment to entry in nil map' (spf13#2111). Because Viper is an
exported struct type, users following normal Go conventions for zero
values (sync.Mutex{}, bytes.Buffer{}, etc.) reasonably expect the
zero value to either work or error - not panic with an internal
implementation detail message.
Add an ensureMaps helper that lazily allocates defaults, override,
config, kvstore, pflags, env, and aliases, and sets keyDelim to its
'.' default if zero. Call it from SetDefault and Set before any map
touch. Instances that went through New are already fully populated,
so ensureMaps is a no-op for them and the hot path for existing
callers is unchanged.
Add two regression tests that construct &Viper{} directly, call
SetDefault / Set on dotted keys, and assert no panic fires and the
values are readable back. On master the Set test fails because the
stored value never reaches the zero-initialised map; after the fix
both pass.
Closes spf13#2111
Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes #2111.
A Viper instance constructed via a struct literal (
&viper.Viper{}) rather thanviper.New()has all of its internal string-keyed maps as nil.SetDefaultandSeteventually dispatch todeepSearch, which assigns into those maps viamap[key] = valueand panics at runtime withassignment to entry in nil map.Because
Viperis an exported struct type, users following normal Go conventions for zero values (sync.Mutex{},bytes.Buffer{},strings.Builder{}, and so on) reasonably expect the zero value to either work or error - not panic with an internal implementation detail message.Fix
Add an
ensureMapshelper that lazily allocatesdefaults,override,config,kvstore,pflags,env, andaliases, and setskeyDelimto its.default if zero. Call it fromSetDefaultandSetbefore any map touch. Instances that went throughNeware already fully populated, soensureMapsis a no-op for them and the hot path for existing callers is unchanged.Tests
Added
TestZeroValueStructSetDefaultNoPanicandTestZeroValueStructSetNoPanic. Both construct&Viper{}directly, call the mutator on dotted keys, and assert no panic fires and the stored values are readable back viaGet. On master the Set test fails (nested.keynever resolves because the stored value never reaches the zero-initialised map); after the fix both pass.Verification
Locally on macOS, go 1.26.2:
gofmt -s -l: cleango vet ./...: cleango test -race -count=1 -run 'TestDefault|TestZeroValueStruct' ./...: passCloses #2111