fix: serialize ByteBuffer remaining view - #7817
Open
DarrenChangJR wants to merge 1 commit into
Open
Conversation
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 this PR does and why
Fastjson2 currently serializes heap-backed
ByteBuffervalues by returning theentire backing array. This ignores the buffer's
position,limit, andarrayOffset, so a window exposing[1,2,3]from[9,1,2,3,8]is serializedand parsed as all five bytes.
Direct and read-only buffers already serialize only their remaining view. This
change makes heap buffers follow the same rule:
arrayOffset + position .. arrayOffset + limitfor heap windowsand slices;
mutation and restoration of the source position.
Tests
The expanded
ByteBufferTestcovers:Validation performed:
ByteBufferTest;mvn -pl core -DskipTests validate;mvn -pl core testsuite: 7,982 tests, no failures or errors;heap, slice, and direct views.
Checklist
中文说明
修改内容和原因
Fastjson2 当前对有底层数组的
ByteBuffer直接返回完整array(),忽略position、limit和arrayOffset。因此,从[9,1,2,3,8]中仅暴露[1,2,3]的窗口,会被错误地序列化并反序列化为全部五个字节。Direct 和只读 buffer 已经只序列化 remaining view。本修改统一该语义:完整底层
数组仍走零拷贝快速路径;heap window 和 slice 只复制有效区间;没有可访问数组的
buffer 通过 duplicate 读取,避免临时修改原对象的 position。
测试
测试覆盖非零 position/较小 limit 的 heap buffer、带非零 array offset 的
slice、等价的 direct 和只读 view、文本 JSON 与 JSONB 往返,以及源 position
和 limit 不变。定向测试、Maven
validate和完整 7,982 个 core 测试均通过。