THRIFT-5088 Memory leak in TWinHttpTransport
Client: Delphi
Patch: Jens Geyer
diff --git a/lib/delphi/src/Thrift.Transport.WinHTTP.pas b/lib/delphi/src/Thrift.Transport.WinHTTP.pas
index 7a1b48f..5794b1c 100644
--- a/lib/delphi/src/Thrift.Transport.WinHTTP.pas
+++ b/lib/delphi/src/Thrift.Transport.WinHTTP.pas
@@ -309,7 +309,7 @@
http : IWinHTTPRequest;
pData : PByte;
len : Integer;
- error : Cardinal;
+ error, dwSize : Cardinal;
sMsg : string;
begin
http := CreateRequest;
@@ -334,7 +334,8 @@
end;
FInputStream := THTTPResponseStream.Create( http);
- UpdateKnownMessageSize( http.QueryTotalResponseSize);
+ if http.QueryTotalResponseSize( dwSize) // FALSE indicates "no info available"
+ then UpdateKnownMessageSize( dwSize);
end;
procedure TWinHTTPClientImpl.Write( const pBuf : Pointer; off, len : Integer);
diff --git a/lib/delphi/src/Thrift.Transport.pas b/lib/delphi/src/Thrift.Transport.pas
index af62548..1b8300c 100644
--- a/lib/delphi/src/Thrift.Transport.pas
+++ b/lib/delphi/src/Thrift.Transport.pas
@@ -579,7 +579,7 @@
// update only: message size can shrink, but not grow
ASSERT( KnownMessageSize <= MaxMessageSize);
if newSize > KnownMessageSize
- then TTransportExceptionEndOfFile.Create('MaxMessageSize reached');
+ then raise TTransportExceptionEndOfFile.Create('MaxMessageSize reached');
FKnownMessageSize := newSize;
FRemainingMessageSize := newSize;
diff --git a/lib/delphi/src/Thrift.WinHTTP.pas b/lib/delphi/src/Thrift.WinHTTP.pas
index d060066..5c07193 100644
--- a/lib/delphi/src/Thrift.WinHTTP.pas
+++ b/lib/delphi/src/Thrift.WinHTTP.pas
@@ -576,7 +576,7 @@
IWinHTTPConnection = interface;
IWinHTTPRequest = interface
- ['{7A8E7255-5440-4621-A8A8-1E9FFAA6D6FA}']
+ ['{7AE6F63F-49B6-436B-8AAB-159E58EB900A}']
function Handle : HINTERNET;
function Connection : IWinHTTPConnection;
function AddRequestHeader( const aHeader : string; const addflag : DWORD = WINHTTP_ADDREQ_FLAG_ADD) : Boolean;
@@ -589,7 +589,7 @@
function ReadData( const dwRead : DWORD) : TBytes; overload;
function ReadData( const pBuf : Pointer; const dwRead : DWORD) : DWORD; overload;
function QueryDataAvailable : DWORD;
- function QueryTotalResponseSize : DWORD;
+ function QueryTotalResponseSize( out dwSize : DWORD) : Boolean;
end;
IWinHTTPConnection = interface
@@ -709,7 +709,7 @@
function ReadData( const dwRead : DWORD) : TBytes; overload;
function ReadData( const pBuf : Pointer; const dwRead : DWORD) : DWORD; overload;
function QueryDataAvailable : DWORD;
- function QueryTotalResponseSize : DWORD;
+ function QueryTotalResponseSize( out dwSize : DWORD) : Boolean;
public
constructor Create( const aConnection : IWinHTTPConnection;
@@ -1212,20 +1212,20 @@
end;
-function TWinHTTPRequestImpl.QueryTotalResponseSize : DWORD;
+function TWinHTTPRequestImpl.QueryTotalResponseSize( out dwSize : DWORD) : Boolean;
var dwBytes, dwError, dwIndex : DWORD;
begin
dwBytes := SizeOf( result);
dwIndex := DWORD( WINHTTP_NO_HEADER_INDEX);
- if not WinHttpQueryHeaders( FHandle,
- WINHTTP_QUERY_CONTENT_LENGTH or WINHTTP_QUERY_FLAG_NUMBER,
- WINHTTP_HEADER_NAME_BY_INDEX,
- @result, dwBytes,
- dwIndex)
- then begin
+ result := WinHttpQueryHeaders( FHandle,
+ WINHTTP_QUERY_CONTENT_LENGTH or WINHTTP_QUERY_FLAG_NUMBER,
+ WINHTTP_HEADER_NAME_BY_INDEX,
+ @dwSize, dwBytes,
+ dwIndex);
+ if not result then begin
+ dwSize := MAXINT; // we don't know, just return something useful
dwError := GetLastError;
if dwError <> ERROR_WINHTTP_HEADER_NOT_FOUND then ASSERT(FALSE); // anything else would be an real error
- result := MAXINT; // we don't know
end;
end;