From fafaafd578e310e70e32c1cf099e7bd668d0b362 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Sat, 14 Jan 2023 01:51:34 +0100 Subject: [PATCH] Fix crash when showing frame for cursor condition Extreme values for setGeometry() could cause a crash. Limit frame size to screen union to avoid these crashes. --- src/macro-core/macro-condition-cursor.cpp | 38 +++++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/macro-core/macro-condition-cursor.cpp b/src/macro-core/macro-condition-cursor.cpp index 90c8a6aa..0ea702cb 100644 --- a/src/macro-core/macro-condition-cursor.cpp +++ b/src/macro-core/macro-condition-cursor.cpp @@ -3,6 +3,8 @@ #include "utility.hpp" #include "advanced-scene-switcher.hpp" +#include + const std::string MacroConditionCursor::id = "cursor"; bool MacroConditionCursor::_registered = MacroConditionFactory::Register( @@ -294,6 +296,29 @@ void MacroConditionCursorEdit::SetWidgetVisibility() adjustSize(); } +static QRect getScreenUnionRect() +{ + QPoint screenTopLeft(0, 0); + QPoint screenBottomRight(0, 0); + auto screens = QGuiApplication::screens(); + for (const auto &screen : screens) { + const auto geo = screen->geometry(); + if (geo.topLeft().x() < screenTopLeft.x()) { + screenTopLeft.setX(geo.topLeft().x()); + } + if (geo.topLeft().y() < screenTopLeft.y()) { + screenTopLeft.setY(geo.topLeft().y()); + } + if (geo.bottomRight().x() > screenBottomRight.x()) { + screenBottomRight.setX(geo.bottomRight().x()); + } + if (geo.bottomRight().y() > screenBottomRight.y()) { + screenBottomRight.setY(geo.bottomRight().y()); + } + } + return QRect(screenTopLeft, screenBottomRight); +} + void MacroConditionCursorEdit::SetupFrame() { _frame.setFrameStyle(QFrame::Box | QFrame::Plain); @@ -304,9 +329,16 @@ void MacroConditionCursorEdit::SetupFrame() _frame.setAttribute(Qt::WA_TranslucentBackground, true); if (_entryData) { - _frame.setGeometry(_entryData->_minX, _entryData->_minY, - _entryData->_maxX - _entryData->_minX, - _entryData->_maxY - _entryData->_minY); + QRect rect(_entryData->_minX, _entryData->_minY, + _entryData->_maxX - _entryData->_minX, + _entryData->_maxY - _entryData->_minY); + + if (rect.size().height() == 0 || rect.size().width() == 0) { + _frame.setGeometry(0, 0, 1, 1); + return; + } + + _frame.setGeometry(getScreenUnionRect().intersected(rect)); } }