skia2/gm/fontcache.cpp
herb c1e97b372e Fix uninitialized memory bug in the SkGlyphCache.
The core of the problem is that the system is asked to lookup the metrics for a character with id == 0. This causes a hit in the fCharToGlyphHash matching the sentinel glyph. This happens because fCharToGlpyhHash is initialized with all zeros, therefore, the fID is zero matching the char with id == 0. The fAdvanceX field of the sentinel glyph is in fact not initialized.

The bigger question is now did a zero character get passed to getUnicharMetrics?

The breaking code is basically as follows:
wchar_t glyph = L'S';
paint.measureText(&glyph, 2);

This get mischaracterized as a utf8 string instead of a utf16(?) string. Because of the little endian ordering, this is the character string 'L' '\0'. Since the size of the original string is two bytes (but a single character) the '\0' is treated as its own character and past to getUnicharMetrics.

TEST:
On windows failed using DrMemory. With this change does not fail.

BUG=463204

Review URL: https://codereview.chromium.org/977063002
2015-03-05 11:51:11 -08:00

87 lines
2.3 KiB
C++

/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm.h"
#include "SkCanvas.h"
#include "SkGraphics.h"
#include "SkTypeface.h"
// GM to stress the GPU font cache
static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x,
SkScalar y, const SkPaint& paint) {
canvas->drawText(text.c_str(), text.size(), x, y, paint);
return x + paint.measureText(text.c_str(), text.size());
}
class FontCacheGM : public skiagm::GM {
public:
FontCacheGM() {
fTypefaces[0] = NULL;
fTypefaces[1] = NULL;
}
virtual ~FontCacheGM() {
SkSafeUnref(fTypefaces[0]);
SkSafeUnref(fTypefaces[1]);
}
protected:
SkString onShortName() SK_OVERRIDE {
return SkString("fontcache");
}
SkISize onISize() SK_OVERRIDE {
return SkISize::Make(1280, 640);
}
void onOnceBeforeDraw() SK_OVERRIDE {
fTypefaces[0] = sk_tool_utils::create_portable_typeface("serif", SkTypeface::kItalic);
fTypefaces[1] = sk_tool_utils::create_portable_typeface("sans-serif", SkTypeface::kItalic);
}
void onDraw(SkCanvas* canvas) SK_OVERRIDE {
SkPaint paint;
paint.setAntiAlias(true);
paint.setLCDRenderText(true);
paint.setSubpixelText(true);
paint.setTypeface(fTypefaces[0]);
paint.setTextSize(192);
// Make sure the nul character does not cause problems.
paint.measureText("\0", 1);
SkScalar x = 20;
SkScalar y = 128;
SkString text("ABCDEFGHIJ");
draw_string(canvas, text, x, y, paint);
y += 100;
SkString text2("KLMNOPQRS");
draw_string(canvas, text2, x, y, paint);
y += 100;
SkString text3("TUVWXYZ012");
draw_string(canvas, text3, x, y, paint);
y += 100;
paint.setTypeface(fTypefaces[1]);
draw_string(canvas, text, x, y, paint);
y += 100;
draw_string(canvas, text2, x, y, paint);
y += 100;
draw_string(canvas, text3, x, y, paint);
y += 100;
}
private:
SkTypeface* fTypefaces[2];
typedef GM INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
DEF_GM( return SkNEW(FontCacheGM); )