From 19c4255c7d5c3707b41779d03d52b1e1a13cf348 Mon Sep 17 00:00:00 2001 From: sosssego Date: Tue, 25 Oct 2022 17:33:23 +0100 Subject: [PATCH] [Analyzers][CPP]Turn on C26451 and fix code (#21230) * Turn on rule as error and fixing code * Add packages to external include paths * Disable warrnings on external code. --- Cpp.Build.props | 2 +- CppRuleSet.ruleset | 2 +- src/common/logger/call_tracer.cpp | 2 +- .../fancyzones/FancyZonesLib/LayoutConfigurator.cpp | 12 ++++++------ src/modules/fancyzones/FancyZonesLib/ZoneSet.cpp | 4 ++-- .../KeyboardEventHandlers.cpp | 7 +++++-- src/modules/powerrename/lib/PowerRenameLib.vcxproj | 2 +- .../VideoConferenceShared/SerializedSharedMemory.cpp | 2 +- src/runner/powertoy_module.cpp | 2 +- .../BugReportTool/BugReportTool.vcxproj | 3 ++- 10 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Cpp.Build.props b/Cpp.Build.props index d12d62495a..b651f4a6df 100644 --- a/Cpp.Build.props +++ b/Cpp.Build.props @@ -34,7 +34,7 @@ x64 false - $(MSBuildThisFileFullPath)\..\deps\;$(ExternalIncludePath) + $(MSBuildThisFileFullPath)\..\deps\;$(MSBuildThisFileFullPath)\..\packages\;$(ExternalIncludePath) true $(MsbuildThisFileDirectory)\CppRuleSet.ruleset diff --git a/CppRuleSet.ruleset b/CppRuleSet.ruleset index 9468ec2c36..69f1babbcc 100644 --- a/CppRuleSet.ruleset +++ b/CppRuleSet.ruleset @@ -16,7 +16,7 @@ - + diff --git a/src/common/logger/call_tracer.cpp b/src/common/logger/call_tracer.cpp index ae4b9dfa03..1527e61b1f 100644 --- a/src/common/logger/call_tracer.cpp +++ b/src/common/logger/call_tracer.cpp @@ -24,7 +24,7 @@ namespace } else { - return std::string(2 * min(level, 64) - 1, ' ') + " - "; + return std::string(static_cast(2) * min(level, 64) - 1, ' ') + " - "; } } diff --git a/src/modules/fancyzones/FancyZonesLib/LayoutConfigurator.cpp b/src/modules/fancyzones/FancyZonesLib/LayoutConfigurator.cpp index ce5472e9d0..de834dc2bd 100644 --- a/src/modules/fancyzones/FancyZonesLib/LayoutConfigurator.cpp +++ b/src/modules/fancyzones/FancyZonesLib/LayoutConfigurator.cpp @@ -139,9 +139,9 @@ ZonesMap CalculateGridZones(FancyZonesUtils::Rect workArea, FancyZonesDataTypes: columnInfo[col].Extent = columnInfo[col].End - columnInfo[col].Start; } - for (int row = 0; row < gridLayoutInfo.rows(); row++) + for (int64_t row = 0; row < gridLayoutInfo.rows(); row++) { - for (int col = 0; col < gridLayoutInfo.columns(); col++) + for (int64_t col = 0; col < gridLayoutInfo.columns(); col++) { int i = gridLayoutInfo.cellChildMap()[row][col]; if (((row == 0) || (gridLayoutInfo.cellChildMap()[row - 1][col] != i)) && @@ -150,12 +150,12 @@ ZonesMap CalculateGridZones(FancyZonesUtils::Rect workArea, FancyZonesDataTypes: long left = columnInfo[col].Start; long top = rowInfo[row].Start; - int maxRow = row; + int64_t maxRow = row; while (((maxRow + 1) < gridLayoutInfo.rows()) && (gridLayoutInfo.cellChildMap()[maxRow + 1][col] == i)) { maxRow++; } - int maxCol = col; + int64_t maxCol = col; while (((maxCol + 1) < gridLayoutInfo.columns()) && (gridLayoutInfo.cellChildMap()[row][maxCol + 1] == i)) { maxCol++; @@ -165,9 +165,9 @@ ZonesMap CalculateGridZones(FancyZonesUtils::Rect workArea, FancyZonesDataTypes: long bottom = rowInfo[maxRow].End; top += row == 0 ? spacing : spacing / 2; - bottom -= maxRow == gridLayoutInfo.rows() - 1 ? spacing : spacing / 2; + bottom -= maxRow == static_cast(gridLayoutInfo.rows()) - 1 ? spacing : spacing / 2; left += col == 0 ? spacing : spacing / 2; - right -= maxCol == gridLayoutInfo.columns() - 1 ? spacing : spacing / 2; + right -= maxCol == static_cast(gridLayoutInfo.columns()) - 1 ? spacing : spacing / 2; auto zone = MakeZone(RECT{ left, top, right, bottom }, i); if (zone) diff --git a/src/modules/fancyzones/FancyZonesLib/ZoneSet.cpp b/src/modules/fancyzones/FancyZonesLib/ZoneSet.cpp index 0f8c2ab040..04f60deccf 100644 --- a/src/modules/fancyzones/FancyZonesLib/ZoneSet.cpp +++ b/src/modules/fancyzones/FancyZonesLib/ZoneSet.cpp @@ -709,11 +709,11 @@ ZoneIndexSet ZoneSet::ZoneSelectSubregion(const ZoneIndexSet& capturedZones, POI if (verticalSplit) { - zoneIndex = (pt.y - overlap.top) * capturedZones.size() / height; + zoneIndex = (static_cast(pt.y) - overlap.top) * capturedZones.size() / height; } else { - zoneIndex = (pt.x - overlap.left) * capturedZones.size() / width; + zoneIndex = (static_cast(pt.x) - overlap.left) * capturedZones.size() / width; } zoneIndex = std::clamp(zoneIndex, ZoneIndex(0), static_cast(capturedZones.size()) - 1); diff --git a/src/modules/keyboardmanager/KeyboardManagerEngineLibrary/KeyboardEventHandlers.cpp b/src/modules/keyboardmanager/KeyboardManagerEngineLibrary/KeyboardEventHandlers.cpp index 89ab272c93..f2ee2447f3 100644 --- a/src/modules/keyboardmanager/KeyboardManagerEngineLibrary/KeyboardEventHandlers.cpp +++ b/src/modules/keyboardmanager/KeyboardManagerEngineLibrary/KeyboardEventHandlers.cpp @@ -587,7 +587,7 @@ namespace KeyboardEventHandlers { DWORD to = std::get<0>(newRemapping.targetShortcut); bool isLastKeyStillPressed = ii.GetVirtualKeyState((WORD)from.actionKey); - key_count = from.Size() - 1 + 1 + (isLastKeyStillPressed ? 1 : 0); + key_count = static_cast(from.Size()) - 1 + 1 + (isLastKeyStillPressed ? 1 : 0); keyEventList = new INPUT[key_count](); memset(keyEventList, 0, sizeof(keyEventList)); int i = 0; @@ -604,7 +604,10 @@ namespace KeyboardEventHandlers Shortcut to = std::get(newRemapping.targetShortcut); bool isLastKeyStillPressed = ii.GetVirtualKeyState((WORD)from.actionKey); - key_count = from.Size() - 1 + to.Size() - 1 - 2* from.GetCommonModifiersCount(to) + 1 + (isLastKeyStillPressed ? 1 : 0); + size_t temp_key_count_calculation = static_cast(from.Size()) - 1; + temp_key_count_calculation += static_cast(to.Size()) - 1; + temp_key_count_calculation -= static_cast(2) * from.GetCommonModifiersCount(to); + key_count = temp_key_count_calculation + 1 + (isLastKeyStillPressed ? 1 : 0); keyEventList = new INPUT[key_count](); int i = 0; diff --git a/src/modules/powerrename/lib/PowerRenameLib.vcxproj b/src/modules/powerrename/lib/PowerRenameLib.vcxproj index 42785e8255..4b9594c2b1 100644 --- a/src/modules/powerrename/lib/PowerRenameLib.vcxproj +++ b/src/modules/powerrename/lib/PowerRenameLib.vcxproj @@ -25,7 +25,7 @@ - Level4 + Level3 WIN32;_LIB;%(PreprocessorDefinitions) $(ProjectDir)..\;$(ProjectDir)..\ui;$(ProjectDir)..\dll;$(ProjectDir)..\lib;$(ProjectDir)..\..\..\;$(ProjectDir)..\..\..\common\Telemetry;%(AdditionalIncludeDirectories);$(GeneratedFilesDir) diff --git a/src/modules/videoconference/VideoConferenceShared/SerializedSharedMemory.cpp b/src/modules/videoconference/VideoConferenceShared/SerializedSharedMemory.cpp index 1a95310e80..a7620e7741 100644 --- a/src/modules/videoconference/VideoConferenceShared/SerializedSharedMemory.cpp +++ b/src/modules/videoconference/VideoConferenceShared/SerializedSharedMemory.cpp @@ -83,7 +83,7 @@ std::optional SerializedSharedMemory::create(const std:: } // We need an extra byte for locking if it's not readonly - const ULARGE_INTEGER UISize{ .QuadPart = size + !read_only }; + const ULARGE_INTEGER UISize{ .QuadPart = static_cast(size) + !read_only }; wil::unique_handle hMapFile{ CreateFileMappingW(INVALID_HANDLE_VALUE, maybe_attributes ? maybe_attributes : &sa, diff --git a/src/runner/powertoy_module.cpp b/src/runner/powertoy_module.cpp index d350979f46..cc5cb86d20 100644 --- a/src/runner/powertoy_module.cpp +++ b/src/runner/powertoy_module.cpp @@ -34,7 +34,7 @@ json::JsonObject PowertoyModule::json_config() const int size = 0; pt_module->get_config(nullptr, &size); std::wstring result; - result.resize(size - 1); + result.resize(static_cast(size) - 1); pt_module->get_config(result.data(), &size); return json::JsonObject::Parse(result); } diff --git a/tools/BugReportTool/BugReportTool/BugReportTool.vcxproj b/tools/BugReportTool/BugReportTool/BugReportTool.vcxproj index a6253c719d..8f24ed5f45 100644 --- a/tools/BugReportTool/BugReportTool/BugReportTool.vcxproj +++ b/tools/BugReportTool/BugReportTool/BugReportTool.vcxproj @@ -44,7 +44,8 @@ - TurnOffAllWarnings + + 26451;4267;4244;%(DisableSpecificWarnings)