Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Unreleased

* Fix `IndexOutOfBoundsException` in the JRuby extension when encoding shared strings.

### 2025-09-18 (2.14.0)

* Add new `allow_duplicate_key` generator options. By default a warning is now emitted when a duplicated key is encountered.
Expand Down
8 changes: 4 additions & 4 deletions java/src/json/ext/SWARBasicStringEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ void encode(ByteList src) throws IOException {
int pos = 0;

ByteBuffer bb = ByteBuffer.wrap(ptrBytes, 0, len);
while (pos + 8 <= len) {
while (ptr + pos + 8 <= len) {
long x = bb.getLong(ptr + pos);
if (skipChunk(x)) {
pos += 8;
continue;
}
int chunkEnd = pos + 8;
while (pos < chunkEnd) {
int chunkEnd = ptr + pos + 8;
while (ptr + pos < chunkEnd) {
int ch = Byte.toUnsignedInt(ptrBytes[ptr + pos]);
int ch_len = ESCAPE_TABLE[ch];
if (ch_len > 0) {
Expand All @@ -43,7 +43,7 @@ void encode(ByteList src) throws IOException {
}
}

if (pos + 4 <= len) {
if (ptr + pos + 4 <= len) {
int x = bb.getInt(ptr + pos);
if (skipChunk(x)) {
pos += 4;
Expand Down
6 changes: 6 additions & 0 deletions test/json/json_encoding_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def test_generate
assert_equal @generated, JSON.generate(@utf_16_data, ascii_only: true)
end

def test_generate_shared_string
# Ref: https://github.com/ruby/json/issues/859
s = "01234567890"
assert_equal '"234567890"', JSON.dump(s[2..-1])
end

def test_unicode
assert_equal '""', ''.to_json
assert_equal '"\\b"', "\b".to_json
Expand Down
Loading