QLabel::setPixmap(): remove the no-op self-masking

When calling QLabel::setPixmap() with a 1bpp pixmap (i.e. a bitmap), and
that pixmap didn't have a mask set, QLabel would then set the pixmap as
its own mask.

This seems to be a no-op due to how QPixmap::setMask is coded:

  void QPixmap::setMask(const QBitmap &mask) {

    // ...

    if (static_cast<const QPixmap &>(mask).data == data) // trying to selfmask
      return;

  }

Moreover, in order to convert the pixmap to a QBitmap, the code would just
straight downcast it, triggering UB (if the input to setPixmap wasn't a
QBitmap to begin with).

I *guess* this was done this way to avoid a QBitmap::fromPixmap call,
which however is not expensive at all if the pixmap is already 1bpp,
which QLabel::setPixmap checks explicitly (before attempting to mask the
pixmap). I don't know the historical reasons for the code to have the
shape it had (and the code history is from before open governance).

So get two birds with one stone: remove the no-op and also the UB.

Change-Id: Ibab20492c2945bd1d01f98a18b168fabc56292b0
Pick-to: 6.3 6.2 5.15
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Giuseppe D'Angelo 2022-02-24 01:10:43 +01:00
parent 96a025c28b
commit b20cf7feee

View File

@ -378,9 +378,6 @@ void QLabel::setPixmap(const QPixmap &pixmap)
d->pixmap = new QPixmap(pixmap);
}
if (d->pixmap->depth() == 1 && !d->pixmap->mask())
d->pixmap->setMask(*((QBitmap *)d->pixmap));
d->updateLabel();
}