Skip to content

Fix: don't panic when squashing unexported embedded pointers + Regression test - #204

Open
auyer wants to merge 2 commits into
go-viper:mainfrom
auyer:main
Open

auyer wants to merge 2 commits into
go-viper:mainfrom
auyer:main

Conversation

@auyer

@auyer auyer commented Sep 17, 2026

Copy link
Copy Markdown

Hi,

I am a Debian contributor, and noticed a failure when running the tests for a package that uses mapstructure.

I added a regression test first (that fails), and the commit that fixes it after.

Decoding a map into a struct with an embedded pointer
to an unexported type tagged with mapstructure:",squash"
would panic with:

reflect: reflect.Value.Set using value obtained using unexported field

Add TestDecode_EmbeddedUnexportedPointerSquash_FromMapToStruct to
lock in the expected behaviour: Decode returns an error containing
"unsupported type for squash" rather than panicking.

This behaviour change was noticed by the
Test_EmbeddedPrivateStructPointer in anchore/fangs:
https://github.com/anchore/fangs/blob/d9f7358ffc5f9c1935fd3bbc2e5a16fb92ff5b6c/load_test.go#L731

Validate the behaviour of returning an error instead
of panicking when trying to set a value for an
unexported variable.

Decoding a map into a struct with an embedded pointer
to an unexported type tagged with `mapstructure:",squash"`
currently panics with:

`reflect: reflect.Value.Set using value obtained using unexported
field`

Add TestDecode_EmbeddedUnexportedPointerSquash_FromMapToStruct to
lock in the expected behaviour: Decode returns an error containing
"unsupported type for squash" rather than panicking.

This behaviour change was noticed by the
`Test_EmbeddedPrivateStructPointer` in anchore/fangs:
https://github.com/anchore/fangs/blob/d9f7358ffc5f9c1935fd3bbc2e5a16fb92ff5b6c/load_test.go#L731

Signed-off-by: Rafael Passos <rafael@rcpassos.me>
Guard the allocation with CanSet() and return the previous
"unsupported type for squash" error for fields that cannot be set, rather
than panicking.

Signed-off-by: Rafael Passos <rafael@rcpassos.me>
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