diff --git a/java/src/json/ext/StringEncoder.java b/java/src/json/ext/StringEncoder.java index ee262f9c..38fa3642 100644 --- a/java/src/json/ext/StringEncoder.java +++ b/java/src/json/ext/StringEncoder.java @@ -273,29 +273,33 @@ void encodeBasic(ByteList src) throws IOException { } } - protected boolean isUnsafeScriptTag(byte[] ptrBytes, int pos, int len) { + protected boolean isUnsafeScriptTag(byte[] ptrBytes, int pos, int len, int ptr) { int remaining = len - pos; - if ((remaining >= 4 && ByteBuffer.wrap(ptrBytes, pos, 4).equals(HTML_COMMENT))) { + if (remaining < 4) { + return false; + } + + if (ByteBuffer.wrap(ptrBytes, ptr + pos, 4).equals(HTML_COMMENT)) { return true; } pos++; remaining--; - if (ptrBytes[pos] == '/') { + if (ptrBytes[ptr + pos] == '/') { pos++; remaining--; } return ( remaining >= 6 && - (ptrBytes[pos + 0] | 0x20) == 's' && - (ptrBytes[pos + 1] | 0x20) == 'c' && - (ptrBytes[pos + 2] | 0x20) == 'r' && - (ptrBytes[pos + 3] | 0x20) == 'i' && - (ptrBytes[pos + 4] | 0x20) == 'p' && - (ptrBytes[pos + 5] | 0x20) == 't' + (ptrBytes[ptr + pos + 0] | 0x20) == 's' && + (ptrBytes[ptr + pos + 1] | 0x20) == 'c' && + (ptrBytes[ptr + pos + 2] | 0x20) == 'r' && + (ptrBytes[ptr + pos + 3] | 0x20) == 'i' && + (ptrBytes[ptr + pos + 4] | 0x20) == 'p' && + (ptrBytes[ptr + pos + 5] | 0x20) == 't' ); } @@ -332,7 +336,7 @@ void encode(ByteList src) throws IOException { break; } case 25: { - if (isUnsafeScriptTag(ptrBytes, pos, len)) { + if (isUnsafeScriptTag(ptrBytes, pos, len, ptr)) { beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1); escapeAscii(ch, scratch, hexdig); } else { diff --git a/java/src/json/ext/StringEncoderAsciiOnly.java b/java/src/json/ext/StringEncoderAsciiOnly.java index 7fee66f7..636d2dcb 100644 --- a/java/src/json/ext/StringEncoderAsciiOnly.java +++ b/java/src/json/ext/StringEncoderAsciiOnly.java @@ -57,7 +57,7 @@ void encode(ByteList src) throws IOException { break; } case 25: { - if (isUnsafeScriptTag(ptrBytes, pos, len)) { + if (isUnsafeScriptTag(ptrBytes, pos, len, ptr)) { beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1); escapeAscii(ch, scratch, hexdig); } else { diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb index 15434c80..c3087d25 100755 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -801,6 +801,10 @@ def run_script_safe_test(ascii_only) data = "\n" json = '"\\n\\u003c/script>"' assert_equal json, generate(data, script_safe: true, ascii_only: ascii_only) + + data = ("a" * 100 + "")[100..] # JRuby shared string. + json = '"\\u003c/script>"' + assert_equal json, generate(data, script_safe: true, ascii_only: ascii_only) end def test_backslash