From 28899df48587d3ea528228043c9eba53724889a4 Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Mon, 17 Oct 2022 09:34:20 +0000 Subject: [PATCH] Make Workspace::outputAt() more robust to extreme values Since QPointF can have values that exceed INT_MAX, "distance < minDistance" may not be triggered for the first output. In order to make Workspace::outputAt() more robust to such extreme cases, this patch changes the type of minDistance from int to qreal to avoid truncation and adds an explicit check to initialize bestOutput when we see the first workspace output. It would be also great to add size hints sanitization code in kwin, but it can be done later. BUG: 460446 --- Should close: * https://errors-eval.kde.org/organizations/kde/issues/321 * https://errors-eval.kde.org/organizations/kde/issues/341 --- src/workspace.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/workspace.cpp b/src/workspace.cpp index 85ce1d7bca..6712b72c2b 100644 --- a/src/workspace.cpp +++ b/src/workspace.cpp @@ -1390,7 +1390,8 @@ void Workspace::updateCurrentActivity(const QString &new_activity) Output *Workspace::outputAt(const QPointF &pos) const { Output *bestOutput = nullptr; - int minDistance = INT_MAX; + qreal minDistance; + for (Output *output : std::as_const(m_outputs)) { const QRect &geo = output->geometry(); if (geo.contains(pos.toPoint())) { @@ -1400,7 +1401,7 @@ Output *Workspace::outputAt(const QPointF &pos) const distance = std::min(distance, QPointF(geo.topRight() - pos).manhattanLength()); distance = std::min(distance, QPointF(geo.bottomRight() - pos).manhattanLength()); distance = std::min(distance, QPointF(geo.bottomLeft() - pos).manhattanLength()); - if (distance < minDistance) { + if (!bestOutput || distance < minDistance) { minDistance = distance; bestOutput = output; }