From e460a9e8b89861ae3e1e14fcd2bd2cfda9ca2c68 Mon Sep 17 00:00:00 2001 From: yaozichen2025 Date: Fri, 7 Aug 2026 22:06:33 +0800 Subject: [PATCH] fix(platform): guard invalid screen geometry inputs Avoid dereferencing a null QScreen and clamp malformed panel sizes so callers never receive an inverted geometry. Add a regression test for headless/null-screen callers. --- platform/autotest/test-screen-area-utils.cpp | 7 +++++++ platform/ukui/screen-area-utils.cpp | 11 ++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/platform/autotest/test-screen-area-utils.cpp b/platform/autotest/test-screen-area-utils.cpp index 696892d..02bdb06 100644 --- a/platform/autotest/test-screen-area-utils.cpp +++ b/platform/autotest/test-screen-area-utils.cpp @@ -12,6 +12,7 @@ class screen_area_utils_test : public QObject private slots: void instanceIsShared(); + void nullScreenReturnsEmptyGeometry(); void availableGeometryStaysWithinScreenGeometry(); }; @@ -20,6 +21,12 @@ void screen_area_utils_test::instanceIsShared() QCOMPARE(ScreenAreaUtils::instance(), ScreenAreaUtils::instance()); } +void screen_area_utils_test::nullScreenReturnsEmptyGeometry() +{ + const QRect geometry = ScreenAreaUtils::instance()->getAvailableGeometry(nullptr); + QVERIFY(geometry.isEmpty()); +} + void screen_area_utils_test::availableGeometryStaysWithinScreenGeometry() { QScreen *screen = QGuiApplication::primaryScreen(); diff --git a/platform/ukui/screen-area-utils.cpp b/platform/ukui/screen-area-utils.cpp index 9afceeb..8251b82 100644 --- a/platform/ukui/screen-area-utils.cpp +++ b/platform/ukui/screen-area-utils.cpp @@ -61,8 +61,17 @@ ScreenAreaUtils::~ScreenAreaUtils() QRect ScreenAreaUtils::getAvailableGeometry(QScreen *screen) { + if (!screen) { + return {}; + } + QRect availableRect = screen->geometry(); - int adjustment = d->m_panelAutoHide ? 0 : d->m_panelSize; + const bool verticalPanel = d->m_panelPos == 2 || d->m_panelPos == 3; + const int screenExtent = verticalPanel ? availableRect.width() : availableRect.height(); + const int maximumAdjustment = qMax(0, screenExtent - 1); + const int adjustment = d->m_panelAutoHide + ? 0 + : qBound(0, d->m_panelSize, maximumAdjustment); //上: 1, 下: 0, 左: 2, 右: 3 switch (d->m_panelPos) { default: -- Gitee