From 2305c8e754cd18cbb680c4aba4037406f4576794 Mon Sep 17 00:00:00 2001 From: Seraphima Zykova Date: Tue, 29 Aug 2023 19:59:02 +0300 Subject: [PATCH] [FancyZones] Prevent snapping to invalid zone (#28206) --- .../fancyzones/FancyZonesLib/WorkArea.cpp | 8 +++++++ .../UnitTests/WorkArea.Spec.cpp | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/modules/fancyzones/FancyZonesLib/WorkArea.cpp b/src/modules/fancyzones/FancyZonesLib/WorkArea.cpp index 002bcdc63b..2d4b6adf90 100644 --- a/src/modules/fancyzones/FancyZonesLib/WorkArea.cpp +++ b/src/modules/fancyzones/FancyZonesLib/WorkArea.cpp @@ -124,6 +124,14 @@ bool WorkArea::Snap(HWND window, const ZoneIndexSet& zones, bool updatePosition) return false; } + for (ZoneIndex zone : zones) + { + if (static_cast(zone) >= m_layout->Zones().size()) + { + return false; + } + } + m_layoutWindows.Assign(window, zones); AppZoneHistory::instance().SetAppLastZones(window, m_uniqueId, m_layout->Id(), zones); diff --git a/src/modules/fancyzones/FancyZonesTests/UnitTests/WorkArea.Spec.cpp b/src/modules/fancyzones/FancyZonesTests/UnitTests/WorkArea.Spec.cpp index c365a06c03..0506e0c6d4 100644 --- a/src/modules/fancyzones/FancyZonesTests/UnitTests/WorkArea.Spec.cpp +++ b/src/modules/fancyzones/FancyZonesTests/UnitTests/WorkArea.Spec.cpp @@ -316,6 +316,28 @@ namespace FancyZonesUnitTests Assert::IsTrue(expected == layoutWindows.GetZoneIndexSetFromWindow(window)); } + TEST_METHOD(SnapEmptyZones) + { + const auto workArea = WorkArea::Create(m_hInst, m_workAreaId, m_parentUniqueId, m_workAreaRect); + const auto window = Mocks::WindowCreate(m_hInst); + + Assert::IsFalse(workArea->Snap(window, {})); + } + + TEST_METHOD(SnapToIncorrectZone) + { + const auto workArea = WorkArea::Create(m_hInst, m_workAreaId, m_parentUniqueId, m_workAreaRect); + const auto window = Mocks::WindowCreate(m_hInst); + + const ZoneIndexSet zones = { 10 }; + Assert::IsFalse(workArea->Snap(window, zones)); + + const auto processPath = get_process_path(window); + const auto history = AppZoneHistory::instance().GetZoneHistory(processPath, m_workAreaId); + + Assert::IsFalse(history.has_value()); + } + TEST_METHOD (UnsnapPropertyTest) { const auto workArea = WorkArea::Create(m_hInst, m_workAreaId, m_parentUniqueId, m_workAreaRect);