Skip to content

Commit a0115cd

Browse files
committed
Fix the out-of-bounds memory access issue in the FTMask class.
1 parent f34a005 commit a0115cd

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

Diff for: src/vectors/freetype/FTMask.cpp

+23-11
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ struct RasterTarget {
6565
static void SpanFunc(int y, int, const FT_Span* spans, void* user) {
6666
auto* target = reinterpret_cast<RasterTarget*>(user);
6767
auto* q = target->origin - target->pitch * y + spans->x;
68-
auto c = spans->coverage;
69-
if (target->gammaTable) {
70-
c = target->gammaTable[c];
71-
}
68+
auto c = target->gammaTable[spans->coverage];
7269
auto aCount = spans->len;
7370
/**
7471
* For small-spans it is faster to do it by ourselves than calling memset.
@@ -118,21 +115,36 @@ void FTMask::onFillPath(const Path& path, const Matrix& matrix, bool needsGammaC
118115
ftPath.setFillType(path.getFillType());
119116
auto outlines = ftPath.getOutlines();
120117
auto ftLibrary = GetLibrary().library();
118+
if (!needsGammaCorrection) {
119+
FT_Bitmap bitmap;
120+
bitmap.width = static_cast<unsigned>(info.width());
121+
bitmap.rows = static_cast<unsigned>(info.height());
122+
bitmap.pitch = static_cast<int>(info.rowBytes());
123+
bitmap.buffer = static_cast<unsigned char*>(pixels);
124+
bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
125+
bitmap.num_grays = 256;
126+
for (auto& outline : outlines) {
127+
FT_Outline_Get_Bitmap(ftLibrary, &(outline->outline), &bitmap);
128+
}
129+
pixelRef->unlockPixels();
130+
return;
131+
}
121132
auto buffer = static_cast<unsigned char*>(pixels);
122133
int rows = info.height();
123134
int pitch = static_cast<int>(info.rowBytes());
124-
RasterTarget target{};
135+
RasterTarget target = {};
125136
target.origin = buffer + (rows - 1) * pitch;
126137
target.pitch = pitch;
127-
if (needsGammaCorrection) {
128-
target.gammaTable = PixelRefMask::GammaTable().data();
129-
} else {
130-
target.gammaTable = nullptr;
131-
}
138+
target.gammaTable = PixelRefMask::GammaTable().data();
132139
FT_Raster_Params params;
133-
params.flags = FT_RASTER_FLAG_AA | FT_RASTER_FLAG_DIRECT;
140+
params.flags = FT_RASTER_FLAG_AA | FT_RASTER_FLAG_DIRECT | FT_RASTER_FLAG_CLIP;
134141
params.gray_spans = SpanFunc;
135142
params.user = &target;
143+
auto& clip = params.clip_box;
144+
clip.xMin = 0;
145+
clip.yMin = 0;
146+
clip.xMax = (FT_Pos)info.width();
147+
clip.yMax = (FT_Pos)info.height();
136148
for (auto& outline : outlines) {
137149
FT_Outline_Render(ftLibrary, &(outline->outline), &params);
138150
}

0 commit comments

Comments
 (0)