struct: return error instead of panic when MapTo receives a nil pointer (fixes #369)#383
Open
nghiack7 wants to merge 2 commits into
Open
struct: return error instead of panic when MapTo receives a nil pointer (fixes #369)#383nghiack7 wants to merge 2 commits into
nghiack7 wants to merge 2 commits into
Conversation
added 2 commits
May 9, 2026 14:54
Calling MapTo (or Section.MapTo) with a nil struct pointer caused a panic in mapToField because val.Elem() on a nil pointer returns a zero reflect.Value, and the subsequent val.Type() call on that zero value triggers a runtime panic. Add an IsValid check after the Pointer dereference and return a descriptive error, matching the expected behaviour of the method (returns error) instead of crashing the caller. Fixes go-ini#369
…ter structs - Change single if to for loop so chained/double pointers are fully dereferenced - Auto-initialize nil pointers via reflect.New instead of returning an error - Fixes go-ini#369 (nil pointer panic on MapTo) and go-ini#370 (slice of *Struct panic) Previously mapToField would panic when encountering a nil pointer inside a slice of struct pointers ([]*Peer pattern common in WireGuard-style configs). The fix mirrors encoding/json behavior: walk the pointer chain, allocating any nil links, then map into the concrete value.
Author
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.
Problem
Two related bugs in
mapToFieldwhen the target is a nil pointer or a slice of struct pointers:MapTopanics when the destination struct field is a nil pointer (e.g.var s *S; f.MapTo(&s))MapTopanics when mapping into a[]*Structfield (slice of struct pointers), common in WireGuard-style INI configs with repeated[Peer]sectionsBoth root causes are the same:
mapToFieldonly unwrapped one level of pointer indirection and never initialized nil pointers.Fix
Replace the single
if val.Kind() == reflect.Pointerblock with aforloop that:**T,***T, etc.)reflect.New(mirrorsencoding/jsonbehavior)Tests
Test_MapToNilPointer— regression for panic: nil struct pointer to MapTo #369; verifies nil pointer is auto-initialized and fields are populatedTest_MapToSliceOfStructPointers— regression for panic: slice of struct pointers #370; verifies[]*PeerwithAllowNonUniqueSectionsmaps correctlyFixes #369, fixes #370