From 7b47d0c9f0efea6da17ed6f57b4ef6438c862db2 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 7 Jul 2017 19:02:42 +0200 Subject: [PATCH] Fix memory leak of WINHTTP_PROXY_INFO fields Ensure that the strings inside this struct are always freed, which wasn't the case before. --- .../src/http/client/http_client_winhttp.cpp | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Release/src/http/client/http_client_winhttp.cpp b/Release/src/http/client/http_client_winhttp.cpp index 3eee959aff..3d0527880a 100644 --- a/Release/src/http/client/http_client_winhttp.cpp +++ b/Release/src/http/client/http_client_winhttp.cpp @@ -297,6 +297,24 @@ static DWORD ChooseAuthScheme( DWORD dwSupportedSchemes ) return 0; } +// Small RAII helper to ensure that the fields of this struct are always +// properly freed. +struct proxy_info : WINHTTP_PROXY_INFO +{ + proxy_info() + { + memset( this, 0, sizeof(WINHTTP_PROXY_INFO) ); + } + + ~proxy_info() + { + if ( lpszProxy ) + ::GlobalFree(lpszProxy); + if ( lpszProxyBypass ) + ::GlobalFree(lpszProxyBypass); + } +}; + // WinHTTP client. class winhttp_client : public _http_client_communicator { @@ -492,14 +510,13 @@ class winhttp_client : public _http_client_communicator http_request &msg = request->m_request; winhttp_request_context * winhttp_context = static_cast(request.get()); - WINHTTP_PROXY_INFO info; + proxy_info info; bool proxy_info_required = false; if( client_config().proxy().is_auto_discovery() ) { WINHTTP_AUTOPROXY_OPTIONS autoproxy_options; memset( &autoproxy_options, 0, sizeof(WINHTTP_AUTOPROXY_OPTIONS) ); - memset( &info, 0, sizeof(WINHTTP_PROXY_INFO) ); autoproxy_options.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT; autoproxy_options.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A;