-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[chore] Make mapstructure hooks safe against untyped nils #13001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mx-psi
merged 12 commits into
open-telemetry:main
from
mx-psi:mx-psi/reimplement-as-value-hooks
May 9, 2025
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ac3a9f2
[chore] Make all hooks be mapstructure.DecodeHookFuncValue
mx-psi fd6338d
Merge remote-tracking branch 'origin/main' into mx-psi/reimplement-as…
mx-psi 468b619
Make all hooks safe including ComposeDecodeHookFunc
mx-psi bc49eb7
Restore previous hooks and document deviations from upstream
mx-psi 9ce6914
More comments
mx-psi 5d424b3
make goporto
mx-psi cef7ee4
Specify we are talking about untyped nil values
mx-psi c0c6789
Make linter happy
mx-psi 07451a2
Make linter even happier
mx-psi 2d2df97
Move to a third_party folder
mx-psi 99a5f5f
Update confmap/confmap.go
mx-psi 94dd45c
SPDX identifier should mention MIT license and correct copyright
mx-psi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
mx-psi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| package internal // import "go.opentelemetry.io/collector/confmap/internal" | ||
|
|
||
| import ( | ||
| "errors" | ||
| "reflect" | ||
|
|
||
| "github.com/go-viper/mapstructure/v2" | ||
| ) | ||
|
|
||
| // typedDecodeHook takes a raw DecodeHookFunc (an any) and turns | ||
| // it into the proper DecodeHookFunc type, such as DecodeHookFuncType. | ||
| func typedDecodeHook(h mapstructure.DecodeHookFunc) mapstructure.DecodeHookFunc { | ||
| // Create variables here so we can reference them with the reflect pkg | ||
| var f1 mapstructure.DecodeHookFuncType | ||
| var f2 mapstructure.DecodeHookFuncKind | ||
| var f3 mapstructure.DecodeHookFuncValue | ||
|
|
||
| // Fill in the variables into this interface and the rest is done | ||
| // automatically using the reflect package. | ||
| potential := []any{f3, f1, f2} | ||
|
|
||
| v := reflect.ValueOf(h) | ||
| vt := v.Type() | ||
| for _, raw := range potential { | ||
| pt := reflect.ValueOf(raw).Type() | ||
| if vt.ConvertibleTo(pt) { | ||
| return v.Convert(pt).Interface() | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // cachedDecodeHook takes a raw DecodeHookFunc (an any) and turns | ||
| // it into a closure to be used directly | ||
| // if the type fails to convert we return a closure always erroring to keep the previous behavior | ||
| func cachedDecodeHook(raw mapstructure.DecodeHookFunc) func(reflect.Value, reflect.Value) (any, error) { | ||
| switch f := typedDecodeHook(raw).(type) { | ||
| case mapstructure.DecodeHookFuncType: | ||
| return func(from reflect.Value, to reflect.Value) (any, error) { | ||
| // CHANGE FROM UPSTREAM: check if from is valid and return nil if not | ||
| if !from.IsValid() { | ||
| return nil, nil | ||
| } | ||
| return f(from.Type(), to.Type(), from.Interface()) | ||
| } | ||
| case mapstructure.DecodeHookFuncKind: | ||
| return func(from reflect.Value, to reflect.Value) (any, error) { | ||
| // CHANGE FROM UPSTREAM: check if from is valid and return nil if not | ||
| if !from.IsValid() { | ||
| return nil, nil | ||
| } | ||
| return f(from.Kind(), to.Kind(), from.Interface()) | ||
| } | ||
| case mapstructure.DecodeHookFuncValue: | ||
| return func(from reflect.Value, to reflect.Value) (any, error) { | ||
| return f(from, to) | ||
| } | ||
| default: | ||
| return func(reflect.Value, reflect.Value) (any, error) { | ||
| return nil, errors.New("invalid decode hook signature") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // ComposeDecodeHookFunc creates a single DecodeHookFunc that | ||
| // automatically composes multiple DecodeHookFuncs. | ||
| // | ||
| // The composed funcs are called in order, with the result of the | ||
| // previous transformation. | ||
| // | ||
| // This is a copy of [mapstructure.ComposeDecodeHookFunc] but with | ||
| // validation added. | ||
| func ComposeDecodeHookFunc(fs ...mapstructure.DecodeHookFunc) mapstructure.DecodeHookFunc { | ||
| cached := make([]func(reflect.Value, reflect.Value) (any, error), 0, len(fs)) | ||
| for _, f := range fs { | ||
| cached = append(cached, cachedDecodeHook(f)) | ||
| } | ||
| return func(f reflect.Value, t reflect.Value) (any, error) { | ||
| var err error | ||
|
|
||
| // CHANGE FROM UPSTREAM: check if f is valid before calling f.Interface() | ||
| var data any | ||
| if f.IsValid() { | ||
| data = f.Interface() | ||
| } | ||
|
|
||
| newFrom := f | ||
| for _, c := range cached { | ||
| data, err = c(newFrom, t) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| newFrom = reflect.ValueOf(data) | ||
| } | ||
|
|
||
| return data, nil | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.