Skip to content

Validate array inputs before reusing destination values - #208

Open
x0Lazarus wants to merge 1 commit into
go-viper:mainfrom
x0Lazarus:fix/array-input-validation
Open

x0Lazarus wants to merge 1 commit into
go-viper:mainfrom
x0Lazarus:fix/array-input-validation

Conversation

@x0Lazarus

Copy link
Copy Markdown

Decoding an oversized slice into an array that already contains values can panic after changing part of the destination. For example, decoding []int{1, 2, 3} into [2]int{7, 8} panics instead of returning the length error that a zero-valued array receives. Arrays containing slices also skip these checks. Invalid scalar inputs can panic, and a string can be incorrectly accepted as its character bytes.

Check the input type and length before deciding whether to reuse the destination array. Invalid inputs now return the existing errors without changing that array, and weak conversions work regardless of its previous contents. Valid shorter inputs still preserve the remaining elements unless ZeroFields is enabled.

Regression tests cover populated arrays, arrays containing slices, weak conversions, nested-field errors and unchanged valid-input behavior. On Windows, the full shuffled suite passes on Go 1.18.10 and Go 1.27.1, with race detection on Go 1.27.1. Both builds and Go 1.27.1 vet pass, and the configured formatters report no changes.

Local limits: Go 1.18 vet reports the same two existing findings before and after this change; its race build cannot link with the local compiler. Golangci-lint 2.12.2 reports the same three existing reflect.Ptr findings on both revisions. The CI-pinned linter version and Linux matrix were not run locally.

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