From ac8a3b948da1980bc59bae3fc76d20b5b45662a0 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 9 Feb 2016 09:14:12 +0100 Subject: [PATCH] QtGui: Use QImage::constBits()/constScanLine() in non-const contexts. Prevent potential detaching by using constBits()/constScanLine() instead of bits()/scanLine(). Change-Id: If03f8d4d3b8ed4c07aed5eff7f580e57ca771919 Reviewed-by: Gunnar Sletta Reviewed-by: Marc Mutz --- src/gui/image/qgifhandler.cpp | 4 ++-- src/gui/image/qpixmap_blitter.cpp | 2 +- src/gui/image/qpixmap_win.cpp | 2 +- src/gui/image/qppmhandler.cpp | 6 +++--- src/gui/image/qxbmhandler.cpp | 2 +- src/gui/image/qxpmhandler.cpp | 4 ++-- src/gui/painting/qpaintengine_raster.cpp | 4 ++-- src/gui/painting/qregion.cpp | 4 ++-- src/gui/painting/qtextureglyphcache.cpp | 6 +++--- src/gui/text/qfontengine.cpp | 8 ++++---- 10 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/gui/image/qgifhandler.cpp b/src/gui/image/qgifhandler.cpp index 7ba6b123e8a..9c748c03734 100644 --- a/src/gui/image/qgifhandler.cpp +++ b/src/gui/image/qgifhandler.cpp @@ -199,7 +199,7 @@ void QGIFFormat::disposePrevious(QImage *image) fillRect(image, l, t, r-l+1, b-t+1, color(bgcol)); } else { // Impossible: We don't know of a bgcol - use pixel 0 - QRgb *bits = (QRgb*)image->bits(); + const QRgb *bits = reinterpret_cast(image->constBits()); fillRect(image, l, t, r-l+1, b-t+1, bits[0]); } // ### Changed: QRect(l, t, r-l+1, b-t+1) @@ -208,7 +208,7 @@ void QGIFFormat::disposePrevious(QImage *image) if (frame >= 0) { for (int ln=t; ln<=b; ln++) { memcpy(image->scanLine(ln)+l, - backingstore.scanLine(ln-t), + backingstore.constScanLine(ln-t), (r-l+1)*sizeof(QRgb)); } // ### Changed: QRect(l, t, r-l+1, b-t+1) diff --git a/src/gui/image/qpixmap_blitter.cpp b/src/gui/image/qpixmap_blitter.cpp index b254c5a2af2..a68425e1006 100644 --- a/src/gui/image/qpixmap_blitter.cpp +++ b/src/gui/image/qpixmap_blitter.cpp @@ -183,7 +183,7 @@ void QBlittablePlatformPixmap::fromImage(const QImage &image, correctFormatPic = correctFormatPic.convertToFormat(thisImg->format(), flags); uchar *mem = thisImg->bits(); - const uchar *bits = correctFormatPic.bits(); + const uchar *bits = correctFormatPic.constBits(); int bytesCopied = 0; while (bytesCopied < correctFormatPic.byteCount()) { memcpy(mem,bits,correctFormatPic.bytesPerLine()); diff --git a/src/gui/image/qpixmap_win.cpp b/src/gui/image/qpixmap_win.cpp index a7a9b375ff4..8db3bdbc7f3 100644 --- a/src/gui/image/qpixmap_win.cpp +++ b/src/gui/image/qpixmap_win.cpp @@ -198,7 +198,7 @@ Q_GUI_EXPORT HBITMAP qt_createIconMask(const QBitmap &bitmap) QScopedArrayPointer bits(new uchar[bpl * h]); bm.invertPixels(); for (int y = 0; y < h; ++y) - memcpy(bits.data() + y * bpl, bm.scanLine(y), bpl); + memcpy(bits.data() + y * bpl, bm.constScanLine(y), bpl); HBITMAP hbm = CreateBitmap(w, h, 1, 1, bits.data()); return hbm; } diff --git a/src/gui/image/qppmhandler.cpp b/src/gui/image/qppmhandler.cpp index 7f23656c02e..6eb35e15586 100644 --- a/src/gui/image/qppmhandler.cpp +++ b/src/gui/image/qppmhandler.cpp @@ -329,7 +329,7 @@ static bool write_pbm_image(QIODevice *out, const QImage &sourceImage, const QBy if (image.format() == QImage::Format_Indexed8) { QVector color = image.colorTable(); for (uint y=0; y(image.constScanLine(y)); uchar *p = buf; uchar *end = buf+bpl; while (p < end) { diff --git a/src/gui/image/qxbmhandler.cpp b/src/gui/image/qxbmhandler.cpp index 81525d9dd6e..44d07f1624e 100644 --- a/src/gui/image/qxbmhandler.cpp +++ b/src/gui/image/qxbmhandler.cpp @@ -210,7 +210,7 @@ static bool write_xbm_image(const QImage &sourceImage, QIODevice *device, const char *p = buf; int bpl = (w+7)/8; for (int y = 0; y < h; ++y) { - uchar *b = image.scanLine(y); + const uchar *b = image.constScanLine(y); for (i = 0; i < bpl; ++i) { *p++ = '0'; *p++ = 'x'; *p++ = hexrep[*b >> 4]; diff --git a/src/gui/image/qxpmhandler.cpp b/src/gui/image/qxpmhandler.cpp index e9ac4a9cc29..fbce78eb748 100644 --- a/src/gui/image/qxpmhandler.cpp +++ b/src/gui/image/qxpmhandler.cpp @@ -1098,7 +1098,7 @@ static bool write_xpm_image(const QImage &sourceImage, QIODevice *device, const // build color table for(y=0; y(image.constScanLine(y)); for(x=0; x(image.constScanLine(y)); int cc = 0; for(x=0; x(dest.scanLine(y)); if (!source || !target) QT_THROW(std::bad_alloc()); // we must have run out of memory diff --git a/src/gui/painting/qregion.cpp b/src/gui/painting/qregion.cpp index 5e648eabf53..757c78cec8e 100644 --- a/src/gui/painting/qregion.cpp +++ b/src/gui/painting/qregion.cpp @@ -3735,7 +3735,7 @@ static QRegionPrivate *PolygonRegion(const QPoint *Pts, int Count, int rule) QRegionPrivate *qt_bitmapToRegion(const QBitmap& bitmap) { - QImage image = bitmap.toImage(); + const QImage image = bitmap.toImage(); QRegionPrivate *region = new QRegionPrivate; @@ -3753,7 +3753,7 @@ QRegionPrivate *qt_bitmapToRegion(const QBitmap& bitmap) int x, y; for (y = 0; y < image.height(); ++y) { - uchar *line = image.scanLine(y); + const uchar *line = image.constScanLine(y); int w = image.width(); uchar all = zero; int prev1 = -1; diff --git a/src/gui/painting/qtextureglyphcache.cpp b/src/gui/painting/qtextureglyphcache.cpp index 97f82d16d39..20039d902a2 100644 --- a/src/gui/painting/qtextureglyphcache.cpp +++ b/src/gui/painting/qtextureglyphcache.cpp @@ -339,7 +339,7 @@ void QImageTextureGlyphCache::fillTexture(const Coord &c, glyph_t g, QFixed subP uchar *dest = d + (c.y + y) *dbpl + c.x/8; if (y < mh) { - uchar *src = mask.scanLine(y); + const uchar *src = mask.constScanLine(y); for (int x = 0; x < c.w/8; ++x) { if (x < (mw+7)/8) dest[x] = src[x]; @@ -361,7 +361,7 @@ void QImageTextureGlyphCache::fillTexture(const Coord &c, glyph_t g, QFixed subP for (int y = 0; y < c.h; ++y) { uchar *dest = d + (c.y + y) *dbpl + c.x; if (y < mh) { - uchar *src = (uchar *) mask.scanLine(y); + const uchar *src = mask.constScanLine(y); for (int x = 0; x < c.w; ++x) { if (x < mw) dest[x] = (src[x >> 3] & (1 << (7 - (x & 7)))) > 0 ? 255 : 0; @@ -372,7 +372,7 @@ void QImageTextureGlyphCache::fillTexture(const Coord &c, glyph_t g, QFixed subP for (int y = 0; y < c.h; ++y) { uchar *dest = d + (c.y + y) *dbpl + c.x; if (y < mh) { - uchar *src = (uchar *) mask.scanLine(y); + const uchar *src = mask.constScanLine(y); for (int x = 0; x < c.w; ++x) { if (x < mw) dest[x] = src[x]; diff --git a/src/gui/text/qfontengine.cpp b/src/gui/text/qfontengine.cpp index fc66c4ec4cb..03ad6a24e97 100644 --- a/src/gui/text/qfontengine.cpp +++ b/src/gui/text/qfontengine.cpp @@ -823,7 +823,7 @@ void QFontEngine::addBitmapFontToPath(qreal x, qreal y, const QGlyphLayout &glyp } } } - const uchar *bitmap_data = bitmap.bits(); + const uchar *bitmap_data = bitmap.constBits(); QFixedPoint offset = glyphs.offsets[i]; advanceX += offset.x; advanceY += offset.y; @@ -880,12 +880,12 @@ QImage QFontEngine::alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition, con QImage QFontEngine::alphaRGBMapForGlyph(glyph_t glyph, QFixed /*subPixelPosition*/, const QTransform &t) { - QImage alphaMask = alphaMapForGlyph(glyph, t); + const QImage alphaMask = alphaMapForGlyph(glyph, t); QImage rgbMask(alphaMask.width(), alphaMask.height(), QImage::Format_RGB32); for (int y=0; y(im.constScanLine(y)); for (int x=0; x