Skip to content

Avoid nil-value panics in RecursiveStructToMapHookFunc - #203

Open
Fulenn wants to merge 1 commit into
go-viper:mainfrom
Fulenn:codex/recursive-hook-nil-input
Open

Fulenn wants to merge 1 commit into
go-viper:mainfrom
Fulenn:codex/recursive-hook-nil-input

Conversation

@Fulenn

@Fulenn Fulenn commented Sep 17, 2026 •

Copy link
Copy Markdown

With DecodeNil: true, composing StringToTimeDurationHookFunc() followed by RecursiveStructToMapHookFunc() panics when decoding a map containing a nil value. The first hook passes nil through, ComposeDecodeHookFunc turns it into an invalid reflect.Value, and the recursive hook calls Interface() on that value.

Use the existing safeInterface helper for the recursive hook's non-struct passthrough. The regression test exercises standalone and composed hooks, checks that the nil entry is preserved, and checks that nested structs still become maps.

Validation on Go 1.27.1:

  • The new composed-hook regression failed with reflect: call of reflect.Value.Interface on zero Value before the fix and passes afterward.
  • go test -race -v -shuffle=on ./... passed.
  • go vet ./... passed.
  • golangci-lint fmt --diff and git diff --check passed.
  • golangci-lint run --new-from-rev=origin/main --timeout=2m reported zero issues. The full local lint run with v2.13.2 reports three govet findings about reflect.Ptr in unchanged mapstructure.go; the same three findings reproduce in a clean worktree of upstream main (52aa5c6).

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.

1 participant