Skip to content

Commit 17f2639

Browse files
Samuel RødalThe Qt Project
Samuel Rødal
authored and
The Qt Project
committed
Fixed potential invalid memory access in OpenGL engine glyph cache.
We need to clamp glyph_height to prevent from overflowing the texture cache. A slightly similar issue was fixed for glyph_width in change 9520674. Also, we should only increment the y_offset in the place where we check for an overflow. Task-number: QTBUG-26649 Change-Id: I297191b2ffd68a636bfced7f5284fd3b9383e988 Reviewed-by: Eskil Abrahamsen Blomfeldt <[email protected]>
1 parent 64e25b0 commit 17f2639

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/opengl/qpaintengine_opengl.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4810,7 +4810,7 @@ void QGLGlyphCache::cacheGlyphs(QGLContext *context, QFontEngine *fontEngine,
48104810
int strip_height = qt_next_power_of_two(qRound(fontEngine->ascent().toReal() + fontEngine->descent().toReal())+2);
48114811
font_tex->x_offset = x_margin;
48124812
font_tex->y_offset += strip_height;
4813-
if (font_tex->y_offset >= font_tex->height) {
4813+
if (font_tex->y_offset + strip_height > font_tex->height) {
48144814
// get hold of the old font texture
48154815
uchar *old_tex_data = font_tex->data;
48164816
int old_tex_height = font_tex->height;
@@ -4838,6 +4838,8 @@ void QGLGlyphCache::cacheGlyphs(QGLContext *context, QFontEngine *fontEngine,
48384838
}
48394839
}
48404840

4841+
glyph_height = qMin(glyph_height, glyph_im.height());
4842+
48414843
QGLGlyphCoord *qgl_glyph = new QGLGlyphCoord;
48424844
qgl_glyph->x = qreal(font_tex->x_offset) / font_tex->width;
48434845
qgl_glyph->y = qreal(font_tex->y_offset) / font_tex->height;
@@ -4855,8 +4857,8 @@ void QGLGlyphCache::cacheGlyphs(QGLContext *context, QFontEngine *fontEngine,
48554857

48564858
if (!glyph_im.isNull()) {
48574859
int idx = 0;
4858-
uchar *tex_data = (uchar *) malloc(glyph_width*glyph_im.height()*2);
4859-
memset(tex_data, 0, glyph_width*glyph_im.height()*2);
4860+
uchar *tex_data = (uchar *) malloc(glyph_width*glyph_height*2);
4861+
memset(tex_data, 0, glyph_width*glyph_height*2);
48604862

48614863
bool is8BitGray = false;
48624864
#ifdef Q_WS_QPA
@@ -4866,7 +4868,7 @@ void QGLGlyphCache::cacheGlyphs(QGLContext *context, QFontEngine *fontEngine,
48664868
#endif
48674869
glyph_im = glyph_im.convertToFormat(QImage::Format_Indexed8);
48684870
int cacheLineStart = (font_tex->x_offset + font_tex->y_offset*font_tex->width)*2;
4869-
for (int y=0; y<glyph_im.height(); ++y) {
4871+
for (int y=0; y<glyph_height; ++y) {
48704872
uchar *s = (uchar *) glyph_im.scanLine(y);
48714873
int lineStart = idx;
48724874
for (int x=0; x<glyph_im.width(); ++x) {
@@ -4883,16 +4885,12 @@ void QGLGlyphCache::cacheGlyphs(QGLContext *context, QFontEngine *fontEngine,
48834885
cacheLineStart += font_tex->width*2;
48844886
}
48854887
glTexSubImage2D(GL_TEXTURE_2D, 0, font_tex->x_offset, font_tex->y_offset,
4886-
glyph_width, glyph_im.height(),
4888+
glyph_width, glyph_height,
48874889
GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, tex_data);
48884890
free(tex_data);
48894891
}
4890-
if (font_tex->x_offset + glyph_width + x_margin > font_tex->width) {
4891-
font_tex->x_offset = x_margin;
4892-
font_tex->y_offset += glyph_height + y_margin;
4893-
} else {
4894-
font_tex->x_offset += glyph_width + x_margin;
4895-
}
4892+
4893+
font_tex->x_offset += glyph_width + x_margin;
48964894

48974895
cache->insert(glyphs[i], qgl_glyph);
48984896
}

0 commit comments

Comments
 (0)