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
This commit is contained in:
Vlad Zahorodnii 2022-10-17 09:34:20 +00:00
parent 207e78386a
commit 28899df485

View file

@ -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;
}