Commit a1092aed authored by Dominik Röttsches's avatar Dominik Röttsches Committed by Commit Bot

Fix leak in FontService PPAPI fallback function

Replace manual memory management of FontConfig options with unique_ptr's
and address a container-overflow issue in the unit test.

Bug: 961018
Change-Id: Ief6a44abb98a4d48372440bc62cb801bfddabeb0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1604263
Commit-Queue: Dominik Röttsches <drott@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Auto-Submit: Dominik Röttsches <drott@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#658486}
parent 7cceb59b
......@@ -40,7 +40,7 @@ std::string GetPostscriptNameFromFile(base::File& font_file) {
return "";
std::vector<char> file_contents;
file_contents.reserve(file_size);
file_contents.resize(file_size);
CHECK_EQ(file_size, font_file.Read(0, file_contents.data(), file_size));
std::string font_family_name;
FT_Library library;
......
......@@ -127,10 +127,12 @@ int MatchFontFaceWithFallback(const std::string& face,
bool is_italic,
uint32_t charset,
uint32_t fallback_family) {
FcLangSet* langset = FcLangSetCreate();
bool is_lgc = MSCharSetToFontconfig(langset, charset);
FcPattern* pattern = FcPatternCreate();
FcPatternAddString(pattern, FC_FAMILY,
std::unique_ptr<FcLangSet, decltype(&FcLangSetDestroy)> langset(
FcLangSetCreate(), &FcLangSetDestroy);
bool is_lgc = MSCharSetToFontconfig(langset.get(), charset);
std::unique_ptr<FcPattern, decltype(&FcPatternDestroy)> pattern(
FcPatternCreate(), &FcPatternDestroy);
FcPatternAddString(pattern.get(), FC_FAMILY,
reinterpret_cast<const FcChar8*>(face.c_str()));
// TODO(thestig) Check if we can access Chrome's per-script font preference
......@@ -152,20 +154,22 @@ int MatchFontFaceWithFallback(const std::string& face,
if (!generic_font_name.empty()) {
const FcChar8* fc_generic_font_name =
reinterpret_cast<const FcChar8*>(generic_font_name.c_str());
FcPatternAddString(pattern, FC_FAMILY, fc_generic_font_name);
FcPatternAddString(pattern.get(), FC_FAMILY, fc_generic_font_name);
}
if (is_bold)
FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
FcPatternAddInteger(pattern.get(), FC_WEIGHT, FC_WEIGHT_BOLD);
if (is_italic)
FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
FcPatternAddLangSet(pattern, FC_LANG, langset);
FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
FcConfigSubstitute(nullptr, pattern, FcMatchPattern);
FcDefaultSubstitute(pattern);
FcPatternAddInteger(pattern.get(), FC_SLANT, FC_SLANT_ITALIC);
FcPatternAddLangSet(pattern.get(), FC_LANG, langset.get());
FcPatternAddBool(pattern.get(), FC_SCALABLE, FcTrue);
FcConfigSubstitute(nullptr, pattern.get(), FcMatchPattern);
FcDefaultSubstitute(pattern.get());
FcResult result;
FcFontSet* font_set = FcFontSort(nullptr, pattern, 0, nullptr, &result);
std::unique_ptr<FcFontSet, decltype(&FcFontSetDestroy)> font_set(
FcFontSort(nullptr, pattern.get(), 0, nullptr, &result),
&FcFontSetDestroy);
int font_fd = -1;
int good_enough_index = -1;
bool good_enough_index_set = false;
......@@ -259,10 +263,6 @@ int MatchFontFaceWithFallback(const std::string& face,
}
}
if (font_set)
FcFontSetDestroy(font_set);
FcPatternDestroy(pattern);
return font_fd;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment