fix: reject content after the value in Parse - #75
Merged
Merged
Conversation
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.
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.
变更
Parse忽略尾随内容Parse解析出第一个值后把剩余内容直接丢掉。{"a":1}{"b":2}、{"a":1} trailing之类输入都会「成功」返回第一个文档。现在校验剩余部分只有空白,否则返回错误。前置的严格校验方看到的文档,与后端实际理解的文档因此保持一致。
ParseStream保持原语义 —— 它是流式解析的入口,仍返回消费到的偏移量,ParseJsonStreams不受影响。tryStdMarshal的两处 panic对被序列化类型的
MarshalJSON输出做严格校验后,解析失败会更容易出现,因此一并去掉这两个 panic:MarshalJSON返回错误、或返回的文档无法解析时,记录日志并产出null,而不是让整个进程崩溃。Marshal没有 error 返回值,null是唯一可用的降级结果。测试
新增
parse_trailing_test.go:TestParseTrailingContent:7 种尾随内容必须报错,6 种尾随空白必须正常解析TestParseStreamKeepsOffset:流式解析的偏移量语义未变TestMarshalMarshalJSONFailure:MarshalJSON返回错误或返回非 JSON 文档时,Marshal不再 panic 并返回nullgo test ./...全量通过。