Skip to content

Commit

Permalink
[Analyzers][CPP]Changes to fix warning 26493 on src/common (#23467)
Browse files Browse the repository at this point in the history
* Changes to fix warning 26493 on src/common

It will be multi PR change. This does not turn on the rule but fix the code under src/commom

* int cast
  • Loading branch information
sosssego committed Feb 8, 2023
1 parent 956eb98 commit a743e49
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 69 deletions.
54 changes: 27 additions & 27 deletions src/common/GPOWrapper/GPOWrapper.cpp
Expand Up @@ -6,110 +6,110 @@ namespace winrt::PowerToys::GPOWrapper::implementation
{
GpoRuleConfigured GPOWrapper::GetConfiguredAlwaysOnTopEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredAlwaysOnTopEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredAlwaysOnTopEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredAwakeEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredAwakeEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredAwakeEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredColorPickerEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredColorPickerEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredColorPickerEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredFancyZonesEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredFancyZonesEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredFancyZonesEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredFileLocksmithEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredFileLocksmithEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredFileLocksmithEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredSvgPreviewEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredSvgPreviewEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredSvgPreviewEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredMarkdownPreviewEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredMarkdownPreviewEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredMarkdownPreviewEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredMonacoPreviewEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredMonacoPreviewEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredMonacoPreviewEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredPdfPreviewEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredPdfPreviewEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredPdfPreviewEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredGcodePreviewEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredGcodePreviewEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredGcodePreviewEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredSvgThumbnailsEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredSvgThumbnailsEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredSvgThumbnailsEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredPdfThumbnailsEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredPdfThumbnailsEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredPdfThumbnailsEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredGcodeThumbnailsEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredGcodeThumbnailsEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredGcodeThumbnailsEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredStlThumbnailsEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredStlThumbnailsEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredStlThumbnailsEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredHostsFileEditorEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredHostsFileEditorEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredHostsFileEditorEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredImageResizerEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredImageResizerEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredImageResizerEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredKeyboardManagerEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredKeyboardManagerEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredKeyboardManagerEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredFindMyMouseEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredFindMyMouseEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredFindMyMouseEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredMouseHighlighterEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredMouseHighlighterEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredMouseHighlighterEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredMousePointerCrosshairsEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredMousePointerCrosshairsEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredMousePointerCrosshairsEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredPowerRenameEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredPowerRenameEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredPowerRenameEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredPowerLauncherEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredPowerLauncherEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredPowerLauncherEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredQuickAccentEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredQuickAccentEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredQuickAccentEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredScreenRulerEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredScreenRulerEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredScreenRulerEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredShortcutGuideEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredShortcutGuideEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredShortcutGuideEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredTextExtractorEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredTextExtractorEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredTextExtractorEnabledValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredVideoConferenceMuteEnabledValue()
{
return (GpoRuleConfigured)powertoys_gpo::getConfiguredVideoConferenceMuteEnabledValue();
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredVideoConferenceMuteEnabledValue());
}
}
2 changes: 1 addition & 1 deletion src/common/SettingsAPI/settings_objects.cpp
Expand Up @@ -247,7 +247,7 @@ namespace PowerToysSettings
bool Settings::serialize_to_buffer(wchar_t* buffer, int* buffer_size)
{
auto result = m_json.Stringify();
const int result_len = (int)result.size() + 1;
const int result_len = static_cast<int>(result.size() + 1);

if (buffer == nullptr || *buffer_size < result_len)
{
Expand Down
2 changes: 1 addition & 1 deletion src/common/SettingsAPI/settings_objects.h
Expand Up @@ -220,7 +220,7 @@ namespace PowerToysSettings
std::array<BYTE, 256> key_states{}; // Zero-initialize
std::array<wchar_t, 256> output;
const UINT wFlags = 1 << 2; // If bit 2 is set, keyboard state is not changed (Windows 10, version 1607 and newer)
auto output_bytes = ToUnicodeEx(key_code, scan_code, key_states.data(), output.data(), (int)output.size() - 1, wFlags, layout);
auto output_bytes = ToUnicodeEx(key_code, scan_code, key_states.data(), output.data(), static_cast<int>(output.size()) - 1, wFlags, layout);
if (output_bytes <= 0)
{
// If ToUnicodeEx fails (e.g. for F1-F12 keys) use GetKeyNameTextW
Expand Down
6 changes: 3 additions & 3 deletions src/common/Themes/icon_helpers.cpp
Expand Up @@ -14,7 +14,7 @@ HRESULT GetIconIndexFromPath(_In_ PCWSTR path, _Out_ int* index)
if (!PathIsRelative(path))
{
DWORD attrib = GetFileAttributes(path);
HIMAGELIST himl = (HIMAGELIST)SHGetFileInfo(path, attrib, &shFileInfo, sizeof(shFileInfo), (SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES));
auto himl =SHGetFileInfo(path, attrib, &shFileInfo, sizeof(shFileInfo), (SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES));
if (himl)
{
*index = shFileInfo.iIcon;
Expand Down Expand Up @@ -61,14 +61,14 @@ HBITMAP CreateBitmapFromIcon(_In_ HICON hIcon, _In_opt_ UINT width, _In_opt_ UIN
if (hBitmap != NULL)
{
// Select bitmap into DC
HBITMAP hBitmapOld = (HBITMAP)SelectObject(hDC, hBitmap);
HBITMAP hBitmapOld = static_cast<HBITMAP>(SelectObject(hDC, hBitmap));
if (hBitmapOld != NULL)
{
// Draw icon into DC
if (DrawIconEx(hDC, 0, 0, hIcon, rc.right, rc.bottom, 0, NULL, DI_NORMAL))
{
// Restore original bitmap in DC
hBitmapResult = (HBITMAP)SelectObject(hDC, hBitmapOld);
hBitmapResult = static_cast<HBITMAP>(SelectObject(hDC, hBitmapOld));
hBitmapOld = NULL;
hBitmap = NULL;
}
Expand Down
5 changes: 1 addition & 4 deletions src/common/Themes/theme_helpers.cpp
Expand Up @@ -28,10 +28,7 @@ AppTheme ThemeHelpers::GetAppTheme()
}

// convert bytes written to our buffer to an int, assuming little-endian
auto i = int(buffer[3] << 24 |
buffer[2] << 16 |
buffer[1] << 8 |
buffer[0]);
auto i = static_cast<int>(buffer[3] << 24 | buffer[2] << 16 | buffer[1] << 8 | buffer[0]);

return AppTheme(i);
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/Themes/theme_listener.cpp
Expand Up @@ -8,7 +8,7 @@
#pragma warning(disable : 4702)
DWORD WINAPI _checkTheme(LPVOID lpParam)
{
auto listener = (ThemeListener*)lpParam;
auto listener = static_cast<ThemeListener*>(lpParam);
listener->CheckTheme();
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/Themes/windows_colors.cpp
Expand Up @@ -11,7 +11,7 @@ DWORD WindowsColors::rgb_color(DWORD abgr_color)
}
DWORD WindowsColors::rgb_color(winrt::Windows::UI::Color color)
{
return ((DWORD)color.R << 16) | ((DWORD)color.G << 8) | ((DWORD)color.B);
return static_cast<DWORD>((color.R << 16) | (color.G << 8) | (color.B));
}
WindowsColors::Color WindowsColors::get_button_face_color()
{
Expand Down
2 changes: 1 addition & 1 deletion src/common/interop/keyboard_layout.cpp
Expand Up @@ -56,7 +56,7 @@ bool mapKeycodeToUnicode(const int vCode, HKL layout, const BYTE* keyState, std:
const UINT scanCode = MapVirtualKeyExW(vCode, MAPVK_VK_TO_VSC, layout);
// Get the unicode representation from the virtual key code and scan code pair
const UINT wFlags = 1 << 2; // If bit 2 is set, keyboard state is not changed (Windows 10, version 1607 and newer)
const int result = ToUnicodeEx(vCode, scanCode, keyState, outBuffer.data(), (int)outBuffer.size(), wFlags, layout);
const int result = ToUnicodeEx(vCode, scanCode, keyState, outBuffer.data(), static_cast<int>(outBuffer.size()), wFlags, layout);
return result != 0;
}

Expand Down
20 changes: 10 additions & 10 deletions src/common/interop/two_way_pipe_message_ipc.cpp
Expand Up @@ -184,9 +184,9 @@ BOOL TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::GetLogonSID(HANDLE hToken,
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
goto Cleanup;

ptg = (PTOKEN_GROUPS)HeapAlloc(GetProcessHeap(),
ptg = static_cast<PTOKEN_GROUPS>(HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
dwLength);
dwLength));

if (ptg == NULL)
goto Cleanup;
Expand All @@ -213,14 +213,14 @@ BOOL TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::GetLogonSID(HANDLE hToken,
// Found the logon SID; make a copy of it.

dwLength = GetLengthSid(ptg->Groups[dwIndex].Sid);
*ppsid = (PSID)HeapAlloc(GetProcessHeap(),
*ppsid = static_cast<PSID>(HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
dwLength);
dwLength));
if (*ppsid == NULL)
goto Cleanup;
if (!CopySid(dwLength, *ppsid, ptg->Groups[dwIndex].Sid))
{
HeapFree(GetProcessHeap(), 0, (LPVOID)*ppsid);
HeapFree(GetProcessHeap(), 0, static_cast<LPVOID>(*ppsid));
goto Cleanup;
}
break;
Expand All @@ -233,15 +233,15 @@ BOOL TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::GetLogonSID(HANDLE hToken,
// Free the buffer for the token groups.

if (ptg != NULL)
HeapFree(GetProcessHeap(), 0, (LPVOID)ptg);
HeapFree(GetProcessHeap(), 0, static_cast<LPVOID>(ptg));

return bSuccess;
}

VOID TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::FreeLogonSID(PSID* ppsid)
{
// From https://learn.microsoft.com/previous-versions/aa446670(v=vs.85)
HeapFree(GetProcessHeap(), 0, (LPVOID)*ppsid);
HeapFree(GetProcessHeap(), 0, static_cast<LPVOID>(*ppsid));
}

int TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::change_pipe_security_allow_restricted_token(HANDLE handle, HANDLE token)
Expand Down Expand Up @@ -279,7 +279,7 @@ int TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::change_pipe_security_allow_r
ea.grfInheritance = NO_INHERITANCE;
ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea.Trustee.TrusteeType = TRUSTEE_IS_USER;
ea.Trustee.ptstrName = (LPTSTR)user_restricted;
ea.Trustee.ptstrName = static_cast<LPTSTR>(user_restricted);

if (SetEntriesInAcl(1, &ea, old_dacl, &new_dacl))
{
Expand All @@ -302,9 +302,9 @@ int TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::change_pipe_security_allow_r
error = 0;

Lclean_dacl:
LocalFree((HLOCAL)new_dacl);
LocalFree(static_cast<HLOCAL>(new_dacl));
Lclean_sd:
LocalFree((HLOCAL)sd);
LocalFree(static_cast<HLOCAL>(sd));
Lclean_sid:
FreeLogonSID(&user_restricted);
Ldone:
Expand Down
8 changes: 4 additions & 4 deletions src/common/utils/HDropIterator.h
Expand Up @@ -19,7 +19,7 @@ class HDropIterator

if (SUCCEEDED(pDataObject->GetData(&formatetc, &m_medium)))
{
_listCount = DragQueryFile((HDROP)m_medium.hGlobal, 0xFFFFFFFF, NULL, 0);
_listCount = DragQueryFile(static_cast<HDROP>(m_medium.hGlobal), 0xFFFFFFFF, NULL, 0);
}
else
{
Expand Down Expand Up @@ -52,10 +52,10 @@ class HDropIterator

LPTSTR CurrentItem() const
{
UINT cch = DragQueryFile((HDROP)m_medium.hGlobal, _current, NULL, 0) + 1;
LPTSTR pszPath = (LPTSTR)malloc(sizeof(TCHAR) * cch);
UINT cch = DragQueryFile(static_cast<HDROP>(m_medium.hGlobal), _current, NULL, 0) + 1;
LPTSTR pszPath = static_cast<LPTSTR>(malloc(sizeof(TCHAR) * cch));

DragQueryFile((HDROP)m_medium.hGlobal, _current, pszPath, cch);
DragQueryFile(static_cast<HDROP>(m_medium.hGlobal), _current, pszPath, cch);

return pszPath;
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/utils/HttpClient.h
Expand Up @@ -57,7 +57,7 @@ namespace http
totalBytesRead += buffer.Length();
if (progressUpdateCallback)
{
float percentage = (float)totalBytesRead / totalBytes;
float percentage = static_cast<float>(totalBytesRead) / totalBytes;
progressUpdateCallback(percentage);
}

Expand Down
4 changes: 2 additions & 2 deletions src/common/utils/UnhandledExceptionHandler.h
Expand Up @@ -93,7 +93,7 @@ inline std::wstring GetModuleName(HANDLE process, const STACKFRAME64& stack)
return std::wstring();
}

if (!GetModuleFileNameW((HINSTANCE)moduleBase, modulePath, MAX_PATH))
if (!GetModuleFileNameW(reinterpret_cast<HINSTANCE>(moduleBase), modulePath, MAX_PATH))
{
Logger::error(L"Failed to get a module path. {}", get_last_error_or_default(GetLastError()));
return std::wstring();
Expand All @@ -105,7 +105,7 @@ inline std::wstring GetModuleName(HANDLE process, const STACKFRAME64& stack)

inline std::wstring GetName(HANDLE process, const STACKFRAME64& stack)
{
static IMAGEHLP_SYMBOL64* pSymbol = (IMAGEHLP_SYMBOL64*)malloc(sizeof(IMAGEHLP_SYMBOL64) + MAX_PATH * sizeof(TCHAR));
static IMAGEHLP_SYMBOL64* pSymbol = static_cast<IMAGEHLP_SYMBOL64*>(malloc(sizeof(IMAGEHLP_SYMBOL64) + MAX_PATH * sizeof(TCHAR)));
if (!pSymbol)
{
return std::wstring();
Expand Down
4 changes: 2 additions & 2 deletions src/common/utils/elevation.h
Expand Up @@ -198,7 +198,7 @@ inline bool drop_elevated_privileges()
TOKEN_MANDATORY_LABEL label = { 0 };
label.Label.Attributes = SE_GROUP_INTEGRITY;
label.Label.Sid = medium_sid;
DWORD size = (DWORD)sizeof(TOKEN_MANDATORY_LABEL) + ::GetLengthSid(medium_sid);
DWORD size = static_cast<DWORD>(sizeof(TOKEN_MANDATORY_LABEL) + ::GetLengthSid(medium_sid));

BOOL result = SetTokenInformation(token, TokenIntegrityLevel, &label, size);
LocalFree(medium_sid);
Expand Down Expand Up @@ -464,7 +464,7 @@ inline bool check_user_is_admin()
}

// Allocate the buffer.
pGroupInfo = (PTOKEN_GROUPS)GlobalAlloc(GPTR, dwSize);
pGroupInfo = static_cast<PTOKEN_GROUPS>(GlobalAlloc(GPTR, dwSize));

// Call GetTokenInformation again to get the group information.
if (!GetTokenInformation(hToken, TokenGroups, pGroupInfo, dwSize, &dwSize))
Expand Down
6 changes: 3 additions & 3 deletions src/common/utils/gpo.h
Expand Up @@ -50,7 +50,7 @@ namespace powertoys_gpo {
inline gpo_rule_configured_t getConfiguredValue(const std::wstring& registry_value_name)
{
HKEY key{};
DWORD value = (DWORD) -2;
DWORD value = 0xFFFFFFFE;
DWORD valueSize = sizeof(value);

bool machine_key_found = true;
Expand All @@ -62,7 +62,7 @@ namespace powertoys_gpo {
if(machine_key_found)
{
// If the path was found in the machine, we need to check if the value for the policy exists.
auto res = RegQueryValueExW(key, registry_value_name.c_str(), nullptr, nullptr, (LPBYTE)&value, &valueSize);
auto res = RegQueryValueExW(key, registry_value_name.c_str(), nullptr, nullptr, reinterpret_cast<LPBYTE>(&value), &valueSize);

RegCloseKey(key);

Expand All @@ -82,7 +82,7 @@ namespace powertoys_gpo {
}
return gpo_rule_configured_unavailable;
}
auto res = RegQueryValueExW(key, registry_value_name.c_str(), nullptr, nullptr, (LPBYTE)&value, &valueSize);
auto res = RegQueryValueExW(key, registry_value_name.c_str(), nullptr, nullptr, reinterpret_cast<LPBYTE>(&value), &valueSize);
RegCloseKey(key);

if (res != ERROR_SUCCESS) {
Expand Down

0 comments on commit a743e49

Please sign in to comment.