Skip to content

fix: lazily initialise Viper maps so zero-value struct does not panic#2127

Open
SAY-5 wants to merge 1 commit intospf13:masterfrom
SAY-5:fix/zero-value-viper-ensure-maps-2111
Open

fix: lazily initialise Viper maps so zero-value struct does not panic#2127
SAY-5 wants to merge 1 commit intospf13:masterfrom
SAY-5:fix/zero-value-viper-ensure-maps-2111

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented Apr 21, 2026

What

Fixes #2111.

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.

Because Viper is 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 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.

Tests

Added TestZeroValueStructSetDefaultNoPanic and TestZeroValueStructSetNoPanic. Both construct &Viper{} directly, call the mutator on dotted keys, and assert no panic fires and the stored values are readable back via Get. On master the Set test fails (nested.key never 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: clean
  • go vet ./...: clean
  • go test -race -count=1 -run 'TestDefault|TestZeroValueStruct' ./...: pass

Closes #2111

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>
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 21, 2026

CLA assistant check
All committers have signed the CLA.

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