You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by ns...@apache.org on 2016/03/18 15:48:29 UTC

[5/6] thrift git commit: THRIFT-3743 Java JSON protocol left in incorrect state when an exception is thrown during read or write operations

THRIFT-3743 Java JSON protocol left in incorrect state when an exception is thrown during read or write operations

This closes #952


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/1d4a4393
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/1d4a4393
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/1d4a4393

Branch: refs/heads/master
Commit: 1d4a4393c9a9396ec76c3ba674e0d6a65fe39cc1
Parents: b3a42dd
Author: Tyler Treat <ty...@webfilings.com>
Authored: Mon Mar 14 13:27:54 2016 -0500
Committer: Nobuaki Sukegawa <ns...@apache.org>
Committed: Fri Mar 18 22:37:02 2016 +0900

----------------------------------------------------------------------
 .../src/org/apache/thrift/protocol/TJSONProtocol.java     |  9 +++++++++
 .../org/apache/thrift/protocol/TSimpleJSONProtocol.java   | 10 ++++++++++
 2 files changed, 19 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/1d4a4393/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java
----------------------------------------------------------------------
diff --git a/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java b/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java
index 12341ab..fd54fdf 100644
--- a/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java
+++ b/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java
@@ -308,6 +308,13 @@ public class TJSONProtocol extends TProtocol {
     context_ = contextStack_.pop();
   }
 
+  // Reset the context stack to its initial state
+  private void resetContext() {
+    while (!contextStack_.isEmpty()) {
+      popContext();
+    }
+  }
+
   /**
    * Constructor
    */
@@ -503,6 +510,7 @@ public class TJSONProtocol extends TProtocol {
 
   @Override
   public void writeMessageBegin(TMessage message) throws TException {
+    resetContext(); // THRIFT-3743
     writeJSONArrayStart();
     writeJSONInteger(VERSION);
     try {
@@ -854,6 +862,7 @@ public class TJSONProtocol extends TProtocol {
 
   @Override
   public TMessage readMessageBegin() throws TException {
+    resetContext(); // THRIFT-3743
     readJSONArrayStart();
     if (readJSONInteger() != VERSION) {
       throw new TProtocolException(TProtocolException.BAD_VERSION,

http://git-wip-us.apache.org/repos/asf/thrift/blob/1d4a4393/lib/java/src/org/apache/thrift/protocol/TSimpleJSONProtocol.java
----------------------------------------------------------------------
diff --git a/lib/java/src/org/apache/thrift/protocol/TSimpleJSONProtocol.java b/lib/java/src/org/apache/thrift/protocol/TSimpleJSONProtocol.java
index ffa139a..b24e421 100644
--- a/lib/java/src/org/apache/thrift/protocol/TSimpleJSONProtocol.java
+++ b/lib/java/src/org/apache/thrift/protocol/TSimpleJSONProtocol.java
@@ -143,6 +143,15 @@ public class TSimpleJSONProtocol extends TProtocol {
   }
 
   /**
+   * Reset the write context stack to its initial state.
+   */
+  protected void resetWriteContext() {
+    while (!writeContextStack_.isEmpty()) {
+      popWriteContext();
+    }
+  }
+
+  /**
    * Used to make sure that we are not encountering a map whose keys are containers
    */
   protected void assertContextIsNotMapKey(String invalidKeyType) throws CollectionMapKeyException {
@@ -159,6 +168,7 @@ public class TSimpleJSONProtocol extends TProtocol {
   }
 
   public void writeMessageBegin(TMessage message) throws TException {
+    resetWriteContext(); // THRIFT-3743
     trans_.write(LBRACKET);
     pushWriteContext(new ListContext());
     writeString(message.name);