From d5155ee9cfbbfedbd96cf67fdb367ad1beb96ec4 Mon Sep 17 00:00:00 2001 From: joysinleung Date: Tue, 4 Aug 2026 20:27:25 +0800 Subject: [PATCH 1/3] Harden inline readLength() array decode sites against declared-length OOM (R2-1) Folds the same checkLength remaining-buffer guard used by startArray() (PR #7733) into the three inline BC_ARRAY decode sites that never call startArray() and therefore pre-allocated new JSONArray(len)/new ArrayList(len) with no remaining-buffer check: - readObject() map-value branch (~392) - readAny() value branch (~927) - readArray() element branch (~1099) A 6-byte BC_ARRAY payload declaring ~268M elements against a tiny buffer (e.g. {A4 48 0F FF FF FF}) previously pre-allocated a ~1GiB Object[] and OOM'd; it now throws JSONException: array length out of range. Add regression tests covering all three paths (Issue7669): testUntypedArrayDeclaredLengthOOM_readAny (JSONB.parse -> readAny) testUntypedArrayDeclaredLengthOOM_readObject (JSONB.parseObject -> readObject) testUntypedArrayDeclaredLengthOOM_readArray (JSONB.parseArray -> readArray) Follow-up to PR #7733, same 7669 series (7696/7705/7733). --- .../alibaba/fastjson2/JSONReaderJSONB.java | 6 ++ .../alibaba/fastjson2/issues/Issue7669.java | 79 +++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java b/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java index fcc64f36e6..621779e56d 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java @@ -404,6 +404,8 @@ public Map readObject() { len = valueType - BC_ARRAY_FIX_MIN; } + checkLength(len, offset, end, "array length"); + if (len == 0) { if ((features & Feature.UseNativeObject.mask) != 0) { value = new ArrayList<>(); @@ -927,6 +929,8 @@ public Object readAny() { ? readLength() : type - BC_ARRAY_FIX_MIN; + checkLength(len, offset, end, "array length"); + if (len == 0) { if ((context.features & Feature.UseNativeObject.mask) != 0) { return new ArrayList<>(); @@ -1098,6 +1102,8 @@ public List readArray() { ? readLength() : valueType - BC_ARRAY_FIX_MIN; + checkLength(len, offset, end, "array length"); + if (len == 0) { if ((context.features & Feature.UseNativeObject.mask) != 0) { value = new ArrayList<>(); diff --git a/core/src/test/java/com/alibaba/fastjson2/issues/Issue7669.java b/core/src/test/java/com/alibaba/fastjson2/issues/Issue7669.java index 097b91672d..ef22f2801e 100644 --- a/core/src/test/java/com/alibaba/fastjson2/issues/Issue7669.java +++ b/core/src/test/java/com/alibaba/fastjson2/issues/Issue7669.java @@ -7,6 +7,8 @@ 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; @@ -189,4 +191,81 @@ 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. + assertThrows(JSONException.class, () -> JSONB.parse(UNTYPED_ARRAY_MAX_LEN)); + } + + @Test + public void testUntypedArrayDeclaredLengthOOM_readObject() { + // A map whose value is an int[] (32 elems -> BC_ARRAY + BC_INT32 length). + // Corrupt the declared length, then parse as Object.class so readObject() + // takes the map-value BC_ARRAY branch (readLength). + Map m = new HashMap<>(); + m.put("k", new int[32]); + byte[] bytes = JSONB.toBytes(m); + corruptFirstArrayLength(bytes); + assertThrows(JSONException.class, () -> JSONB.parseObject(bytes, Object.class)); + } + + @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)); + } } From e84a1985211839f84e10436ed81b6462e9281fae Mon Sep 17 00:00:00 2001 From: joysinleung Date: Thu, 13 Aug 2026 21:17:10 +0800 Subject: [PATCH 2/3] Address review round 1: unify guard spelling, fold BC_BINARY inline copies, hot/cold split, pin test messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - R1-1: testUntypedArrayDeclaredLengthOOM_readObject now routes through the untyped JSONB.parseObject(bytes) overload so it actually exercises the readObject() map-value BC_ARRAY guard (line 407); the Object.class variant short-circuited through readAny() - R1-2: pin the guard's own message ("array length out of range") in the untyped tests so a downstream generic JSONException cannot mask a removed guard - R1-3: hot/cold split checkLength() — the two comparisons stay inlinable; the message StringBuilder chain moved to throwLengthOutOfRange() - R1-4: fold the three remaining inline BC_BINARY length checks (readAny/readBinary/readBigInteger) into checkLength(..., "BC_BINARY length"), unifying the message format - R1-5: route all five array-length guard sites through checkArrayLen() (startArray x2 + readObject/readAny/readArray) — single spelling Issue7669: 20/20 pass. Full core suite: only pre-existing Chinese-path resource failures (CSVTest2/CSVReaderTest4/Issue3559) remain, reproduced on pristine d5155ee — unrelated to this PR. --- .../alibaba/fastjson2/JSONReaderJSONB.java | 29 ++++++++++--------- .../alibaba/fastjson2/issues/Issue7669.java | 17 +++++++---- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java b/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java index 621779e56d..0907f37f86 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java @@ -404,7 +404,7 @@ public Map readObject() { len = valueType - BC_ARRAY_FIX_MIN; } - checkLength(len, offset, end, "array length"); + checkArrayLen(len, offset, end); if (len == 0) { if ((features & Feature.UseNativeObject.mask) != 0) { @@ -697,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; @@ -929,7 +927,7 @@ public Object readAny() { ? readLength() : type - BC_ARRAY_FIX_MIN; - checkLength(len, offset, end, "array length"); + checkArrayLen(len, offset, end); if (len == 0) { if ((context.features & Feature.UseNativeObject.mask) != 0) { @@ -1102,7 +1100,7 @@ public List readArray() { ? readLength() : valueType - BC_ARRAY_FIX_MIN; - checkLength(len, offset, end, "array length"); + checkArrayLen(len, offset, end); if (len == 0) { if ((context.features & Feature.UseNativeObject.mask) != 0) { @@ -1467,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) { 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); } } + 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(); @@ -3736,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; @@ -4510,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; diff --git a/core/src/test/java/com/alibaba/fastjson2/issues/Issue7669.java b/core/src/test/java/com/alibaba/fastjson2/issues/Issue7669.java index ef22f2801e..792faac495 100644 --- a/core/src/test/java/com/alibaba/fastjson2/issues/Issue7669.java +++ b/core/src/test/java/com/alibaba/fastjson2/issues/Issue7669.java @@ -13,6 +13,7 @@ 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") @@ -236,20 +237,26 @@ private static void corruptFirstArrayLength(byte[] bytes) { 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. - assertThrows(JSONException.class, () -> JSONB.parse(UNTYPED_ARRAY_MAX_LEN)); + // 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)); + assertTrue(ex.getMessage().contains("array length out of range")); } @Test public void testUntypedArrayDeclaredLengthOOM_readObject() { // A map whose value is an int[] (32 elems -> BC_ARRAY + BC_INT32 length). - // Corrupt the declared length, then parse as Object.class so readObject() - // takes the map-value BC_ARRAY branch (readLength). + // 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 m = new HashMap<>(); m.put("k", new int[32]); byte[] bytes = JSONB.toBytes(m); corruptFirstArrayLength(bytes); - assertThrows(JSONException.class, () -> JSONB.parseObject(bytes, Object.class)); + JSONException ex = assertThrows(JSONException.class, () -> JSONB.parseObject(bytes)); + assertTrue(ex.getMessage().contains("array length out of range")); } @Test From c8fbd5b9a8c1574ae49799284c0b4359ec4fdd1e Mon Sep 17 00:00:00 2001 From: Shaojin Wen Date: Sat, 17 Oct 2026 20:47:51 +0800 Subject: [PATCH 3/3] fix: bound skipValue()/skipName() declared-length reads (review R2-5) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit skipValue() and skipName() consumed stream-declared lengths with no remaining-buffer check — the same defect class this PR closes elsewhere, flagged as the round-2 Critical: - BC_BINARY / long-string arms: checkLength before offset += - BC_OBJECT loop: fail closed at end of input, checkLength in the length-prefixed item arms (a negative length previously rewound offset and spun the loop forever on a 13-byte payload) - BC_ARRAY arm: checkArrayLen + fail closed at end of input, checkLength in the length-prefixed element arm - skipName() long-string arm: checkLength before offset += - readObject()/readAny() BC_ARRAY arms: reject a stream that ends right after the BC_ARRAY type byte (R2-4) codeSize (javap): skipValue 897 -> 1043, skipName 154 -> 172; both methods stay on the same side of the C1/C2 inline thresholds, so no inlining regression — the added work is two compares per skip. Tests: 13 new cases in Issue7669 replaying the three probe-verified failure shapes (huge BC_BINARY declared length, negative string length rewind, truncated array/object) plus valid-payload acceptance through skipValue() and the JSONValidator path. --- .../alibaba/fastjson2/JSONReaderJSONB.java | 22 ++ .../alibaba/fastjson2/issues/Issue7669.java | 188 ++++++++++++++++++ 2 files changed, 210 insertions(+) diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java b/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java index 0df05b188f..b6311d56d2 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java @@ -422,6 +422,9 @@ public Map readObject() { offset++; int len; if (valueType == BC_ARRAY) { + if (offset >= end) { + throw error("malformed JSONB input: truncated array"); + } byte itemType = bytes[offset]; if (isInt32Num(itemType)) { offset++; @@ -946,6 +949,9 @@ public Object readAny() { } if (type >= BC_ARRAY_FIX_MIN && type <= BC_ARRAY) { + if (type == BC_ARRAY && offset >= end) { + throw error("malformed JSONB input: truncated array"); + } int len = type == BC_ARRAY ? readLength() : type - BC_ARRAY_FIX_MIN; @@ -2511,6 +2517,7 @@ public void skipValue() { return; case BC_BINARY: int byteslen = readInt32Value(); + checkLength(byteslen, offset, end, "BC_BINARY length"); offset += byteslen; return; case BC_STR_ASCII: @@ -2519,6 +2526,7 @@ public void skipValue() { case BC_STR_UTF16LE: case BC_STR_UTF16BE: int strlen = readInt32Value(); + checkLength(strlen, offset, end, "string length"); offset += strlen; return; case BC_TYPED_ANY: { @@ -2528,6 +2536,9 @@ public void skipValue() { } case BC_OBJECT: { while (true) { + if (offset >= end) { + throw error("malformed JSONB input: truncated object"); + } byte b = bytes[offset]; if (b == BC_OBJECT_END) { offset++; @@ -2540,11 +2551,15 @@ public void skipValue() { } else if (size == -1) { offset++; int len = readInt32Value(); + checkLength(len, offset, end, "object item length"); offset += len; } else { skipName(); } + if (offset >= end) { + throw error("malformed JSONB input: truncated object"); + } b = bytes[offset]; size = FIXED_TYPE_SIZE[b & 0xFF]; if (size > 0) { @@ -2552,6 +2567,7 @@ public void skipValue() { } else if (size == -1) { offset++; int len = readInt32Value(); + checkLength(len, offset, end, "object item length"); offset += len; } else { skipValue(); @@ -2594,13 +2610,18 @@ public void skipValue() { } else { itemCnt = type - BC_ARRAY_FIX_MIN; } + checkArrayLen(itemCnt, offset, end); for (int i = 0; i < itemCnt; ++i) { + if (offset >= end) { + throw error("malformed JSONB input: truncated array"); + } int size = FIXED_TYPE_SIZE[bytes[offset] & 0xFF]; if (size > 0) { offset += size; } else if (size == -1) { offset++; int len = readInt32Value(); + checkLength(len, offset, end, "array item length"); offset += len; } else { skipValue(); @@ -2628,6 +2649,7 @@ public boolean skipName() { || strtype == BC_STR_UTF16BE ) { strlen = readLength(); + checkLength(strlen, offset, end, "field name length"); offset += strlen; return true; } diff --git a/core/src/test/java/com/alibaba/fastjson2/issues/Issue7669.java b/core/src/test/java/com/alibaba/fastjson2/issues/Issue7669.java index 792faac495..85cf1759c6 100644 --- a/core/src/test/java/com/alibaba/fastjson2/issues/Issue7669.java +++ b/core/src/test/java/com/alibaba/fastjson2/issues/Issue7669.java @@ -3,6 +3,7 @@ import com.alibaba.fastjson2.JSONB; import com.alibaba.fastjson2.JSONException; import com.alibaba.fastjson2.JSONReader; +import com.alibaba.fastjson2.JSONValidator; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -12,6 +13,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -275,4 +277,190 @@ public void testUntypedArrayDeclaredLengthOOM_readArray() { }; assertThrows(JSONException.class, () -> JSONB.parseArray(bytes)); } + + // --- R2-5: skipValue()/skipName() consumed stream-declared lengths with no + // remaining-buffer check — the same defect class this PR closes elsewhere. + // The PR's checkLength/checkArrayLen guards are folded into the skip arms so + // a crafted payload fails as JSONException instead of AIOOBE, a rewind loop, + // or a billion-iteration spin. + + // 0x91 = BC_BINARY, 0x48 = BC_INT32, 0x7FFFFFFF = Integer.MAX_VALUE. + // skipValue's BC_BINARY arm reads the length via readInt32Value(), which has + // no 256MB cap — pin that the shared guard fires on the raw declared value. + static final byte[] SKIP_BINARY_MAX_LEN = { + (byte) 0x91, + (byte) 0x48, + (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF + }; + + // 0x79 = BC_STR_ASCII, 0x48 = BC_INT32, 0xFFFFFFFA = -6 + static final byte[] SKIP_STR_NEG_LEN = { + (byte) 0x79, + (byte) 0x48, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFA + }; + + // 0xA6 = BC_OBJECT, 0x4A = BC_STR_ASCII_FIX(1) name "x" (0x78), + // 0x79 = BC_STR_ASCII, 0x48 = BC_INT32, 0xFFFFFFFA = -6: before the fix + // offset rewound onto the type byte and the object loop re-dispatched the + // same bytes forever (probe-verified hang in review R2-5, scenario 2). + static final byte[] SKIP_OBJECT_NEG_ITEM_LEN = { + (byte) 0xA6, + (byte) 0x4A, 0x78, + (byte) 0x79, + (byte) 0x48, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFA + }; + + // 0xA6 = BC_OBJECT, 0x4A = fix-str(1) "x", 0xB1 = BC_TRUE — one complete + // entry, then the stream ends with no BC_OBJECT_END: the loop-top read must + // fail as JSONException, not AIOOBE. + static final byte[] SKIP_OBJECT_TRUNCATED = { + (byte) 0xA6, + (byte) 0x4A, 0x78, + (byte) 0xB1 + }; + + // 0x96 = BC_ARRAY_FIX(2 elems), 0xBE = BC_INT64 (fixed 9 bytes, 2 remain) + static final byte[] SKIP_ARRAY_TRUNCATED = { + (byte) 0x96, + (byte) 0xBE, 0x00, 0x00 + }; + + // 0x79 = BC_STR_ASCII, 0x48 = BC_INT32, 0x0FFFFFFF = 256MB-1 (passes the + // readLength() cap, exceeds the remaining buffer) + static final byte[] SKIP_NAME_MAX_LEN = { + (byte) 0x79, + (byte) 0x48, + (byte) 0x0F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF + }; + + // 0xA6 = BC_OBJECT, 0x4A = fix-str(1) unknown field "x", + // 0x91 = BC_BINARY, 0x48 = BC_INT32, 0x7FFFFFFF, 0xA5 = BC_OBJECT_END + static final byte[] BEAN_UNKNOWN_FIELD_BINARY_MAX_LEN = { + (byte) 0xA6, + (byte) 0x4A, 0x78, + (byte) 0x91, + (byte) 0x48, + (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xA5 + }; + + // 0xA6 = BC_OBJECT, 0x4A = fix-str(1) "x", 0xA4 = BC_ARRAY, then EOF: + // the readObject() map-value array branch read bytes[offset] for the item + // type before any bounds check (R2-4). + static final byte[] MAP_VALUE_ARRAY_TRUNCATED = { + (byte) 0xA6, + (byte) 0x4A, 0x78, + (byte) 0xA4 + }; + + @Test + public void testSkipValueBinaryDeclaredLength() { + try (JSONReader reader = JSONReader.ofJSONB(SKIP_BINARY_MAX_LEN)) { + JSONException ex = assertThrows(JSONException.class, reader::skipValue); + assertTrue(ex.getMessage().contains("BC_BINARY length out of range")); + } + } + + @Test + public void testSkipValueStringNegativeLength() { + try (JSONReader reader = JSONReader.ofJSONB(SKIP_STR_NEG_LEN)) { + JSONException ex = assertThrows(JSONException.class, reader::skipValue); + assertTrue(ex.getMessage().contains("string length out of range")); + } + } + + @Test + public void testSkipValueObjectNegativeItemLength() { + try (JSONReader reader = JSONReader.ofJSONB(SKIP_OBJECT_NEG_ITEM_LEN)) { + JSONException ex = assertThrows(JSONException.class, reader::skipValue); + assertTrue(ex.getMessage().contains("object item length out of range")); + } + } + + @Test + public void testSkipValueObjectTruncated() { + try (JSONReader reader = JSONReader.ofJSONB(SKIP_OBJECT_TRUNCATED)) { + JSONException ex = assertThrows(JSONException.class, reader::skipValue); + assertTrue(ex.getMessage().contains("truncated object")); + } + } + + @Test + public void testSkipValueArrayDeclaredLength() { + try (JSONReader reader = JSONReader.ofJSONB(ARRAY_PAYLOAD_MAX_LEN)) { + JSONException ex = assertThrows(JSONException.class, reader::skipValue); + assertTrue(ex.getMessage().contains("array length out of range")); + } + } + + @Test + public void testSkipValueArrayTruncated() { + try (JSONReader reader = JSONReader.ofJSONB(SKIP_ARRAY_TRUNCATED)) { + JSONException ex = assertThrows(JSONException.class, reader::skipValue); + assertTrue(ex.getMessage().contains("truncated array")); + } + } + + @Test + public void testSkipNameDeclaredLength() { + try (JSONReader reader = JSONReader.ofJSONB(SKIP_NAME_MAX_LEN)) { + JSONException ex = assertThrows(JSONException.class, reader::skipName); + assertTrue(ex.getMessage().contains("field name length out of range")); + } + } + + @Test + public void testUnknownFieldSkipDeclaredLength() { + // review R2-5 scenario 1: bean deserialization skips the unknown field + // "x" through skipValue(); the declared 0x7FFFFFFF binary must fail as + // JSONException, not ArrayIndexOutOfBoundsException. + assertThrows(JSONException.class, + () -> JSONB.parseObject(BEAN_UNKNOWN_FIELD_BINARY_MAX_LEN, Bean.class)); + } + + @Test + public void testValidatorRejectsMalformedSkipPayload() { + // review R2-5 scenario 2 reproduced through the validator path. + try (JSONReader reader = JSONReader.ofJSONB(SKIP_OBJECT_NEG_ITEM_LEN)) { + assertFalse(JSONValidator.from(reader).validate()); + } + } + + @Test + public void testReadObjectArrayValueTruncated() { + // JSONB.parse routes the top-level object through readAny()'s inline + // object handling; its BC_ARRAY arm must reject a stream that ends + // right after the BC_ARRAY type byte. + assertThrows(JSONException.class, () -> JSONB.parse(MAP_VALUE_ARRAY_TRUNCATED)); + } + + @Test + public void testReadObjectArrayValueTruncated_readObjectBranch() { + // The untyped parseObject overload routes through readObject(); its + // map-value array branch (R2-4) must fail the same truncated stream as + // JSONException, not AIOOBE. + assertThrows(JSONException.class, () -> JSONB.parseObject(MAP_VALUE_ARRAY_TRUNCATED)); + } + + @Test + public void testSkipValueValidPayloadStillWorks() { + Map m = new HashMap<>(); + m.put("bin", new byte[]{1, 2, 3}); + m.put("str", "hello"); + m.put("arr", new int[]{1, 2, 3}); + Map nested = new HashMap<>(); + nested.put("k", "v"); + m.put("obj", nested); + byte[] bytes = JSONB.toBytes(m); + try (JSONReader reader = JSONReader.ofJSONB(bytes)) { + reader.skipValue(); + assertTrue(reader.isEnd()); + } + } + + public static class Bean { + public int a; + } }