THRIFT-5383 TJSONProtocol Java readString throws on bounds check
Client: java
Patch: Aaron St. George
This closes #2366
diff --git a/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java b/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java
index 6bb49cb..95eb62c 100644
--- a/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java
+++ b/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java
@@ -972,9 +972,7 @@
@Override
public String readString() throws TException {
- String str = readJSONString(false).toString(StandardCharsets.UTF_8);
- getTransport().checkReadBytesAvailable(str.length() * getMinSerializedSize(TType.STRING));
- return str;
+ return readJSONString(false).toString(StandardCharsets.UTF_8);
}
@Override
diff --git a/lib/java/test/org/apache/thrift/protocol/TestTJSONProtocol.java b/lib/java/test/org/apache/thrift/protocol/TestTJSONProtocol.java
index c2ca1fa..ecbd101 100644
--- a/lib/java/test/org/apache/thrift/protocol/TestTJSONProtocol.java
+++ b/lib/java/test/org/apache/thrift/protocol/TestTJSONProtocol.java
@@ -45,4 +45,18 @@
assertEquals(expectedString, protocol.readString());
}
+
+ public void testExactlySizedBuffer() throws TException {
+ // Regression test for https://issues.apache.org/jira/browse/THRIFT-5383.
+ // Ensures that a JSON string can be read after writing to a buffer just
+ // large enough to contain it.
+ String inputString = "abcdefg";
+ TMemoryBuffer buffer = new TMemoryBuffer(inputString.length() + 2);
+
+ TJSONProtocol protocol = new TJSONProtocol(buffer);
+ protocol.writeString(inputString);
+ String outputString = protocol.readString();
+
+ assertEquals(inputString, outputString);
+ }
}