From 744f33baec14abc07b0cf527f25014a6d0cba7e4 Mon Sep 17 00:00:00 2001 From: Trevor Lacey <36862671+trevorlacey-msft@users.noreply.github.com> Date: Tue, 4 Dec 2018 13:59:36 -0800 Subject: [PATCH] Prevent infinite loop during proxy authentication (#986) --- .../src/http/client/http_client_winhttp.cpp | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/Release/src/http/client/http_client_winhttp.cpp b/Release/src/http/client/http_client_winhttp.cpp index 6976fbc771..e960058fd9 100644 --- a/Release/src/http/client/http_client_winhttp.cpp +++ b/Release/src/http/client/http_client_winhttp.cpp @@ -1723,23 +1723,27 @@ class winhttp_client final : public _http_client_communicator } } - static utility::string_t get_request_url(HINTERNET hRequestHandle) + static std::wstring get_request_url(HINTERNET hRequestHandle) { - DWORD urlSize{ 0 }; - if(FALSE == WinHttpQueryOption(hRequestHandle, WINHTTP_OPTION_URL, nullptr, &urlSize) || urlSize == 0) + std::wstring url; + auto urlSize = static_cast(url.capacity()) * 2; // use initial small string optimization capacity + for (;;) { - return U(""); - } - - auto urlwchar = new WCHAR[urlSize / sizeof(WCHAR)]; - - WinHttpQueryOption(hRequestHandle, WINHTTP_OPTION_URL, (void*)urlwchar, &urlSize); - - utility::string_t url(urlwchar); - - delete[] urlwchar; + url.resize(urlSize / sizeof(wchar_t)); + if (WinHttpQueryOption(hRequestHandle, WINHTTP_OPTION_URL, &url[0], &urlSize)) + { + url.resize(wcslen(url.c_str())); + return url; + } - return url; + const auto lastError = GetLastError(); + if (lastError != ERROR_INSUFFICIENT_BUFFER || urlSize == 0) + { + url.clear(); + url.shrink_to_fit(); + return url; + } + } } // Returns true if we handle successfully and resending the request