[CodeQuality]Fix C++ static analyzer findings (#29745)

* [PVS] Fix static analyzer findings

* f: fix error handling

* f: more improvements

* Update src/modules/FileLocksmith/FileLocksmithLibInterop/NtdllExtensions.cpp
This commit is contained in:
Andrey Nekrasov 2023-11-15 17:38:44 +01:00 коммит произвёл GitHub
Родитель 4ebc4cbc0d
Коммит 5ab83c6ad2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 19 добавлений и 27 удалений

Просмотреть файл

@ -3,9 +3,10 @@
#ifndef PCH_H
#define PCH_H
#pragma warning(push)
#pragma warning(disable : 5205)
#include <winrt/base.h>
#pragma warning(default : 5205)
#pragma warning(pop)
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>

Просмотреть файл

@ -188,7 +188,7 @@ int WINAPI wWinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ PWSTR lpCmdLine, _I
m_reparent_event_handle = CreateEventW(nullptr, false, false, CommonSharedConstants::CROP_AND_LOCK_REPARENT_EVENT);
m_thumbnail_event_handle = CreateEventW(nullptr, false, false, CommonSharedConstants::CROP_AND_LOCK_THUMBNAIL_EVENT);
m_exit_event_handle = CreateEventW(nullptr, false, false, CommonSharedConstants::CROP_AND_LOCK_EXIT_EVENT);
if (!m_reparent_event_handle || !m_reparent_event_handle || !m_exit_event_handle)
if (!m_reparent_event_handle || !m_thumbnail_event_handle || !m_exit_event_handle)
{
Logger::warn(L"Failed to create events. {}", get_last_error_or_default(GetLastError()));
return 1;

Просмотреть файл

@ -309,9 +309,8 @@ std::wstring NtdllExtensions::pid_to_user(DWORD pid)
}
DWORD token_size = 0;
GetTokenInformation(token, TokenUser, nullptr, 0, &token_size);
if (token_size < 0)
const bool ok = GetTokenInformation(token, TokenUser, nullptr, 0, &token_size);
if ((!ok && GetLastError() != ERROR_INSUFFICIENT_BUFFER) || !token_size)
{
return user;
}

Просмотреть файл

@ -32,8 +32,6 @@ namespace
winrt::com_ptr<ID2D1Bitmap> ConvertID3D11Texture2DToD2D1Bitmap(winrt::com_ptr<ID2D1RenderTarget> rt,
const MappedTextureView* capturedScreenTexture)
{
capturedScreenTexture->view.pixels;
D2D1_BITMAP_PROPERTIES props = { .pixelFormat = rt->GetPixelFormat() };
rt->GetDpi(&props.dpiX, &props.dpiY);
const auto sizeF = rt->GetSize();

Просмотреть файл

@ -238,7 +238,7 @@ void FancyZonesSettings::LoadSettings()
if (auto val = values.get_int_value(NonLocalizable::OverlappingZonesAlgorithmID))
{
// Avoid undefined behavior
if (*val >= 0 || *val < static_cast<int>(OverlappingZonesAlgorithm::EnumElements))
if (*val >= 0 && *val < static_cast<int>(OverlappingZonesAlgorithm::EnumElements))
{
auto algorithm = (OverlappingZonesAlgorithm)*val;
if (m_settings.overlappingZonesAlgorithm != algorithm)

Просмотреть файл

@ -306,26 +306,23 @@ void FancyZonesWindowUtils::SizeWindowToRect(HWND window, RECT rect) noexcept
::GetWindowPlacement(window, &placement);
}
if (!IsWindowVisible(window))
{
placement.showCmd = SW_HIDE;
}
else
if (IsWindowVisible(window))
{
// Do not restore minimized windows. We change their placement though so they restore to the correct zone.
if ((placement.showCmd != SW_SHOWMINIMIZED) &&
(placement.showCmd != SW_MINIMIZE))
{
placement.showCmd = SW_RESTORE;
}
// Remove maximized show command to make sure window is moved to the correct zone.
if (placement.showCmd == SW_SHOWMAXIMIZED)
placement.flags &= ~WPF_RESTORETOMAXIMIZED;
// Remove maximized show command to make sure window is moved to the correct zone.
if (placement.showCmd == SW_SHOWMAXIMIZED)
{
placement.showCmd = SW_RESTORE;
placement.flags &= ~WPF_RESTORETOMAXIMIZED;
}
}
else
{
placement.showCmd = SW_HIDE;
}
ScreenToWorkAreaCoords(window, rect);

Просмотреть файл

@ -12,8 +12,7 @@ namespace KeyboardEventHandlers
// Num Lock's key state is applied before it is intercepted by low level keyboard hooks, so we have to manually set back the state when we suppress the key. This is done by sending an additional key up, key down set of messages.
// We need 2 key events because after Num Lock is suppressed, key up to release num lock key and key down to revert the num lock state
int key_count = 2;
LPINPUT keyEventList = new INPUT[size_t(key_count)]();
memset(keyEventList, 0, sizeof(keyEventList));
LPINPUT keyEventList = new INPUT[size_t(key_count)]{};
// Use the suppress flag to ensure these are not intercepted by any remapped keys or shortcuts
Helpers::SetKeyEvent(keyEventList, 0, INPUT_KEYBOARD, VK_NUMLOCK, KEYEVENTF_KEYUP, KeyboardManagerConstants::KEYBOARDMANAGER_SUPPRESS_FLAG);

Просмотреть файл

@ -109,7 +109,7 @@ LRESULT Toolbar::WindowProcessMessages(HWND hwnd, UINT msg, WPARAM wparam, LPARA
}
case WM_DPICHANGED:
{
UINT dpi = LOWORD(dpi);
UINT dpi = LOWORD(wparam);
RECT* prcNewWindow = reinterpret_cast<RECT*>(lparam);
POINT suggestedPosition;

Просмотреть файл

@ -99,8 +99,6 @@ std::string toMediaTypeString(GUID subtype)
return "MFVideoFormat_D16";
else if (subtype == MFVideoFormat_AYUV)
return "MFVideoFormat_AYUV";
else if (subtype == MFVideoFormat_YUY2)
return "MFVideoFormat_YUY2";
else if (subtype == MFVideoFormat_YVYU)
return "MFVideoFormat_YVYU";
else if (subtype == MFVideoFormat_YVU9)

Просмотреть файл

@ -616,10 +616,10 @@ void close_settings_window()
{
if (g_settings_process_id != 0)
{
HANDLE proc = OpenProcess(PROCESS_TERMINATE, false, g_settings_process_id);
if (proc != INVALID_HANDLE_VALUE)
wil::unique_handle proc{ OpenProcess(PROCESS_TERMINATE, false, g_settings_process_id) };
if (proc)
{
TerminateProcess(proc, 0);
TerminateProcess(proc.get(), 0);
}
}
}