From 0a9478a715ea766d627ff3a72cf7a4f033fe536f Mon Sep 17 00:00:00 2001 From: Scott Myron Date: Thu, 18 Sep 2025 21:15:40 -0500 Subject: [PATCH] implement a better fix for an out of bounds exception --- java/src/json/ext/SWARBasicStringEncoder.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/java/src/json/ext/SWARBasicStringEncoder.java b/java/src/json/ext/SWARBasicStringEncoder.java index 08cd2e47..02a3bfcd 100644 --- a/java/src/json/ext/SWARBasicStringEncoder.java +++ b/java/src/json/ext/SWARBasicStringEncoder.java @@ -23,9 +23,13 @@ void encode(ByteList src) throws IOException { int beg = 0; int pos = 0; - ByteBuffer bb = ByteBuffer.wrap(ptrBytes, 0, len); - while (ptr + pos + 8 <= len) { - long x = bb.getLong(ptr + pos); + // There are optimizations in JRuby where ptr > 0 will be true. For example, if we + // slice a string, the underlying byte array is the same, but the + // begin index and real size are different. When reading from the ptrBytes + // array, we need to always add ptr to the index. + ByteBuffer bb = ByteBuffer.wrap(ptrBytes, ptr, len); + while (pos + 8 <= len) { + long x = bb.getLong(pos); if (skipChunk(x)) { pos += 8; continue; @@ -43,8 +47,8 @@ void encode(ByteList src) throws IOException { } } - if (ptr + pos + 4 <= len) { - int x = bb.getInt(ptr + pos); + if (pos + 4 <= len) { + int x = bb.getInt(pos); if (skipChunk(x)) { pos += 4; }