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) { ...@@ -40,7 +40,7 @@ std::string GetPostscriptNameFromFile(base::File& font_file) {
return ""; return "";
std::vector<char> file_contents; 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)); CHECK_EQ(file_size, font_file.Read(0, file_contents.data(), file_size));
std::string font_family_name; std::string font_family_name;
FT_Library library; FT_Library library;
......
...@@ -127,10 +127,12 @@ int MatchFontFaceWithFallback(const std::string& face, ...@@ -127,10 +127,12 @@ int MatchFontFaceWithFallback(const std::string& face,
bool is_italic, bool is_italic,
uint32_t charset, uint32_t charset,
uint32_t fallback_family) { uint32_t fallback_family) {
FcLangSet* langset = FcLangSetCreate(); std::unique_ptr<FcLangSet, decltype(&FcLangSetDestroy)> langset(
bool is_lgc = MSCharSetToFontconfig(langset, charset); FcLangSetCreate(), &FcLangSetDestroy);
FcPattern* pattern = FcPatternCreate(); bool is_lgc = MSCharSetToFontconfig(langset.get(), charset);
FcPatternAddString(pattern, FC_FAMILY, std::unique_ptr<FcPattern, decltype(&FcPatternDestroy)> pattern(
FcPatternCreate(), &FcPatternDestroy);
FcPatternAddString(pattern.get(), FC_FAMILY,
reinterpret_cast<const FcChar8*>(face.c_str())); reinterpret_cast<const FcChar8*>(face.c_str()));
// TODO(thestig) Check if we can access Chrome's per-script font preference // TODO(thestig) Check if we can access Chrome's per-script font preference
...@@ -152,20 +154,22 @@ int MatchFontFaceWithFallback(const std::string& face, ...@@ -152,20 +154,22 @@ int MatchFontFaceWithFallback(const std::string& face,
if (!generic_font_name.empty()) { if (!generic_font_name.empty()) {
const FcChar8* fc_generic_font_name = const FcChar8* fc_generic_font_name =
reinterpret_cast<const FcChar8*>(generic_font_name.c_str()); 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) if (is_bold)
FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD); FcPatternAddInteger(pattern.get(), FC_WEIGHT, FC_WEIGHT_BOLD);
if (is_italic) if (is_italic)
FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC); FcPatternAddInteger(pattern.get(), FC_SLANT, FC_SLANT_ITALIC);
FcPatternAddLangSet(pattern, FC_LANG, langset); FcPatternAddLangSet(pattern.get(), FC_LANG, langset.get());
FcPatternAddBool(pattern, FC_SCALABLE, FcTrue); FcPatternAddBool(pattern.get(), FC_SCALABLE, FcTrue);
FcConfigSubstitute(nullptr, pattern, FcMatchPattern); FcConfigSubstitute(nullptr, pattern.get(), FcMatchPattern);
FcDefaultSubstitute(pattern); FcDefaultSubstitute(pattern.get());
FcResult result; 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 font_fd = -1;
int good_enough_index = -1; int good_enough_index = -1;
bool good_enough_index_set = false; bool good_enough_index_set = false;
...@@ -259,10 +263,6 @@ int MatchFontFaceWithFallback(const std::string& face, ...@@ -259,10 +263,6 @@ int MatchFontFaceWithFallback(const std::string& face,
} }
} }
if (font_set)
FcFontSetDestroy(font_set);
FcPatternDestroy(pattern);
return font_fd; 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