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>
bb10
Eirik Aavitsland 2019-03-29 10:36:21 +01:00 committed by Heikki Halmet
parent 9e97d64ccd
commit 3b7db8ac90
2 changed files with 10 additions and 4 deletions

View File

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

View File

@ -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) {