summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEirik Aavitsland <eirik.aavitsland@qt.io>2019-03-29 10:36:21 +0100
committerHeikki Halmet <heikki.halmet@qt.io>2019-04-01 11:56:42 +0000
commit3b7db8ac90ba36949cb4168f07cc8dace47758a7 (patch)
treec0d2d883f582a5a164dc334cb1cd3cb818b45b82
parent9e97d64ccd27adcb2053c90cfbcafaef68e53893 (diff)
Fix assert/crash when creating QBrush with null QGradient
The QBrush constructor taking a QGradient would assert or crash if passed a null (NoGradient) gradient. But it is not necessary for the API to be as brittle as that: instead the result can simply be a null QBrush object, i.e. the same as the default QBrush() constructor creates (style == NoBrush). This issue comes up now since with the recent introduction of QGradient presets, the API opens for using QGradient directly, whereas earlier, only the subclasses QLinearGradient etc. were to be used. Fixes: QTBUG-74648 Change-Id: I1a9b1c4654e4375aa6684700a262cc0946851448 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io> Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
-rw-r--r--src/gui/painting/qbrush.cpp12
-rw-r--r--tests/auto/gui/painting/qbrush/tst_qbrush.cpp2
2 files changed, 10 insertions, 4 deletions
diff --git a/src/gui/painting/qbrush.cpp b/src/gui/painting/qbrush.cpp
index 860653cc4c..bcc23fa683 100644
--- a/src/gui/painting/qbrush.cpp
+++ b/src/gui/painting/qbrush.cpp
@@ -545,9 +545,11 @@ QBrush::QBrush(const QBrush &other)
*/
QBrush::QBrush(const QGradient &gradient)
{
- Q_ASSERT_X(gradient.type() != QGradient::NoGradient, "QBrush::QBrush",
- "QGradient should not be used directly, use the linear, radial\n"
- "or conical gradients instead");
+ if (Q_UNLIKELY(gradient.type() == QGradient::NoGradient)) {
+ d.reset(nullBrushInstance());
+ d->ref.ref();
+ return;
+ }
const Qt::BrushStyle enum_table[] = {
Qt::LinearGradientPattern,
@@ -1376,8 +1378,10 @@ QGradient::QGradient(Preset preset)
}();
const QJsonValue presetData = jsonPresets[preset - 1];
- if (!presetData.isObject())
+ if (!presetData.isObject()) {
+ qWarning("QGradient: Undefined preset %i", preset);
return;
+ }
m_type = LinearGradient;
setCoordinateMode(ObjectMode);
diff --git a/tests/auto/gui/painting/qbrush/tst_qbrush.cpp b/tests/auto/gui/painting/qbrush/tst_qbrush.cpp
index cd3eaa1478..ce6ce15767 100644
--- a/tests/auto/gui/painting/qbrush/tst_qbrush.cpp
+++ b/tests/auto/gui/painting/qbrush/tst_qbrush.cpp
@@ -345,6 +345,8 @@ void tst_QBrush::gradientPresets()
QGradient invalidPreset(QGradient::Preset(-1));
QCOMPARE(invalidPreset.type(), QGradient::NoGradient);
+ QBrush brush(invalidPreset);
+ QCOMPARE(brush.style(), Qt::NoBrush);
}
void fill(QPaintDevice *pd) {