From 55f373e164d3f092dd6c7a56e3de6f90c4c6f3dc Mon Sep 17 00:00:00 2001 From: Duncan Horn <40036384+dunhor@users.noreply.github.com> Date: Thu, 29 Jun 2023 14:35:20 -0700 Subject: [PATCH] Squash to reset history (#343) --- include/wil/Tracelogging.h | 28 ++++++++++----------- include/wil/com.h | 6 ++--- include/wil/filesystem.h | 20 +++++++-------- include/wil/registry.h | 28 ++++++++++----------- include/wil/registry_helpers.h | 23 +++++++++-------- include/wil/resource.h | 16 +++++++----- include/wil/result.h | 7 ++++-- include/wil/result_macros.h | 46 ++++++++++++++++++++++++++-------- include/wil/win32_helpers.h | 30 +++++++++++----------- include/wil/winrt.h | 2 +- 10 files changed, 119 insertions(+), 87 deletions(-) diff --git a/include/wil/Tracelogging.h b/include/wil/Tracelogging.h index 1348ab597..b4ad40a52 100644 --- a/include/wil/Tracelogging.h +++ b/include/wil/Tracelogging.h @@ -239,18 +239,18 @@ namespace wil class StoredCallContextInfo : public wil::CallContextInfo { public: - StoredCallContextInfo() + StoredCallContextInfo() WI_NOEXCEPT { ::ZeroMemory(this, sizeof(*this)); } - StoredCallContextInfo(StoredCallContextInfo &&other) : + StoredCallContextInfo(StoredCallContextInfo &&other) WI_NOEXCEPT : StoredCallContextInfo() { operator=(wistd::move(other)); } - StoredCallContextInfo& operator=(StoredCallContextInfo &&other) + StoredCallContextInfo& operator=(StoredCallContextInfo &&other) WI_NOEXCEPT { contextId = other.contextId; contextName = other.contextName; @@ -262,7 +262,7 @@ namespace wil return *this; } - StoredCallContextInfo(StoredCallContextInfo const &other) : + StoredCallContextInfo(StoredCallContextInfo const &other) WI_NOEXCEPT : m_ownsMessage(false) { contextId = other.contextId; @@ -277,7 +277,7 @@ namespace wil } } - StoredCallContextInfo(_In_opt_ PCSTR staticContextName) : + StoredCallContextInfo(_In_opt_ PCSTR staticContextName) WI_NOEXCEPT : m_ownsMessage(false) { contextId = 0; @@ -285,7 +285,7 @@ namespace wil contextMessage = nullptr; } - StoredCallContextInfo(PCSTR staticContextName, _Printf_format_string_ PCSTR formatString, va_list argList) : + StoredCallContextInfo(PCSTR staticContextName, _Printf_format_string_ PCSTR formatString, va_list argList) WI_NOEXCEPT : StoredCallContextInfo(staticContextName) { SetMessage(formatString, argList); @@ -581,9 +581,9 @@ namespace wil Initialize(); } - TraceLoggingHProvider m_providerHandle; - bool m_ownsProviderHandle; - ErrorReportingType m_errorReportingType; + TraceLoggingHProvider m_providerHandle{}; + bool m_ownsProviderHandle{}; + ErrorReportingType m_errorReportingType{}; }; template< @@ -1389,10 +1389,10 @@ namespace wil #define __IMPLEMENT_CALLCONTEXT_CLASS(ActivityClassName) \ protected: \ - ActivityClassName(_In_ void **, PCSTR contextName, _In_opt_ _Printf_format_string_ PCSTR formatString, _In_opt_ va_list argList) : \ + ActivityClassName(_In_opt_ void **, PCSTR contextName, _In_opt_ _Printf_format_string_ PCSTR formatString, _In_opt_ va_list argList) : \ ActivityBase(contextName) \ { GetCallContext()->SetMessage(formatString, argList); StartActivity(); } \ - ActivityClassName(_In_ void **, PCSTR contextName) : \ + ActivityClassName(_In_opt_ void **, PCSTR contextName) : \ ActivityBase(contextName) \ { StartActivity(); } \ public: \ @@ -3315,8 +3315,8 @@ namespace wil if (m_fireEventThreadPoolTimer && !ProcessShutdownInProgress()) { // Note this will override any pending scheduled callback - FILETIME dueTime; - *reinterpret_cast(&dueTime) = -static_cast(m_fireEventDelay * 10000); + FILETIME dueTime{}; + *reinterpret_cast(&dueTime) = -static_cast(m_fireEventDelay) * 10000; SetThreadpoolTimer(m_fireEventThreadPoolTimer.get(), &dueTime, 0, 0); } } @@ -3325,7 +3325,7 @@ namespace wil wil::unique_threadpool_timer m_fireEventThreadPoolTimer; // The timer used to determine when to fire the next telemetry event (when it's fired based on a timer). - UINT m_fireEventDelay; + UINT m_fireEventDelay{}; DWORD const c_fireEventDelayLimit = 20 * 60 * 1000; // 20 minutes }; } // namespace details diff --git a/include/wil/com.h b/include/wil/com.h index ee9cfafa0..bf23175b0 100644 --- a/include/wil/com.h +++ b/include/wil/com.h @@ -2275,7 +2275,7 @@ namespace wil */ inline HRESULT stream_seek_nothrow(_In_ IStream* stream, long long offset, unsigned long from, _Out_opt_ unsigned long long* value = nullptr) { - LARGE_INTEGER amount; + LARGE_INTEGER amount{}; ULARGE_INTEGER landed{}; amount.QuadPart = offset; RETURN_IF_FAILED(stream->Seek(amount, from, value ? &landed : nullptr)); @@ -2360,8 +2360,8 @@ namespace wil */ inline HRESULT stream_copy_bytes_nothrow(_In_ IStream* source, _In_ IStream* target, unsigned long long amount, _Out_opt_ unsigned long long* pCopied = nullptr) { - ULARGE_INTEGER toCopy; - ULARGE_INTEGER copied; + ULARGE_INTEGER toCopy{}; + ULARGE_INTEGER copied{}; toCopy.QuadPart = amount; RETURN_IF_FAILED(source->CopyTo(target, toCopy, nullptr, &copied)); assign_to_opt_param(pCopied, copied.QuadPart); diff --git a/include/wil/filesystem.h b/include/wil/filesystem.h index 89fd3e484..a81ee7c9a 100644 --- a/include/wil/filesystem.h +++ b/include/wil/filesystem.h @@ -40,7 +40,7 @@ namespace wil // If there is a trailing slash ignore that in the search. auto const limitedLength = ((pathLength > 0) && (path[pathLength - 1] == L'\\')) ? (pathLength - 1) : pathLength; - PCWSTR result; + PCWSTR result = nullptr; auto const offset = FindStringOrdinal(FIND_FROMEND, path, static_cast(limitedLength), L"\\", 1, TRUE); if (offset == -1) { @@ -97,7 +97,7 @@ namespace wil { *parentPathLength = 0; bool hasParent = false; - PCWSTR rootEnd; + PCWSTR rootEnd = nullptr; if (SUCCEEDED(PathCchSkipRoot(path, &rootEnd)) && (*rootEnd != L'\0')) { auto const lastSegment = find_last_path_segment(path); @@ -116,7 +116,7 @@ namespace wil DWORD lastError = ::GetLastError(); if (lastError == ERROR_PATH_NOT_FOUND) { - size_t parentLength; + size_t parentLength{}; if (try_get_parent_path_range(path, &parentLength)) { wistd::unique_ptr parent(new (std::nothrow) wchar_t[parentLength + 1]); @@ -183,7 +183,7 @@ namespace wil template string_type GetFullPathNameW(PCWSTR file, _Outptr_opt_ PCWSTR* filePart = nullptr) { - string_type result; + string_type result{}; THROW_IF_FAILED((GetFullPathNameW(file, result, filePart))); return result; } @@ -216,7 +216,7 @@ namespace wil nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nullptr)); if (result) { - FILE_ATTRIBUTE_TAG_INFO fati; + FILE_ATTRIBUTE_TAG_INFO fati{}; if (GetFileInformationByHandleEx(result.get(), FileAttributeTagInfo, &fati, sizeof(fati)) && details::CanRecurseIntoDirectory(fati)) { @@ -261,7 +261,7 @@ namespace wil wil::unique_hlocal_string searchPath; RETURN_IF_FAILED(::PathAllocCombine(path.get(), L"*", combineOptions, &searchPath)); - WIN32_FIND_DATAW fd; + WIN32_FIND_DATAW fd{}; wil::unique_hfind findHandle(::FindFirstFileW(searchPath.get(), &fd)); RETURN_LAST_ERROR_IF(!findHandle); @@ -668,7 +668,7 @@ namespace wil OVERLAPPED m_overlapped{}; TP_IO *m_tpIo = __nullptr; srwlock m_cancelLock; - unsigned char m_readBuffer[4096]; // Consider alternative buffer sizes. With 512 byte buffer i was not able to observe overflow. + unsigned char m_readBuffer[4096]{}; // Consider alternative buffer sizes. With 512 byte buffer i was not able to observe overflow. }; inline void delete_folder_change_reader_state(_In_opt_ folder_change_reader_state *storage) { delete storage; } @@ -830,7 +830,7 @@ namespace wil string_type GetFinalPathNameByHandleW(HANDLE fileHandle, wil::VolumePrefix volumePrefix = wil::VolumePrefix::Dos, wil::PathOptions options = wil::PathOptions::Normalized) { - string_type result; + string_type result{}; THROW_IF_FAILED((GetFinalPathNameByHandleW(fileHandle, result, volumePrefix, options))); return result; } @@ -861,7 +861,7 @@ namespace wil template string_type GetCurrentDirectoryW() { - string_type result; + string_type result{}; THROW_IF_FAILED((GetCurrentDirectoryW(result))); return result; } @@ -1026,7 +1026,7 @@ namespace wil template ::isFixed, int>::type = 0> typename details::MapInfoClassToInfoStruct::type GetFileInfo(HANDLE fileHandle) { - typename details::MapInfoClassToInfoStruct::type result; + typename details::MapInfoClassToInfoStruct::type result{}; THROW_IF_FAILED(GetFileInfoNoThrow(fileHandle, &result)); return result; } diff --git a/include/wil/registry.h b/include/wil/registry.h index 7c65dfe7a..549f0a73b 100644 --- a/include/wil/registry.h +++ b/include/wil/registry.h @@ -1633,11 +1633,11 @@ namespace wil { #if defined(__WIL_OLEAUTO_H_) // not allowing unique types with try_get_value: wil::unique_bstr cannot be copied and thus is difficult to work with a std::optional - static_assert(!std::is_same_v, "try_get with wil::unique_bstr is disabled"); + static_assert(!wistd::is_same_v, "try_get with wil::unique_bstr is disabled"); #endif // #if defined(__WIL_OLEAUTO_H_) #if defined(__WIL_OBJBASE_H_) // not allowing unique types with try_get_value: wil::unique_cotaskmem_string cannot be copied and thus is difficult to work with a std::optional - static_assert(!std::is_same_v, "try_get with wil::unique_cotaskmem_string is disabled"); + static_assert(!wistd::is_same_v, "try_get with wil::unique_cotaskmem_string is disabled"); #endif // #if defined(__WIL_OBJBASE_H_) const reg_view_details::reg_view regview{ key }; @@ -1659,11 +1659,11 @@ namespace wil { #if defined(__WIL_OLEAUTO_H_) // not allowing unique types with try_get_value: wil::unique_bstr cannot be copied and thus is difficult to work with a std::optional - static_assert(!std::is_same_v, "try_get with wil::unique_bstr is disabled"); + static_assert(!wistd::is_same_v, "try_get with wil::unique_bstr is disabled"); #endif // #if defined(__WIL_OLEAUTO_H_) #if defined(__WIL_OBJBASE_H_) // not allowing unique types with try_get_value: wil::unique_cotaskmem_string cannot be copied and thus is difficult to work with a std::optional - static_assert(!std::is_same_v, "try_get with wil::unique_cotaskmem_string is disabled"); + static_assert(!wistd::is_same_v, "try_get with wil::unique_cotaskmem_string is disabled"); #endif // #if defined(__WIL_OBJBASE_H_) return ::wil::reg::try_get_value(key, nullptr, value_name); @@ -2195,7 +2195,7 @@ namespace wil * \return HRESULT error code indicating success or failure (does not throw C++ exceptions) */ template >* = nullptr> + wistd::enable_if_t>* = nullptr> HRESULT get_value_nothrow(HKEY key, _In_opt_ PCWSTR subkey, _In_opt_ PCWSTR value_name, _Out_ T* return_value) WI_NOEXCEPT { const reg_view_details::reg_view_nothrow regview{ key }; @@ -2212,7 +2212,7 @@ namespace wil * \return HRESULT error code indicating success or failure (does not throw C++ exceptions) */ template >* = nullptr> + wistd::enable_if_t>* = nullptr> HRESULT get_value_nothrow(HKEY key, _In_opt_ PCWSTR value_name, _Out_ T* return_value) WI_NOEXCEPT { return ::wil::reg::get_value_nothrow(key, nullptr, value_name, return_value); @@ -2232,7 +2232,7 @@ namespace wil * \return HRESULT error code indicating success or failure (does not throw C++ exceptions) */ template || std::is_same_v>* = nullptr> + wistd::enable_if_t || wistd::is_same_v>* = nullptr> HRESULT get_value_string_nothrow(HKEY key, _In_opt_ PCWSTR subkey, _In_opt_ PCWSTR value_name, WCHAR(&return_value)[Length], _Out_opt_ DwordType * requiredBytes) WI_NOEXCEPT { const reg_view_details::reg_view_nothrow regview{ key }; @@ -2251,7 +2251,7 @@ namespace wil * \return HRESULT error code indicating success or failure (does not throw C++ exceptions) */ template || std::is_same_v>* = nullptr> + wistd::enable_if_t || wistd::is_same_v>* = nullptr> HRESULT get_value_string_nothrow(HKEY key, _In_opt_ PCWSTR value_name, WCHAR(&return_value)[Length], _Out_opt_ DwordType * requiredBytes) WI_NOEXCEPT { return ::wil::reg::get_value_string_nothrow(key, nullptr, value_name, return_value, requiredBytes); @@ -2338,7 +2338,7 @@ namespace wil * \return HRESULT error code indicating success or failure (does not throw C++ exceptions) */ template || std::is_same_v>* = nullptr> + wistd::enable_if_t || wistd::is_same_v>* = nullptr> HRESULT get_value_dword_nothrow(HKEY key, _In_opt_ PCWSTR subkey, _In_opt_ PCWSTR value_name, _Out_ DwordType * return_value) WI_NOEXCEPT { return ::wil::reg::get_value_nothrow(key, subkey, value_name, return_value); @@ -2353,7 +2353,7 @@ namespace wil * \return HRESULT error code indicating success or failure (does not throw C++ exceptions) */ template || std::is_same_v>* = nullptr> + wistd::enable_if_t || wistd::is_same_v>* = nullptr> HRESULT get_value_dword_nothrow(HKEY key, _In_opt_ PCWSTR value_name, _Out_ DwordType * return_value) WI_NOEXCEPT { return ::wil::reg::get_value_nothrow(key, nullptr, value_name, return_value); @@ -2370,7 +2370,7 @@ namespace wil * \return HRESULT error code indicating success or failure (does not throw C++ exceptions) */ template || std::is_same_v>* = nullptr> + wistd::enable_if_t || wistd::is_same_v>* = nullptr> HRESULT get_value_qword_nothrow(HKEY key, _In_opt_ PCWSTR subkey, _In_opt_ PCWSTR value_name, _Out_ QwordType * return_value) WI_NOEXCEPT { return ::wil::reg::get_value_nothrow(key, subkey, value_name, return_value); @@ -2385,7 +2385,7 @@ namespace wil * \return HRESULT error code indicating success or failure (does not throw C++ exceptions) */ template || std::is_same_v>* = nullptr> + wistd::enable_if_t || wistd::is_same_v>* = nullptr> HRESULT get_value_qword_nothrow(HKEY key, _In_opt_ PCWSTR value_name, _Out_ QwordType * return_value) WI_NOEXCEPT { return ::wil::reg::get_value_nothrow(key, nullptr, value_name, return_value); @@ -2683,7 +2683,7 @@ namespace wil * \return HRESULT error code indicating success or failure (does not throw C++ exceptions) */ template || std::is_same_v>* = nullptr> + wistd::enable_if_t || wistd::is_same_v>* = nullptr> HRESULT get_value_expanded_string_nothrow(HKEY key, _In_opt_ PCWSTR subkey, _In_opt_ PCWSTR value_name, WCHAR(&return_value)[Length], _Out_opt_ DwordType * requiredBytes) WI_NOEXCEPT { const reg_view_details::reg_view_nothrow regview{ key }; @@ -2703,7 +2703,7 @@ namespace wil * \return HRESULT error code indicating success or failure (does not throw C++ exceptions) */ template || std::is_same_v>* = nullptr> + wistd::enable_if_t || wistd::is_same_v>* = nullptr> HRESULT get_value_expanded_string_nothrow(HKEY key, _In_opt_ PCWSTR value_name, WCHAR(&return_value)[Length], _Out_opt_ DwordType * requiredBytes) WI_NOEXCEPT { return ::wil::reg::get_value_expanded_string_nothrow(key, nullptr, value_name, return_value, requiredBytes); diff --git a/include/wil/registry_helpers.h b/include/wil/registry_helpers.h index bf98609cb..724defa45 100644 --- a/include/wil/registry_helpers.h +++ b/include/wil/registry_helpers.h @@ -16,6 +16,7 @@ #include #endif +#include #include #include "resource.h" @@ -34,8 +35,8 @@ namespace wil */ constexpr bool is_registry_not_found(HRESULT hr) WI_NOEXCEPT { - return (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || - (hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)); + return (hr == __HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || + (hr == __HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)); } /** @@ -45,7 +46,7 @@ namespace wil */ constexpr bool is_registry_buffer_too_small(HRESULT hr) WI_NOEXCEPT { - return hr == HRESULT_FROM_WIN32(ERROR_MORE_DATA); + return hr == __HRESULT_FROM_WIN32(ERROR_MORE_DATA); } // Access rights for opening registry keys. See https://learn.microsoft.com/en-us/windows/win32/sysinfo/registry-key-security-and-access-rights. @@ -182,7 +183,7 @@ namespace wil if (last - first < 3) { // it doesn't have the required 2 terminating null characters - return an empty string - return { ::std::wstring{} }; + return ::std::vector<::std::wstring>(1); } ::std::vector<::std::wstring> strings; @@ -666,7 +667,7 @@ namespace wil constexpr bool transferringOwnership = false; RETURN_IF_FAILED(resize_buffer(temp_bstr, byteSize, transferringOwnership)); - // if succeeded in creating a new BSTR, move ownership of the new BSTR into string + // if succeeded in creating a new BSTR, move ownership of the new BSTR into string string.reset(temp_bstr); return S_OK; } @@ -715,7 +716,7 @@ namespace wil constexpr bool transferringOwnership = false; RETURN_IF_FAILED(resize_buffer(temp_bstr, byteSize, transferringOwnership)); - // if succeeded in creating a new BSTR, move ownership of the new BSTR into string + // if succeeded in creating a new BSTR, move ownership of the new BSTR into string string.reset(temp_bstr); return S_OK; } @@ -748,7 +749,7 @@ namespace wil auto new_string = ::wil::make_unique_string_nothrow<::wil::unique_cotaskmem_string>(string.get(), length); RETURN_IF_NULL_ALLOC(new_string.get()); - string = ::std::move(new_string); + string = ::wistd::move(new_string); return S_OK; } @@ -778,7 +779,7 @@ namespace wil const auto bytesToCopy = arrayValue.size() < byteSize ? arrayValue.size() : byteSize; CopyMemory(tempValue.get(), arrayValue.get(), bytesToCopy); - arrayValue = ::std::move(tempValue); + arrayValue = ::wistd::move(tempValue); return S_OK; } #endif // #if defined(__WIL_OBJBASE_H_) @@ -810,7 +811,7 @@ namespace wil auto new_string = ::wil::make_unique_string_nothrow<::wil::unique_cotaskmem_string>(string.get(), length); RETURN_IF_NULL_ALLOC(new_string.get()); - string = ::std::move(new_string); + string = ::wistd::move(new_string); return S_OK; } CATCH_RETURN(); @@ -1045,7 +1046,7 @@ namespace wil // typename D supports unsigned 32-bit values; i.e. allows the caller to pass a DWORD* as well as uint32_t* template || std::is_same_v>* = nullptr> + wistd::enable_if_t || wistd::is_same_v>* = nullptr> typename err_policy::result get_value_char_array(_In_opt_ PCWSTR subkey, _In_opt_ PCWSTR value_name, WCHAR(&return_value)[Length], DWORD type, _Out_opt_ DwordType * requiredBytes) const { constexpr DwordType zero_value{ 0ul }; @@ -1070,7 +1071,7 @@ namespace wil const auto hr = get_value_with_type(subkey, value_name, value, type); if (SUCCEEDED(hr)) { - return ::std::optional(::std::move(value)); + return ::std::optional(::wistd::move(value)); } if (::wil::reg::is_registry_not_found(hr)) diff --git a/include/wil/resource.h b/include/wil/resource.h index 538cb7cb3..ddcaf0734 100644 --- a/include/wil/resource.h +++ b/include/wil/resource.h @@ -1710,6 +1710,7 @@ namespace wil return str_raw_ptr(ua.get()); } +#if !defined(__WIL_MIN_KERNEL) && !defined(WIL_KERNEL_MODE) namespace details { // Forward declaration @@ -1760,13 +1761,14 @@ namespace wil static_assert(sizeof...(str) > 0, "attempting to concatenate no strings"); return details::str_build_nothrow(buffer, details::string_maker::get(buffer), str_raw_ptr(str)...); } +#endif // !defined(__WIL_MIN_KERNEL) && !defined(WIL_KERNEL_MODE) #ifdef WIL_ENABLE_EXCEPTIONS // Concatenate any number of strings together and store it in an automatically allocated string. template string_type str_concat(arguments&&... args) { - string_type result; + string_type result{}; THROW_IF_FAILED(str_concat_nothrow(result, wistd::forward(args)...)); return result; } @@ -1776,11 +1778,12 @@ namespace wil template string_type str_concat_failfast(arguments&&... args) { - string_type result; + string_type result{}; FAIL_FAST_IF_FAILED(str_concat_nothrow(result, wistd::forward(args)...)); return result; } +#if !defined(__WIL_MIN_KERNEL) && !defined(WIL_KERNEL_MODE) namespace details { // Wraps StringCchPrintFExW and stores it in an automatically allocated string. Takes a buffer followed by the same format arguments @@ -1819,7 +1822,7 @@ namespace wil template string_type str_printf(_Printf_format_string_ PCWSTR pszFormat, ...) { - string_type result; + string_type result{}; va_list argsVL; va_start(argsVL, pszFormat); auto hr = details::str_vprintf_nothrow(result, pszFormat, argsVL); @@ -1834,7 +1837,7 @@ namespace wil template string_type str_printf_failfast(_Printf_format_string_ PCWSTR pszFormat, ...) { - string_type result; + string_type result{}; va_list argsVL; va_start(argsVL, pszFormat); auto hr = details::str_vprintf_nothrow(result, pszFormat, argsVL); @@ -1842,6 +1845,7 @@ namespace wil FAIL_FAST_IF_FAILED(hr); return result; } +#endif // !defined(__WIL_MIN_KERNEL) && !defined(WIL_KERNEL_MODE) } // namespace wil #endif // __WIL_RESOURCE @@ -2810,7 +2814,7 @@ namespace wil return wait(); } - UINT64 startTime; + UINT64 startTime{}; QueryUnbiasedInterruptTime(&startTime); UINT64 elapsedTimeMilliseconds = 0; @@ -2832,7 +2836,7 @@ namespace wil UINT64 currTime; QueryUnbiasedInterruptTime(&currTime); - elapsedTimeMilliseconds = (currTime - startTime) / (10 * 1000); + elapsedTimeMilliseconds = (currTime - startTime) / static_cast(10 * 1000); } return true; diff --git a/include/wil/result.h b/include/wil/result.h index f50531cb4..fd702b27b 100644 --- a/include/wil/result.h +++ b/include/wil/result.h @@ -385,7 +385,10 @@ namespace wil while (pNode != nullptr) { auto pCurrent = pNode; +#pragma warning(push) +#pragma warning(disable:6001) // https://github.com/microsoft/wil/issues/164 pNode = pNode->pNext; +#pragma warning(pop) pCurrent->~Node(); ::HeapFree(::GetProcessHeap(), 0, pCurrent); } @@ -677,7 +680,7 @@ namespace wil } // NOTE: FailureType::Log as it's only informative (no action) and SupportedExceptions::All as it's not a barrier, only recognition. - wchar_t message[2048]; + wchar_t message[2048]{}; message[0] = L'\0'; const HRESULT hr = details::ReportFailure_CaughtExceptionCommon(__R_DIAGNOSTICS_RA(source, returnAddress), message, ARRAYSIZE(message), SupportedExceptions::All).hr; @@ -874,7 +877,7 @@ namespace wil class ThreadFailureCallbackHolder { public: - ThreadFailureCallbackHolder(_In_ IFailureCallback *pCallbackParam, _In_opt_ CallContextInfo *pCallContext = nullptr, bool watchNow = true) WI_NOEXCEPT : + ThreadFailureCallbackHolder(_In_opt_ IFailureCallback *pCallbackParam, _In_opt_ CallContextInfo *pCallContext = nullptr, bool watchNow = true) WI_NOEXCEPT : m_ppThreadList(nullptr), m_pCallback(pCallbackParam), m_pNext(nullptr), diff --git a/include/wil/result_macros.h b/include/wil/result_macros.h index b16e5cee6..844d325bc 100644 --- a/include/wil/result_macros.h +++ b/include/wil/result_macros.h @@ -802,6 +802,7 @@ WI_ODR_PRAGMA("WIL_FreeMemory", "0") #define THROW_IF_NULL_ALLOC(ptr) __R_FN(Throw_IfNullAlloc)(__R_INFO(#ptr) ptr) #define THROW_HR_IF(hr, condition) __R_FN(Throw_HrIf)(__R_INFO(#condition) wil::verify_hresult(hr), wil::verify_bool(condition)) #define THROW_HR_IF_NULL(hr, ptr) __R_FN(Throw_HrIfNull)(__R_INFO(#ptr) wil::verify_hresult(hr), ptr) +#define THROW_WIN32_IF(win32err, condition) __R_FN(Throw_Win32If)(__R_INFO(#condition) wil::verify_win32(win32err), wil::verify_bool(condition)) #define THROW_LAST_ERROR_IF(condition) __R_FN(Throw_GetLastErrorIf)(__R_INFO(#condition) wil::verify_bool(condition)) #define THROW_LAST_ERROR_IF_NULL(ptr) __R_FN(Throw_GetLastErrorIfNull)(__R_INFO(#ptr) ptr) #define THROW_IF_NTSTATUS_FAILED(status) __R_FN(Throw_IfNtStatusFailed)(__R_INFO(#status) status) @@ -820,6 +821,7 @@ WI_ODR_PRAGMA("WIL_FreeMemory", "0") #define THROW_IF_NULL_ALLOC_MSG(ptr, fmt, ...) __R_FN(Throw_IfNullAllocMsg)(__R_INFO(#ptr) ptr, fmt, ##__VA_ARGS__) #define THROW_HR_IF_MSG(hr, condition, fmt, ...) __R_FN(Throw_HrIfMsg)(__R_INFO(#condition) wil::verify_hresult(hr), wil::verify_bool(condition), fmt, ##__VA_ARGS__) #define THROW_HR_IF_NULL_MSG(hr, ptr, fmt, ...) __R_FN(Throw_HrIfNullMsg)(__R_INFO(#ptr) wil::verify_hresult(hr), ptr, fmt, ##__VA_ARGS__) +#define THROW_WIN32_IF_MSG(win32err, condition, fmt, ...) __R_FN(Throw_Win32IfMsg)(__R_INFO(#condition) wil::verify_win32(win32err), wil::verify_bool(condition), fmt, ##__VA_ARGS__) #define THROW_LAST_ERROR_IF_MSG(condition, fmt, ...) __R_FN(Throw_GetLastErrorIfMsg)(__R_INFO(#condition) wil::verify_bool(condition), fmt, ##__VA_ARGS__) #define THROW_LAST_ERROR_IF_NULL_MSG(ptr, fmt, ...) __R_FN(Throw_GetLastErrorIfNullMsg)(__R_INFO(#ptr) ptr, fmt, ##__VA_ARGS__) #define THROW_IF_NTSTATUS_FAILED_MSG(status, fmt, ...) __R_FN(Throw_IfNtStatusFailedMsg)(__R_INFO(#status) status, fmt, ##__VA_ARGS__) @@ -1128,7 +1130,7 @@ namespace wil break; } - wchar_t szErrorText[256]; + wchar_t szErrorText[256]{}; szErrorText[0] = L'\0'; LONG errorCode = 0; @@ -2118,7 +2120,7 @@ __WI_POP_WARNINGS _Must_inspect_result_ STRSAFEAPI StringCchLengthA(_In_reads_or_z_(cchMax) STRSAFE_PCNZCH psz, _In_ _In_range_(1, STRSAFE_MAX_CCH) size_t cchMax, _Out_opt_ _Deref_out_range_(<, cchMax) _Deref_out_range_(<= , _String_length_(psz)) size_t* pcchLength) { - HRESULT hr; + HRESULT hr = S_OK; if ((psz == nullptr) || (cchMax > STRSAFE_MAX_CCH)) { hr = STRSAFE_E_INVALID_PARAMETER; @@ -2148,7 +2150,7 @@ __WI_POP_WARNINGS static STRSAFEAPI WilStringVPrintfWorkerA(_Out_writes_(cchDest) _Always_(_Post_z_) STRSAFE_LPSTR pszDest, _In_ _In_range_(1, STRSAFE_MAX_CCH) size_t cchDest, _Always_(_Out_opt_ _Deref_out_range_(<=, cchDest - 1)) size_t* pcchNewDestLength, _In_ _Printf_format_string_ STRSAFE_LPCSTR pszFormat, _In_ va_list argList) { HRESULT hr = S_OK; - int iRet; + int iRet{}; // leave the last space for the null terminator size_t cchMax = cchDest - 1; @@ -2249,7 +2251,7 @@ __WI_POP_WARNINGS } memcpy_s(pStart, bufferSize, pszString, stringSize); - assign_to_opt_param(ppszBufferString, reinterpret_cast(pStart)); + assign_to_opt_param(ppszBufferString, reinterpret_cast(pStart));// lgtm[cpp/incorrect-string-type-conversion] False positive - The query is misinterpreting a buffer (char *) with a MBS string, the cast to TString is expected. return pStart + stringSize; } @@ -2761,7 +2763,7 @@ __WI_POP_WARNINGS inline HRESULT ResultFromKnownException(const ResultException& exception, const DiagnosticsInfo& diagnostics, void* returnAddress) { - wchar_t message[2048]; + wchar_t message[2048]{}; message[0] = L'\0'; MaybeGetExceptionString(exception, message, ARRAYSIZE(message)); auto hr = exception.GetErrorCode(); @@ -2771,7 +2773,7 @@ __WI_POP_WARNINGS inline HRESULT ResultFromKnownException(const std::bad_alloc& exception, const DiagnosticsInfo& diagnostics, void* returnAddress) { - wchar_t message[2048]; + wchar_t message[2048]{}; message[0] = L'\0'; MaybeGetExceptionString(exception, message, ARRAYSIZE(message)); constexpr auto hr = E_OUTOFMEMORY; @@ -2781,7 +2783,7 @@ __WI_POP_WARNINGS inline HRESULT ResultFromKnownException(const std::exception& exception, const DiagnosticsInfo& diagnostics, void* returnAddress) { - wchar_t message[2048]; + wchar_t message[2048]{}; message[0] = L'\0'; MaybeGetExceptionString(exception, message, ARRAYSIZE(message)); constexpr auto hr = __HRESULT_FROM_WIN32(ERROR_UNHANDLED_EXCEPTION); @@ -2793,7 +2795,7 @@ __WI_POP_WARNINGS { if (g_pfnResultFromCaughtException_CppWinRt) { - wchar_t message[2048]; + wchar_t message[2048]{}; message[0] = L'\0'; bool ignored; auto hr = g_pfnResultFromCaughtException_CppWinRt(message, ARRAYSIZE(message), &ignored); @@ -4042,7 +4044,7 @@ __WI_SUPPRESS_4127_E template __declspec(noinline) inline HRESULT ReportFailure_CaughtException(__R_FN_PARAMS_FULL, SupportedExceptions supported) { - wchar_t message[2048]; + wchar_t message[2048]{}; message[0] = L'\0'; return ReportFailure_CaughtExceptionCommon(__R_FN_CALL_FULL, message, ARRAYSIZE(message), supported).hr; } @@ -4050,7 +4052,7 @@ __WI_SUPPRESS_4127_E template<> __declspec(noinline) inline RESULT_NORETURN HRESULT ReportFailure_CaughtException(__R_FN_PARAMS_FULL, SupportedExceptions supported) { - wchar_t message[2048]; + wchar_t message[2048]{}; message[0] = L'\0'; RESULT_NORETURN_RESULT(ReportFailure_CaughtExceptionCommon(__R_FN_CALL_FULL, message, ARRAYSIZE(message), supported).hr); } @@ -4058,7 +4060,7 @@ __WI_SUPPRESS_4127_E template<> __declspec(noinline) inline RESULT_NORETURN HRESULT ReportFailure_CaughtException(__R_FN_PARAMS_FULL, SupportedExceptions supported) { - wchar_t message[2048]; + wchar_t message[2048]{}; message[0] = L'\0'; RESULT_NORETURN_RESULT(ReportFailure_CaughtExceptionCommon(__R_FN_CALL_FULL, message, ARRAYSIZE(message), supported).hr); } @@ -5809,6 +5811,16 @@ __WI_SUPPRESS_4127_E } } + _Post_satisfies_(return == condition) _When_(condition, _Analysis_noreturn_) + __R_CONDITIONAL_METHOD(bool, Throw_Win32If)(__R_CONDITIONAL_FN_PARAMS DWORD err, bool condition) + { + if (condition) + { + __R_CALL_INTERNAL_METHOD(_Throw_Win32)(__R_CONDITIONAL_FN_CALL err); + } + return condition; + } + _Post_satisfies_(return == condition) _When_(condition, _Analysis_noreturn_) __R_CONDITIONAL_METHOD(bool, Throw_GetLastErrorIf)(__R_CONDITIONAL_FN_PARAMS bool condition) { @@ -6067,6 +6079,18 @@ __WI_SUPPRESS_4127_E } } + _Post_satisfies_(return == condition) _When_(condition, _Analysis_noreturn_) + __R_CONDITIONAL_NOINLINE_METHOD(bool, Throw_Win32IfMsg)(__R_CONDITIONAL_FN_PARAMS DWORD err, bool condition, _Printf_format_string_ PCSTR formatString, ...) + { + if (condition) + { + va_list argList; + va_start(argList, formatString); + __R_CALL_INTERNAL_NOINLINE_METHOD(_Throw_Win32Msg)(__R_CONDITIONAL_NOINLINE_FN_CALL err, formatString, argList); + } + return condition; + } + _Post_satisfies_(return == condition) _When_(condition, _Analysis_noreturn_) __R_CONDITIONAL_NOINLINE_METHOD(bool, Throw_GetLastErrorIfMsg)(__R_CONDITIONAL_FN_PARAMS bool condition, _Printf_format_string_ PCSTR formatString, ...) { diff --git a/include/wil/win32_helpers.h b/include/wil/win32_helpers.h index d3925d215..af70ce7dd 100644 --- a/include/wil/win32_helpers.h +++ b/include/wil/win32_helpers.h @@ -333,7 +333,7 @@ namespace wil { details::string_maker maker; - wchar_t value[stackBufferLength]; + wchar_t value[stackBufferLength]{}; value[0] = L'\0'; size_t valueLengthNeededWithNull{}; // callback returns the number of characters needed including the null terminator. RETURN_IF_FAILED_EXPECTED(callback(value, ARRAYSIZE(value), &valueLengthNeededWithNull)); @@ -422,7 +422,7 @@ namespace wil RETURN_LAST_ERROR_IF((success == FALSE) && (::GetLastError() != ERROR_INSUFFICIENT_BUFFER)); // On success, return the amount used; on failure, try doubling - *valueLengthNeededWithNul = success ? (lengthToUse + 1) : (lengthToUse * 2); + *valueLengthNeededWithNul = success ? (static_cast(lengthToUse) + 1) : (static_cast(lengthToUse) * 2); return S_OK; }); } @@ -488,17 +488,17 @@ namespace wil { auto adapter = [&](_Out_writes_(valueLength) PWSTR value, size_t valueLength, _Out_ size_t* valueLengthNeededWithNul) -> HRESULT { - DWORD copiedCount; - size_t valueUsedWithNul; - bool copyFailed; - bool copySucceededWithNoTruncation; + DWORD copiedCount{}; + size_t valueUsedWithNul{}; + bool copyFailed{}; + bool copySucceededWithNoTruncation{}; if (process != nullptr) { // GetModuleFileNameExW truncates and provides no error or other indication it has done so. // The only way to be sure it didn't truncate is if it didn't need the whole buffer. The // count copied to the buffer includes the nul-character as well. copiedCount = ::GetModuleFileNameExW(process, module, value, static_cast(valueLength)); - valueUsedWithNul = copiedCount + 1; + valueUsedWithNul = static_cast(copiedCount) + 1; copyFailed = (0 == copiedCount); copySucceededWithNoTruncation = !copyFailed && (copiedCount < valueLength - 1); } @@ -508,7 +508,7 @@ namespace wil // and set the last error to ERROR_INSUFFICIENT_BUFFER. The count returned does not include // the nul-character copiedCount = ::GetModuleFileNameW(module, value, static_cast(valueLength)); - valueUsedWithNul = copiedCount + 1; + valueUsedWithNul = static_cast(copiedCount) + 1; copyFailed = (0 == copiedCount); copySucceededWithNoTruncation = !copyFailed && (copiedCount < valueLength); } @@ -573,7 +573,7 @@ namespace wil template string_type ExpandEnvironmentStringsW(_In_ PCWSTR input) { - string_type result; + string_type result{}; THROW_IF_FAILED((wil::ExpandEnvironmentStringsW(input, result))); return result; } @@ -583,7 +583,7 @@ namespace wil template string_type TrySearchPathW(_In_opt_ PCWSTR path, _In_ PCWSTR fileName, PCWSTR _In_opt_ extension) { - string_type result; + string_type result{}; HRESULT searchHR = wil::SearchPathW(path, fileName, extension, result); THROW_HR_IF(searchHR, FAILED(searchHR) && (searchHR != HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))); return result; @@ -594,7 +594,7 @@ namespace wil template string_type GetEnvironmentVariableW(_In_ PCWSTR key) { - string_type result; + string_type result{}; THROW_IF_FAILED((wil::GetEnvironmentVariableW(key, result))); return result; } @@ -603,7 +603,7 @@ namespace wil template string_type TryGetEnvironmentVariableW(_In_ PCWSTR key) { - string_type result; + string_type result{}; THROW_IF_FAILED((wil::TryGetEnvironmentVariableW(key, result))); return result; } @@ -611,7 +611,7 @@ namespace wil template string_type GetModuleFileNameW(HMODULE module = nullptr /* current process module */) { - string_type result; + string_type result{}; THROW_IF_FAILED((wil::GetModuleFileNameW(module, result))); return result; } @@ -619,7 +619,7 @@ namespace wil template string_type GetModuleFileNameExW(HANDLE process, HMODULE module) { - string_type result; + string_type result{}; THROW_IF_FAILED((wil::GetModuleFileNameExW(process, module, result))); return result; } @@ -645,7 +645,7 @@ namespace wil template string_type QueryFullProcessImageNameW(HANDLE processHandle = GetCurrentProcess(), DWORD flags = 0) { - string_type result; + string_type result{}; THROW_IF_FAILED((wil::QueryFullProcessImageNameW(processHandle, flags, result))); return result; } diff --git a/include/wil/winrt.h b/include/wil/winrt.h index 8e5120051..117acd81d 100644 --- a/include/wil/winrt.h +++ b/include/wil/winrt.h @@ -1779,7 +1779,7 @@ namespace details void OnCancel() override { } private: // needs to be MapToSmartType::type to hold non trial types - TResult m_result; + TResult m_result{}; }; extern const __declspec(selectany) wchar_t SyncAsyncActionName[] = L"SyncActionAction";