Skip to content

Commit dae3440

Browse files
authored
LHC: various minor fixes (#944)
1 parent 96a8156 commit dae3440

16 files changed

Lines changed: 95 additions & 30 deletions

Include/httpClient/httpClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ STDAPI HCWebSocketGetEventFunctions(
10831083
_Out_opt_ HCWebSocketMessageFunction* messageFunc,
10841084
_Out_opt_ HCWebSocketBinaryMessageFunction* binaryMessageFunc,
10851085
_Out_opt_ HCWebSocketCloseEventFunction* closeFunc,
1086-
_Out_ void** functionContext
1086+
_Out_opt_ void** functionContext
10871087
) noexcept;
10881088

10891089
#if HC_PLATFORM == HC_PLATFORM_WIN32 || HC_PLATFORM == HC_PLATFORM_GDK

Include/httpClient/httpProvider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ STDAPI HCHttpCallRequestGetTimeout(
284284
/// </remarks>
285285
STDAPI HCHttpCallRequestGetRetryDelay(
286286
_In_opt_ HCCallHandle call,
287-
_In_ uint32_t* retryDelayInSeconds
287+
_Out_ uint32_t* retryDelayInSeconds
288288
) noexcept;
289289

290290
/// <summary>

Source/Common/Win/utils_win.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ http_internal_string utf8_from_utf16(_In_reads_(size) PCWSTR utf16, size_t size)
3838
return "";
3939
}
4040

41+
if (size > INT_MAX)
42+
{
43+
throw std::exception("utf8_from_utf16 failed: input too large");
44+
}
45+
4146
// query for the buffer size
4247
auto queryResult = WideCharToMultiByte(
4348
CP_UTF8, WC_ERR_INVALID_CHARS,
@@ -49,13 +54,13 @@ http_internal_string utf8_from_utf16(_In_reads_(size) PCWSTR utf16, size_t size)
4954
{
5055
#if HC_TRACE_ERROR_ENABLE // to avoid unused variable warnings
5156
auto err = GetLastError();
52-
HC_TRACE_ERROR(HTTPCLIENT, "utf16_from_uft8 failed during buffer size query with error: %u", err);
57+
HC_TRACE_ERROR(HTTPCLIENT, "utf8_from_utf16 failed during buffer size query with error: %u", err);
5358
#endif
54-
throw std::exception("utf16_from_utf8 failed");
59+
throw std::exception("utf8_from_utf16 failed");
5560
}
5661

5762
// allocate the output buffer, queryResult is the required size
58-
http_internal_string utf8(static_cast<size_t>(queryResult), L'\0');
63+
http_internal_string utf8(static_cast<size_t>(queryResult), '\0');
5964
auto conversionResult = WideCharToMultiByte(
6065
CP_UTF8, WC_ERR_INVALID_CHARS,
6166
utf16, static_cast<int>(size),
@@ -66,9 +71,9 @@ http_internal_string utf8_from_utf16(_In_reads_(size) PCWSTR utf16, size_t size)
6671
{
6772
#if HC_TRACE_ERROR_ENABLE // to avoid unused variable warnings
6873
auto err = GetLastError();
69-
HC_TRACE_ERROR(HTTPCLIENT, "utf16_from_uft8 failed during conversion: %u", err);
74+
HC_TRACE_ERROR(HTTPCLIENT, "utf8_from_utf16 failed during conversion: %u", err);
7075
#endif
71-
throw std::exception("utf16_from_utf8 failed");
76+
throw std::exception("utf8_from_utf16 failed");
7277
}
7378

7479
return utf8;
@@ -82,6 +87,11 @@ http_internal_wstring utf16_from_utf8(_In_reads_(size) const char* utf8, size_t
8287
return L"";
8388
}
8489

90+
if (size > INT_MAX)
91+
{
92+
throw std::exception("utf16_from_utf8 failed: input too large");
93+
}
94+
8595
// query for the buffer size
8696
auto queryResult = MultiByteToWideChar(
8797
CP_UTF8, MB_ERR_INVALID_CHARS,
@@ -92,7 +102,7 @@ http_internal_wstring utf16_from_utf8(_In_reads_(size) const char* utf8, size_t
92102
{
93103
#if HC_TRACE_ERROR_ENABLE // to avoid unused variable warnings
94104
auto err = GetLastError();
95-
HC_TRACE_ERROR(HTTPCLIENT, "utf16_from_uft8 failed during buffer size query with error: %u", err);
105+
HC_TRACE_ERROR(HTTPCLIENT, "utf16_from_utf8 failed during buffer size query with error: %u", err);
96106
#endif
97107
throw std::exception("utf16_from_utf8 failed");
98108
}
@@ -108,7 +118,7 @@ http_internal_wstring utf16_from_utf8(_In_reads_(size) const char* utf8, size_t
108118
{
109119
#if HC_TRACE_ERROR_ENABLE // to avoid unused variable warnings
110120
auto err = GetLastError();
111-
HC_TRACE_ERROR(HTTPCLIENT, "utf16_from_uft8 failed during conversion: %u", err);
121+
HC_TRACE_ERROR(HTTPCLIENT, "utf16_from_utf8 failed during conversion: %u", err);
112122
#endif
113123
throw std::exception("utf16_from_utf8 failed");
114124
}

Source/Common/pch_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ HC_DECLARE_TRACE_AREA(WEBSOCKET);
160160
catch (std::exception const& e) { ::xbox::httpclient::detail::StdExceptionToResult(e, file, line); return errCode; } \
161161
catch (...) { ::xbox::httpclient::detail::UnknownExceptionToResult(file, line); return errCode; }
162162

163-
#define RETURN_IF_PERFORM_CALLED(call) if (call->performCalled) return E_HC_PERFORM_ALREADY_CALLED;
163+
#define RETURN_IF_PERFORM_CALLED(call) if (call->performCalled.load()) return E_HC_PERFORM_ALREADY_CALLED;
164164
#define TO_ULL(x) static_cast<unsigned long long>(x)
165165

166166
NAMESPACE_XBOX_HTTP_CLIENT_DETAIL_BEGIN

Source/Common/uri.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,15 @@ String Uri::ToString() const
289289
break;
290290
case '%':
291291
{
292-
if (chunkEnd > urlPart.size() - 3) // a % encoding is 3 characters long
292+
if (chunkEnd + 3 > urlPart.size()) // a % encoding is 3 characters long
293293
{
294-
//THROW(E_INVALIDARG, "Invalid % encode in url encoded string");
294+
return http_internal_string();
295295
}
296296

297297
uint8_t value = 0;
298298
if (!HexDecodePair(urlPart[chunkEnd + 1], urlPart[chunkEnd + 2], value))
299299
{
300-
//THROW(E_INVALIDARG, "Invalid value for % encode in url encoded string");
300+
return http_internal_string();
301301
}
302302

303303
decoded.push_back(value);
@@ -619,6 +619,12 @@ bool Uri::ParsePort(String const& uri, String::const_iterator& it)
619619
return false;
620620
}
621621

622+
if (portV > 65535)
623+
{
624+
HC_TRACE_WARNING(HTTPCLIENT, "Port out of range in URI.");
625+
return false;
626+
}
627+
622628
m_port = static_cast<uint16_t>(portV);
623629
it = portEnd;
624630

Source/Global/global.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ void http_singleton::set_retry_state(
232232

233233
http_retry_after_api_state http_singleton::get_retry_state(_In_ uint32_t retryAfterCacheId)
234234
{
235-
auto it = m_retryAfterCache.find(retryAfterCacheId); // STL is multithread read safe
235+
std::lock_guard<std::recursive_mutex> lock(m_retryAfterCacheLock);
236+
auto it = m_retryAfterCache.find(retryAfterCacheId);
236237
if (it != m_retryAfterCache.end())
237238
{
238239
return it->second; // returning a copy of state struct

Source/HTTP/WinHttp/winhttp_connection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ void WinHttpConnection::callback_status_data_available(
10681068
nullptr))
10691069
{
10701070
DWORD dwError = GetLastError();
1071-
HC_TRACE_ERROR(HTTPCLIENT, "WinHttpConnection [ID %llu] [TID %ul] WinHttpReadData errorcode %d", TO_ULL(HCHttpCallGetId(pRequestContext->m_call)), GetCurrentThreadId(), GetLastError());
1071+
HC_TRACE_ERROR(HTTPCLIENT, "WinHttpConnection [ID %llu] [TID %ul] WinHttpReadData errorcode %d", TO_ULL(HCHttpCallGetId(pRequestContext->m_call)), GetCurrentThreadId(), dwError);
10721072
pRequestContext->m_lock.unlock();
10731073
pRequestContext->complete_task(E_FAIL, HRESULT_FROM_WIN32(dwError));
10741074
return;

Source/HTTP/WinHttp/winhttp_connection.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ class websocket_message_buffer
9797

9898
if (dataByteCount > m_bufferByteCapacity)
9999
{
100-
newBuffer = static_cast<uint8_t*>(http_memory::mem_alloc(dataByteCount));
100+
// Allocate +1 for possible null terminator on text WebSocket messages
101+
newBuffer = static_cast<uint8_t*>(http_memory::mem_alloc(dataByteCount + 1));
101102
if (newBuffer != nullptr)
102103
{
103104
if (m_buffer != nullptr)

Source/HTTP/XMLHttp/xmlhttp_http_task.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ xmlhttp_http_task::xmlhttp_http_task(
2626
xmlhttp_http_task::~xmlhttp_http_task()
2727
{
2828
m_hRequest = nullptr;
29-
if (SUCCEEDED(m_hrCoInit))
29+
if (m_hrCoInit == S_OK)
3030
{
3131
CoUninitialize();
3232
}

Source/HTTP/compression.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ void Compression::CompressToGzip(uint8_t* inData, size_t inDataSize, HCCompressi
2929
stream.opaque = Z_NULL;
3030

3131
// deflateInit will use zlib (deflate) compression, so deflateInit2 with these flags is required for GZIP Compression
32-
deflateInit2(&stream, compressionLevelValue, Z_DEFLATED, WINDOWBITS | GZIP_ENCODING, 8, Z_DEFAULT_STRATEGY);
32+
int initResult = deflateInit2(&stream, compressionLevelValue, Z_DEFLATED, WINDOWBITS | GZIP_ENCODING, 8, Z_DEFAULT_STRATEGY);
33+
if (initResult != Z_OK)
34+
{
35+
return;
36+
}
3337

3438
stream.next_in = inData;
3539
stream.avail_in = static_cast<uInt>(inDataSize);
@@ -75,7 +79,11 @@ void Compression::DecompressFromGzip(uint8_t* inData, size_t inDataSize, http_in
7579
stream.opaque = Z_NULL;
7680

7781
// WINDOWBITS | GZIP_ENCODING - add 16 to decode only the gzip format
78-
inflateInit2(&stream, WINDOWBITS | GZIP_ENCODING);
82+
int initResult = inflateInit2(&stream, WINDOWBITS | GZIP_ENCODING);
83+
if (initResult != Z_OK)
84+
{
85+
return;
86+
}
7987

8088
stream.next_in = inData;
8189
stream.avail_in = static_cast<uInt>(inDataSize);

0 commit comments

Comments
 (0)