Skip to content

fix: validate compact JSONB number continuations - #7822

Open
DarrenChangJR wants to merge 2 commits into
alibaba:mainfrom
DarrenChangJR:fix/jsonb-compact-number-bounds
Open

fix: validate compact JSONB number continuations#7822
DarrenChangJR wants to merge 2 commits into
alibaba:mainfrom
DarrenChangJR:fix/jsonb-compact-number-bounds

Conversation

@DarrenChangJR

Copy link
Copy Markdown

What this PR does and why

Compact JSONB integer and long encodings require one continuation byte after
their marker. When that byte is missing, getIntByte and getLongByte read
past the reader's logical end.

This affects more than untyped parsing:

  • 38 and d0 leak ArrayIndexOutOfBoundsException through 15 facade and
    direct number-reader paths each;
  • compact array/string lengths and integer values nested in containers use the
    same unchecked helpers;
  • a one-byte logical slice backed by {marker, 1} reads outside the slice and
    returns integer 1.

This change adds the logical-end check at the two shared helper boundaries.
Malformed inputs now consistently throw JSONException, while all callers
retain their existing offset handling. The already guarded three-byte compact
forms and valid encodings are unchanged.

Tests

  • compact int32 and int64 markers across parse, typed facade, any, primitive,
    boxed, number, big-number, float, and double readers;
  • compact array and UTF-8 string lengths;
  • compact values nested in arrays and objects;
  • slices whose backing array contains a byte after the logical end;
  • truncated three-byte compact controls;
  • valid int32/int64 compact boundary values;
  • focused JSONB group: 104 tests, no failures or errors;
  • clean full core suite: 7,981 tests, no failures, errors, skips, or dumps.

Exact upstream searches on 2026-08-22 found no active duplicate. Closed #3883
is about malformed text JSON, while closed #7669 / PR #7696 handles declared
BC_BIGINT lengths rather than truncated compact-number encodings.

中文说明

修改内容和原因

JSONB 的紧凑 int/long 编码在类型 marker 后还需要一个 continuation byte。该字节
缺失时,getIntBytegetLongByte 会越过 reader 的逻辑结束位置读取数据。

影响不只是在无类型解析中抛错:

  • 38d0 在各 15 条 facade/direct number-reader 路径中都会泄漏
    ArrayIndexOutOfBoundsException
  • array/string length 以及容器中的紧凑整数值共用同一组 helper;
  • 如果逻辑长度为 1 的 slice 后面在 backing array 中还有字节 1,reader 会越界读取
    并返回整数 1。

本修改在两个共享 helper 中按 reader 的逻辑 end 统一检查边界。畸形输入现在
稳定抛出 JSONException,所有调用方原有的 offset 处理保持不变。原本已有边界
保护的三字节紧凑编码和合法输入均不受影响。

回归测试覆盖 int32/int64 marker 的 parse、typed facade、any、primitive、boxed、
number、big-number、float、double 路径,array/string length,array/object 内嵌值,
带有范围外 backing byte 的 slice,三字节截断对照,以及合法紧凑边界值。JSONB
focused group 104 项和 clean full core 7,981 项测试全部通过,无 failure、error、
skip 或 dump。2026-08-22 的精确上游检索没有发现 active duplicate;#3883
#7669 / #7696 是不同根因。

@wenshao wenshao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not explored to full depth (tool budget reached): "agent 3b": 无**(所有计划内的核查均已完成)。.

中文说明

未探索到全部深度(达到工具调用预算):"agent 3b"无**(所有计划内的核查均已完成)。

— qwen3.8-max via Qwen Code /review (v0.22.0)

Comment thread core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java
Comment thread core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java
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.

[BUG] JSONB BC_BIGINT 声明大小 OOM [BUG] Multiple ArrayIndexOutOfBoundsException in JSONReaderUTF8.java, JSONReaderASCII.java, JSONReader.java

2 participants