aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2021-04-23 12:16:26 +0200
committerMitch Curtis <mitch.curtis@qt.io>2021-04-26 12:53:33 +0200
commita29d29c020c1d6f7fa5efbb80aa634f43202cd96 (patch)
tree614a97a0f3e47fe3747df7ab264eb0a27006bead
parented2e373281a17011b66f68ad19c628c6f02afa63 (diff)
ScrollView: fix crash when scrolling with zero-sized item
Check if a Flickable type was actually set as the contentItem before accessing the pointer. Also warn that using a type other than Flickable is not supported. Fixes: QTBUG-93039 Change-Id: I1470766c6de02b7b601edf1375791d3147f26ab5 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> (cherry picked from commit e7df2279bf5519703fd0b853abaa23947a599920)
-rw-r--r--src/quicktemplates2/qquickscrollbar.cpp6
-rw-r--r--src/quicktemplates2/qquickscrollview.cpp6
-rw-r--r--tests/auto/controls/data/tst_scrollview.qml24
3 files changed, 35 insertions, 1 deletions
diff --git a/src/quicktemplates2/qquickscrollbar.cpp b/src/quicktemplates2/qquickscrollbar.cpp
index f7b403d7..6c453026 100644
--- a/src/quicktemplates2/qquickscrollbar.cpp
+++ b/src/quicktemplates2/qquickscrollbar.cpp
@@ -869,6 +869,9 @@ class QQuickFriendlyFlickable : public QQuickFlickable
void QQuickScrollBarAttachedPrivate::scrollHorizontal()
{
+ if (!flickable)
+ return;
+
QQuickFriendlyFlickable *f = reinterpret_cast<QQuickFriendlyFlickable *>(flickable);
const qreal viewwidth = f->width();
@@ -881,6 +884,9 @@ void QQuickScrollBarAttachedPrivate::scrollHorizontal()
void QQuickScrollBarAttachedPrivate::scrollVertical()
{
+ if (!flickable)
+ return;
+
QQuickFriendlyFlickable *f = reinterpret_cast<QQuickFriendlyFlickable *>(flickable);
const qreal viewheight = f->height();
diff --git a/src/quicktemplates2/qquickscrollview.cpp b/src/quicktemplates2/qquickscrollview.cpp
index a94c4595..7b089ccc 100644
--- a/src/quicktemplates2/qquickscrollview.cpp
+++ b/src/quicktemplates2/qquickscrollview.cpp
@@ -38,6 +38,7 @@
#include "qquickpane_p_p.h"
#include "qquickscrollbar_p_p.h"
+#include <QtQml/qqmlinfo.h>
#include <QtQuick/private/qquickflickable_p.h>
QT_BEGIN_NAMESPACE
@@ -585,7 +586,10 @@ void QQuickScrollView::contentItemChange(QQuickItem *newItem, QQuickItem *oldIte
// assume/require that it has an explicit content size assigned.
d->flickableHasExplicitContentWidth = true;
d->flickableHasExplicitContentHeight = true;
- d->setFlickable(qobject_cast<QQuickFlickable *>(newItem), false);
+ auto newItemAsFlickable = qobject_cast<QQuickFlickable *>(newItem);
+ if (newItem && !newItemAsFlickable)
+ qmlWarning(this) << "ScrollView only supports Flickable types as its contentItem";
+ d->setFlickable(newItemAsFlickable, false);
}
QQuickPane::contentItemChange(newItem, oldItem);
}
diff --git a/tests/auto/controls/data/tst_scrollview.qml b/tests/auto/controls/data/tst_scrollview.qml
index d8c210a4..7404d0e6 100644
--- a/tests/auto/controls/data/tst_scrollview.qml
+++ b/tests/auto/controls/data/tst_scrollview.qml
@@ -502,4 +502,28 @@ TestCase {
compare(control.contentWidth, flickable.contentWidth)
compare(control.contentHeight, flickable.contentHeight)
}
+
+ Component {
+ id: zeroSizedContentItemComponent
+ ScrollView {
+ width: 100
+ height: 100
+ contentItem: Item {}
+ }
+ }
+
+ function test_zeroSizedContentItem() {
+ ignoreWarning(/ScrollView only supports Flickable types as its contentItem/)
+ let control = createTemporaryObject(zeroSizedContentItemComponent, testCase)
+ verify(control)
+
+ let verticalScrollBar = control.ScrollBar.vertical
+ verify(verticalScrollBar)
+ // Scrolling a ScrollView with a zero-sized contentItem shouldn't crash.
+ mouseDrag(verticalScrollBar, verticalScrollBar.width / 2, verticalScrollBar.height / 2, 0, 50)
+
+ let horizontalScrollBar = control.ScrollBar.horizontal
+ verify(verticalScrollBar)
+ mouseDrag(horizontalScrollBar, horizontalScrollBar.width / 2, horizontalScrollBar.height / 2, 50, 0)
+ }
}