THRIFT-1473 JSON context stack may be left in an incorrect state when an exception is thrown during read or write operations
Patch: Jens Geyer
git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1228963 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/delphi/src/Thrift.Protocol.JSON.pas b/lib/delphi/src/Thrift.Protocol.JSON.pas
index 34338d5..dfab86b 100644
--- a/lib/delphi/src/Thrift.Protocol.JSON.pas
+++ b/lib/delphi/src/Thrift.Protocol.JSON.pas
@@ -125,6 +125,7 @@
FReader : TLookaheadReader;
// Push/pop a new JSON context onto/from the stack.
+ procedure ResetContextStack;
procedure PushContext( aCtx : TJSONBaseContext);
procedure PopContext;
@@ -454,19 +455,6 @@
end;
-procedure TJSONProtocolImpl.PushContext( aCtx : TJSONBaseContext);
-begin
- FContextStack.Push( FContext);
- FContext := aCtx;
-end;
-
-procedure TJSONProtocolImpl.PopContext;
-begin
- FreeAndNil(FContext);
- FContext := FContextStack.Pop;
-end;
-
-
constructor TJSONProtocolImpl.Create( aTrans : ITransport);
begin
inherited Create( aTrans);
@@ -482,6 +470,7 @@
destructor TJSONProtocolImpl.Destroy;
begin
try
+ ResetContextStack; // free any contents
FreeAndNil( FReader);
FreeAndNil( FContext);
FreeAndNil( FContextStack);
@@ -491,6 +480,27 @@
end;
+procedure TJSONProtocolImpl.ResetContextStack;
+begin
+ while FContextStack.Count > 0
+ do PopContext;
+end;
+
+
+procedure TJSONProtocolImpl.PushContext( aCtx : TJSONBaseContext);
+begin
+ FContextStack.Push( FContext);
+ FContext := aCtx;
+end;
+
+
+procedure TJSONProtocolImpl.PopContext;
+begin
+ FreeAndNil(FContext);
+ FContext := FContextStack.Pop;
+end;
+
+
procedure TJSONProtocolImpl.ReadJSONSyntaxChar( b : Byte);
var ch : Byte;
begin
@@ -668,6 +678,8 @@
procedure TJSONProtocolImpl.WriteMessageBegin( aMsg : IMessage);
begin
+ ResetContextStack; // THRIFT-1473
+
WriteJSONArrayStart;
WriteJSONInteger(VERSION);
@@ -980,6 +992,8 @@
function TJSONProtocolImpl.ReadMessageBegin: IMessage;
begin
+ ResetContextStack; // THRIFT-1473
+
result := TMessageImpl.Create;
ReadJSONArrayStart;