widgets: Avoid crash in QScroller

Quoting c6a32751 in okular[1]:

QScrollerPrivate::setDpiFromWidget() before Qt 5.14 crashes
when the target widget does not intersect a physical screen,
because QDesktopWidget returns screen index `-1` in this case,
which leads to an out-of-range read from QApplication::screens(),
which leads to a segfault when reading from an invalid QScreen* pointer.

[1] https://invent.kde.org/graphics/okular/-/commit/c6a32751

Fixes: QTBUG-88288
Change-Id: Ia572bf0207aa6d8ca2a209d22daa36b962e6de7d
Reviewed-by: Fabian Vogt <fabian@ritter-vogt.de>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Fabian Vogt 2020-11-25 20:04:34 +01:00 committed by Christophe Giboudeaux
parent a34a352682
commit 751892a21c

View File

@ -1031,7 +1031,8 @@ void QScrollerPrivate::setDpi(const QPointF &dpi)
*/
void QScrollerPrivate::setDpiFromWidget(QWidget *widget)
{
const QScreen *screen = QGuiApplication::screens().at(QApplication::desktop()->screenNumber(widget));
const int screenNumber = QApplication::desktop()->screenNumber(widget);
const QScreen *screen = screenNumber < 0 ? QGuiApplication::primaryScreen() : QGuiApplication::screens().at(screenNumber);
setDpi(QPointF(screen->physicalDotsPerInchX(), screen->physicalDotsPerInchY()));
}