Repository navigation
fix(read): prevent panic on negative slice index in a key path - #221
Merged
Merged
Conversation
GetValue and Exists validated only the upper bound of a numeric key-path segment, so a negative index such as "arr.-1" passed the check and indexed the slice with -1, causing an index-out-of-range panic. Add a lower-bound check (i < 0) to the []int, []string and []any cases in both functions.
dualfroz
force-pushed
the
dualfroz/negative-index-panic
branch
from
September 5, 2026 22:44
4ebbe6d to
8a81b3b
Compare
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
Looking up a key whose path contains a negative array index panics instead of
reporting "not found":
Key paths frequently come from user input or configuration, so a stray
negative index crashes the caller.
Root cause
read.go, in bothGetValueandExists, the slice cases parse the pathsegment with
strconv.Atoiand validate only the upper bound:strconv.Atoi("-1")succeeds withi == -1, which passeslen(typeData) <= i(
3 <= -1is false), sotypeData[-1]is evaluated and panics. This affectsthe
[]int,[]stringand[]anycases in both functions (six sites).Fix
Add a lower-bound check (
i < 0) to every slice-index guard so a negativeindex is treated as not found:
Test
Added
TestConfig_GetValue_negativeIndexinread_test.go, asserting that bothGetValue("arr.-1")andExists("arr.-1")do not panic and report not found.go test ./...-> ok (PASS, exit 0, all packages)go vet ./...-> exit 0go build ./...-> exit 0gofmt -l read.go read_test.go-> cleanCounterfactual: reverting
read.gomakes the test fail withPanic value: runtime.boundsError{x:-1, y:3, ...}.