Repository navigation
Treat out-of-range slice indexes in struct paths as missing - #352
Merged
Merged
Conversation
gookit#351 made the map arm of StructData's path walker treat a missing entry as missing. The slice/array arm still called fv.Index without a bounds check, so Items.0.Name on an empty slice, or a negative index, panicked in TryGet, Get and Set, including through a requiredIf tag.
inhere
added a commit
that referenced
this pull request
Oct 29, 2026
tryGetRV treats a negative index as missing since #352, and Set keeps the same bound check now for its own walk. Set cannot reach that branch today: HasField rejects any path the field walker did not register, and a negative index is never registered, so the guard keeps the two walkers in sync if that gate ever widens. Tests: Set("Items.-1.Name") and Set("Fixed.-1.Name") return ErrNoField and leave the element untouched.
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.
#351 made the map arm of
StructData's path walker treat a missing entry as missing. The slice/array arm of the same loops (tryGetRVandSet) still callsfv.Index(index)without a bounds check, so a path into an empty slice, or a negative index, panics. That is also reachable from a struct tag alone:tryGetRVnow reports an out-of-range index as missing, the same way the map source does.SetreturnsErrNoFieldwhen an index that resolved earlier no longer exists, like the "entry removed before write" case #351 added for maps. Only inputs that used to panic change. A non-numeric segment still resolves to element 0 as before. I added a CHANGELOG line next to #351's.go test ./...(under UTC and Asia/Seoul),go vet ./...andgofmtpass on Go 1.26.3.golangci-lintreports the same findings as on master.Written with AI assistance (Claude); I have reviewed the change.