Skip to content

fix: reject content after the value in Parse - #75

Merged
swordqiu merged 1 commit into
yunionio:masterfrom
swordqiu:hotfix/qj-parse-trailing
Sep 16, 2026
Merged

swordqiu merged 1 commit into
yunionio:masterfrom
swordqiu:hotfix/qj-parse-trailing

Conversation

@swordqiu

Copy link
Copy Markdown
Member

变更

Parse 忽略尾随内容

Parse 解析出第一个值后把剩余内容直接丢掉。{"a":1}{"b":2}、{"a":1} trailing 之类输入都会「成功」返回第一个文档。现在校验剩余部分只有空白,否则返回错误。

前置的严格校验方看到的文档,与后端实际理解的文档因此保持一致。

ParseStream 保持原语义 —— 它是流式解析的入口,仍返回消费到的偏移量,ParseJsonStreams 不受影响。

tryStdMarshal 的两处 panic

对被序列化类型的 MarshalJSON 输出做严格校验后,解析失败会更容易出现,因此一并去掉这两个 panic:MarshalJSON 返回错误、或返回的文档无法解析时,记录日志并产出 null,而不是让整个进程崩溃。Marshal 没有 error 返回值,null 是唯一可用的降级结果。

说明:F10 原计划放在健壮性 PR 中,因与本 PR 存在依赖关系(严格校验会新增一条解析失败路径),移到此处一并提交,后续健壮性 PR 只覆盖 unmarshal 侧。

测试

新增 parse_trailing_test.go:

  • TestParseTrailingContent:7 种尾随内容必须报错,6 种尾随空白必须正常解析
  • TestParseStreamKeepsOffset:流式解析的偏移量语义未变
  • TestMarshalMarshalJSONFailure:MarshalJSON 返回错误或返回非 JSON 文档时,Marshal 不再 panic 并返回 null

go test ./... 全量通过。

Parse returned the first value and silently ignored whatever followed
it. Check that only whitespace is left over.

ParseStream still returns the offset it consumed, for json streams.

Also stop panicking in tryStdMarshal: a MarshalJSON that returns an
error, or a document that cannot be parsed, now logs and yields null.
@swordqiu
swordqiu merged commit 2b08100 into yunionio:master Sep 16, 2026
1 check passed
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