fix(compose): validate underlying type after pointer dereference in validateStructOrMap - #1172
Open
Joker-0111-G wants to merge 1 commit into
Open
fix(compose): validate underlying type after pointer dereference in validateStructOrMap#1172Joker-0111-G wants to merge 1 commit into
Joker-0111-G wants to merge 1 commit into
Conversation
…alidateStructOrMap
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.
What type of PR is this?
fix
Check the PR title.
Translate the PR title into Chinese.
fix(compose): 修复 validateStructOrMap 对指针类型解引用后未校验底层类型的问题More detailed description for this PR
English
validateStructOrMapdereferences a pointer type viat = t.Elem()and then falls through to returntrueunconditionally, without checking the underlying type. As a result, any pointer type such as*intor*stringis incorrectly accepted as a valid struct/map type for field mapping.This fix checks that the dereferenced type is still a struct or map
(e.g.
*map[string]anyis valid, but*intis not), keeping the original behavior for non-pointer types unchanged.The bug was originally surfaced by
staticcheck(SA4006: "this value of t is never used"), which the project's CI does not run, so it went unnoticed.Added unit tests covering struct, map, pointer-to-struct, pointer-to-map,
pointer-to-scalar, scalar, pointer-to-string and slice types.
中文说明
validateStructOrMap在通过t = t.Elem()解引用指针类型之后直接无条件返回true,没有对解引用后的底层类型做合法性校验。这就造成
*int、*string这类普通指针会被错误判定为可用于字段映射的合法结构体/Map 类型。本次修复逻辑:指针解引用之后二次校验底层类型必须是 struct 或 map。
示例:
*map[string]any合法,*int、*string这类基础类型指针拦截不通过;非指针类型原有校验逻辑完全保留不变。该隐患最先是 staticcheck 规则 SA4006(变量t赋值后未使用)检测出来的,项目现有CI未接入 staticcheck,因此长期遗漏。
配套新增单元测试,覆盖场景:
普通结构体、普通Map、结构体指针、Map指针、基础类型指针、原生基础类型、string指针、切片类型。
(Optional) Which issue(s) this PR fixes:
N/A
(Optional) The PR that updates user documentation:
N/A