Repository navigation
Harden inline readLength() array decode sites against declared-length OOM (follow-up to #7733) #7776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Harden inline readLength() array decode sites against declared-length OOM (follow-up to #7733) #7776
Changes from 2 commits
d5155ee
e84a198
9cd64ec
c8fbd5b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -404,6 +404,8 @@ public Map<String, Object> readObject() { | |
| len = valueType - BC_ARRAY_FIX_MIN; | ||
| } | ||
|
|
||
| checkArrayLen(len, offset, end); | ||
|
|
||
| if (len == 0) { | ||
| if ((features & Feature.UseNativeObject.mask) != 0) { | ||
| value = new ArrayList<>(); | ||
|
|
@@ -695,9 +697,7 @@ public Object readAny() { | |
| } | ||
| case BC_BINARY: { | ||
| int len = readLength(); | ||
| if (len < 0 || len > end - offset) { | ||
| throw new JSONException("BC_BINARY length out of range: " + len); | ||
| } | ||
| checkLength(len, offset, end, "BC_BINARY length"); | ||
| byte[] binary = Arrays.copyOfRange(this.bytes, offset, offset + len); | ||
| offset += len; | ||
| return binary; | ||
|
|
@@ -927,6 +927,8 @@ public Object readAny() { | |
| ? readLength() | ||
| : type - BC_ARRAY_FIX_MIN; | ||
|
|
||
| checkArrayLen(len, offset, end); | ||
|
Comment on lines
956
to
+959
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] No test exercises a truncated BC_ARRAY_FIX payload against the new inline Suggested fix — pin the inline-site rejection: assertThrows(JSONException.class, () -> JSONB.parse(new byte[]{(byte) 0x9B, 0x01}));and extend — qwen3.8-max via Qwen Code /review (v0.21.11) |
||
|
|
||
| if (len == 0) { | ||
| if ((context.features & Feature.UseNativeObject.mask) != 0) { | ||
| return new ArrayList<>(); | ||
|
|
@@ -1098,6 +1100,8 @@ public List readArray() { | |
| ? readLength() | ||
| : valueType - BC_ARRAY_FIX_MIN; | ||
|
|
||
| checkArrayLen(len, offset, end); | ||
|
|
||
| if (len == 0) { | ||
| if ((context.features & Feature.UseNativeObject.mask) != 0) { | ||
| value = new ArrayList<>(); | ||
|
|
@@ -1461,13 +1465,20 @@ static void checkArrayLen(int len, int offset, int end) { | |
| // `label` identifies the context in the error message (e.g. "array length", | ||
| // "BC_BIGINT length"). A declared length larger than the remaining buffer is | ||
| // malformed input and must be rejected before any pre-allocation. | ||
| // Hot/cold split (AGENTS.md MaxInlineSize gate): keep the two comparisons | ||
| // small enough to inline on every BC_ARRAY header decode; the exception | ||
| // message StringBuilder chain lives in the cold throw helper. | ||
| static void checkLength(int len, int offset, int end, String label) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] Fix — hot/cold split, leaving the guard at ~15–20 bytes: static void checkLength(int len, int offset, int end, String label) {
if (len < 0 || len > end - offset) {
throwLengthOutOfRange(len, offset, end, label);
}
}
static void throwLengthOutOfRange(int len, int offset, int end, String label) {
throw new JSONException(
label + " out of range at offset " + offset + "/" + end + ": " + len + ", available: " + (end - offset));
}— qwen3.8-max via Qwen Code /review (v0.21.10) |
||
| if (len < 0 || len > end - offset) { | ||
| throw new JSONException( | ||
| label + " out of range at offset " + offset + "/" + end + ": " + len + ", available: " + (end - offset)); | ||
| throwLengthOutOfRange(len, offset, end, label); | ||
| } | ||
| } | ||
|
Comment on lines
+1463
to
+1467
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] The declared-length pre-allocation OOM class this PR closes for BC_ARRAY/BC_BINARY/BC_BIGINT survives at the string decode sites: string lengths read via Suggested fix: checkLength(strlen, offset, end, "string length");immediately after each — qwen3.8-max via Qwen Code /review (v0.21.11) |
||
|
|
||
| static void throwLengthOutOfRange(int len, int offset, int end, String label) { | ||
| throw new JSONException( | ||
| label + " out of range at offset " + offset + "/" + end + ": " + len + ", available: " + (end - offset)); | ||
| } | ||
|
|
||
| public String error(byte type) { | ||
| StringBuilder buf = new StringBuilder(); | ||
|
|
||
|
|
@@ -3730,9 +3741,7 @@ public byte[] readBinary() { | |
| } | ||
|
|
||
| int len = readLength(); | ||
| if (len < 0 || len > end - offset) { | ||
| throw new JSONException("BC_BINARY length out of range: " + len); | ||
| } | ||
| checkLength(len, offset, end, "BC_BINARY length"); | ||
| byte[] bytes = new byte[len]; | ||
| System.arraycopy(this.bytes, offset, bytes, 0, len); | ||
| offset += len; | ||
|
|
@@ -4504,9 +4513,7 @@ private BigInteger readBigInteger0(byte type) { | |
| } | ||
| case BC_BINARY: { | ||
| int len = readInt32Value(); | ||
| if (len < 0 || len > end - offset) { | ||
| throw new JSONException("BC_BINARY length out of range: " + len); | ||
| } | ||
| checkLength(len, offset, end, "BC_BINARY length"); | ||
| byte[] buf = new byte[len]; | ||
| System.arraycopy(this.bytes, offset, buf, 0, len); | ||
| offset += len; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,10 +7,13 @@ | |
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.math.BigInteger; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| @Tag("regression") | ||
| @Tag("jsonb") | ||
|
|
@@ -189,4 +192,87 @@ public void testArrayDeclaredLengthNotAtBufferEnd() { | |
| assertThrows(JSONException.class, | ||
| () -> JSONB.parseObject(NESTED_ARRAY_BAD_LEN, int[][].class)); | ||
| } | ||
|
|
||
| // --- R2-1: the same declared-length pre-allocation OOM class, but through the | ||
| // untyped/nested BC_ARRAY decode paths that never call startArray(): | ||
| // readAny() (default branch), readObject() (map-value branch), readArray() | ||
| // (element branch). They read the length via readLength() and pre-allocate | ||
| // new JSONArray(len)/new ArrayList(len) with no remaining-buffer check. | ||
| // This change folds the same checkLength guard into those three inline sites. | ||
|
|
||
| // 0xA4 = BC_ARRAY, 0x48 = BC_INT32, 0x0FFFFFFF = 268,435,455 elements | ||
| static final byte[] UNTYPED_ARRAY_MAX_LEN = { | ||
| (byte) 0xA4, | ||
| (byte) 0x48, | ||
| (byte) 0x0F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF | ||
| }; | ||
|
|
||
| /** | ||
| * Corrupt the first BC_ARRAY (0xA4) + BC_INT32 (0x48) length field found in | ||
| * the payload to a huge value, so the declared length vastly exceeds the | ||
| * remaining buffer. Used to exercise the untyped/nested readLength() sites. | ||
| */ | ||
| private static void corruptFirstArrayLength(byte[] bytes) { | ||
| for (int i = 0; i + 5 < bytes.length; i++) { | ||
| if ((bytes[i] & 0xFF) == 0xA4) { // BC_ARRAY | ||
| // Rewrite the array's leading byte as BC_INT32 (0x48) so the inline | ||
| // readLength() site treats it as a 4-byte length-type, then overwrite | ||
| // those 4 bytes with 0x0FFFFFFF (268,435,455) — far larger than the | ||
| // remaining buffer — to exercise the checkLength guard placed at the | ||
| // inline readLength() decode site (readObject() map-value branch). | ||
| // The corrupt target is the inner array value, not the (BC_OBJECT) | ||
| // root, because the root is never encoded as BC_ARRAY. | ||
| bytes[i + 1] = (byte) 0x48; // BC_INT32 | ||
| bytes[i + 2] = (byte) 0x0F; | ||
| bytes[i + 3] = (byte) 0xFF; | ||
| bytes[i + 4] = (byte) 0xFF; | ||
| bytes[i + 5] = (byte) 0xFF; | ||
| return; | ||
| } | ||
| } | ||
| throw new IllegalStateException("BC_ARRAY not found in payload"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUntypedArrayDeclaredLengthOOM_readAny() { | ||
| // JSONB.parse with no target type dispatches to readAny(); its BC_ARRAY | ||
| // branch reads the length via readLength() and pre-allocates | ||
| // new JSONArray(len) — now guarded by checkLength. Pin the guard's own | ||
| // message: without the guard a downstream "readAny overflow" JSONException | ||
| // would also satisfy a type-only assertion, letting the fix rot silently. | ||
| JSONException ex = assertThrows(JSONException.class, () -> JSONB.parse(UNTYPED_ARRAY_MAX_LEN)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] Every new test asserts rejection of malformed input; none asserts acceptance of VALID input through the three inline guard sites (readObject/readAny/readArray). Suggested fix: add acceptance round-trips through the three inline sites asserting values — — qwen3.8-max via Qwen Code /review (v0.21.11) |
||
| assertTrue(ex.getMessage().contains("array length out of range")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUntypedArrayDeclaredLengthOOM_readObject() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] This test never exercises the Fix — route through assertThrows(JSONException.class, () -> JSONB.parseObject(bytes));(keep the — qwen3.8-max via Qwen Code /review (v0.21.10) |
||
| // A map whose value is an int[] (32 elems -> BC_ARRAY + BC_INT32 length). | ||
| // Corrupt the declared length, then parse via the untyped overload: | ||
| // JSONB.parseObject(bytes, Object.class) short-circuits through readAny() | ||
| // (the Object.class fast path) and would NOT exercise the readObject() | ||
| // map-value guard at JSONReaderJSONB.java:407 — the untyped overload does. | ||
| Map<String, Object> m = new HashMap<>(); | ||
| m.put("k", new int[32]); | ||
| byte[] bytes = JSONB.toBytes(m); | ||
| corruptFirstArrayLength(bytes); | ||
| JSONException ex = assertThrows(JSONException.class, () -> JSONB.parseObject(bytes)); | ||
| assertTrue(ex.getMessage().contains("array length out of range")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUntypedArrayDeclaredLengthOOM_readArray() { | ||
| // Top-level JSONB.parseArray dispatches to readArray(); a nested element | ||
| // that is itself a BC_ARRAY must have its declared length (read via the | ||
| // inline readLength() at the element branch, line ~1099) rejected by | ||
| // checkLength. | ||
| // 0xA4 = BC_ARRAY (outer, 1 element), 0x01 = outer declared length, | ||
| // 0xA4 = BC_ARRAY (inner element), 0x48 = BC_INT32 length-type, | ||
| // 0x0FFFFFFF = declares 268,435,455 inner elements (exceeds buffer). | ||
| byte[] bytes = { | ||
| (byte) 0xA4, 0x01, | ||
| (byte) 0xA4, (byte) 0x48, | ||
| (byte) 0x0F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF | ||
| }; | ||
| assertThrows(JSONException.class, () -> JSONB.parseArray(bytes)); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Suggestion] This guard sits one line below the unchecked
bytes[offset]read of the array's length-type byte, so a payload truncated exactly at a BC_ARRAY header still escapes as a rawArrayIndexOutOfBoundsException— the guard never executes for that input shape. Failure scenario: probe-verified at this commit —JSONB.parseObject(new byte[]{(byte) 0xA6, 0x4A, 0x6B, (byte) 0xA4})throwsArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4at readObject:396 with no guard frame in the stack;JSONB.parse(new byte[]{(byte) 0xA4})andJSONB.parseArray(new byte[]{(byte) 0xA4, 0x01, (byte) 0xA4})AIOOBE insidereadLength:197. The control probe (the declared-length overflow payload) throws JSONException through the fullcheckArrayLen → checkLength → throwLengthOutOfRangechain, so the guard works for its claimed case but is unreachable one byte shorter; the same class applies at the sibling inline sites (~930 readAny, ~1103 readArray). The throwing reads are pre-existing (byte-identical at the merge base) and truncation AIOOBEs exist reader-wide for every unchecked marker, so this is scoped as an adjacent follow-up — but the three sites this PR guards still leak raw AIOOBE one line above their new guards.Suggested fix: reject before the length-type byte read — most leverage is one
offset >= endcheck insidereadLength()itself (covers the readAny/readArray callers), plus one beforebytes[offset]at readObject:396 (Int32Num fast path, never callsreadLength()); can land with the truncated-fix-array follow-up above since the fix shape is shared.— qwen3.8-max via Qwen Code /review (v0.21.11)