From 45a372679de011d7bc3fecd053738cf505086b53 Mon Sep 17 00:00:00 2001 From: Roger Meier Date: Sun, 8 Jan 2012 21:44:44 +0000 Subject: [PATCH] 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 --- lib/delphi/src/Thrift.Protocol.JSON.pas | 40 +++++++++++++++++-------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/delphi/src/Thrift.Protocol.JSON.pas b/lib/delphi/src/Thrift.Protocol.JSON.pas index 34338d59..dfab86bc 100644 --- a/lib/delphi/src/Thrift.Protocol.JSON.pas +++ b/lib/delphi/src/Thrift.Protocol.JSON.pas @@ -125,6 +125,7 @@ type FReader : TLookaheadReader; // Push/pop a new JSON context onto/from the stack. + procedure ResetContextStack; procedure PushContext( aCtx : TJSONBaseContext); procedure PopContext; @@ -454,19 +455,6 @@ begin 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 @@ end; destructor TJSONProtocolImpl.Destroy; begin try + ResetContextStack; // free any contents FreeAndNil( FReader); FreeAndNil( FContext); FreeAndNil( FContextStack); @@ -491,6 +480,27 @@ begin 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 @@ end; procedure TJSONProtocolImpl.WriteMessageBegin( aMsg : IMessage); begin + ResetContextStack; // THRIFT-1473 + WriteJSONArrayStart; WriteJSONInteger(VERSION); @@ -980,6 +992,8 @@ end; function TJSONProtocolImpl.ReadMessageBegin: IMessage; begin + ResetContextStack; // THRIFT-1473 + result := TMessageImpl.Create; ReadJSONArrayStart; -- 2.17.1