Skip to content

fix(compose): validate underlying type after pointer dereference in validateStructOrMap - #1172

Open
Joker-0111-G wants to merge 1 commit into
cloudwego:mainfrom
Joker-0111-G:fix/validate-struct-or-map-ptr
Open

fix(compose): validate underlying type after pointer dereference in validateStructOrMap#1172
Joker-0111-G wants to merge 1 commit into
cloudwego:mainfrom
Joker-0111-G:fix/validate-struct-or-map-ptr

Conversation

@Joker-0111-G

@Joker-0111-G Joker-0111-G commented Aug 3, 2026

Copy link
Copy Markdown

What type of PR is this?

fix

Check the PR title.

  • This PR title match the format: (optional scope):
  • The description of this PR title is user-oriented and clear enough for others to understand.

Translate the PR title into Chinese.

fix(compose): 修复 validateStructOrMap 对指针类型解引用后未校验底层类型的问题

More detailed description for this PR

English

validateStructOrMap dereferences a pointer type via t = t.Elem() and then falls through to return true unconditionally, without checking the underlying type. As a result, any pointer type such as *int or *string is 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]any is valid, but *int is 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

@CLAassistant

CLAassistant commented Aug 3, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@Joker-0111-G Joker-0111-G changed the title fix(compose): validate underlying type after pointer dereference in v… fix(compose): validate underlying type after pointer dereference in validateStructOrMap Aug 3, 2026
@Joker-0111-G Joker-0111-G reopened this Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants