Skip to content

Commit a8ff0c8

Browse files
authored
Merge pull request #860 from samyron/sm/fix-swar-index-out-of-bounds-exception
fix issue reading off the end of the ByteBuffer if ptr > 0
2 parents 1d52d18 + 67ebabe commit a8ff0c8

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Unreleased
44

5+
* Fix `IndexOutOfBoundsException` in the JRuby extension when encoding shared strings.
6+
57
### 2025-09-18 (2.14.0)
68

79
* Add new `allow_duplicate_key` generator options. By default a warning is now emitted when a duplicated key is encountered.

java/src/json/ext/SWARBasicStringEncoder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ void encode(ByteList src) throws IOException {
2424
int pos = 0;
2525

2626
ByteBuffer bb = ByteBuffer.wrap(ptrBytes, 0, len);
27-
while (pos + 8 <= len) {
27+
while (ptr + pos + 8 <= len) {
2828
long x = bb.getLong(ptr + pos);
2929
if (skipChunk(x)) {
3030
pos += 8;
3131
continue;
3232
}
33-
int chunkEnd = pos + 8;
34-
while (pos < chunkEnd) {
33+
int chunkEnd = ptr + pos + 8;
34+
while (ptr + pos < chunkEnd) {
3535
int ch = Byte.toUnsignedInt(ptrBytes[ptr + pos]);
3636
int ch_len = ESCAPE_TABLE[ch];
3737
if (ch_len > 0) {
@@ -43,7 +43,7 @@ void encode(ByteList src) throws IOException {
4343
}
4444
}
4545

46-
if (pos + 4 <= len) {
46+
if (ptr + pos + 4 <= len) {
4747
int x = bb.getInt(ptr + pos);
4848
if (skipChunk(x)) {
4949
pos += 4;

test/json/json_encoding_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ def test_generate
3131
assert_equal @generated, JSON.generate(@utf_16_data, ascii_only: true)
3232
end
3333

34+
def test_generate_shared_string
35+
# Ref: https://github.com/ruby/json/issues/859
36+
s = "01234567890"
37+
assert_equal '"234567890"', JSON.dump(s[2..-1])
38+
end
39+
3440
def test_unicode
3541
assert_equal '""', ''.to_json
3642
assert_equal '"\\b"', "\b".to_json

0 commit comments

Comments
 (0)