Skip to content

Commit

Permalink
8284680: sun.font.FontConfigManager.getFontConfig() leaks charset
Browse files Browse the repository at this point in the history
Backport-of: dea6e886af20457597dd02dacf1e80273a7165a7
  • Loading branch information
zhengyu123 committed Jun 28, 2022
1 parent ab7d1ca commit 027302b
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/java.desktop/unix/native/common/awt/fontpath.c
Expand Up @@ -739,6 +739,7 @@ typedef FcFontSet* (*FcFontSortFuncType)(FcConfig *config,
FcResult *result);
typedef FcCharSet* (*FcCharSetUnionFuncType)(const FcCharSet *a,
const FcCharSet *b);
typedef FcCharSet* (*FcCharSetDestroyFuncType)(FcCharSet *fcs);
typedef FcChar32 (*FcCharSetSubtractCountFuncType)(const FcCharSet *a,
const FcCharSet *b);

Expand Down Expand Up @@ -1024,6 +1025,7 @@ Java_sun_font_FontConfigManager_getFontConfig
FcFontSortFuncType FcFontSort;
FcFontSetDestroyFuncType FcFontSetDestroy;
FcCharSetUnionFuncType FcCharSetUnion;
FcCharSetDestroyFuncType FcCharSetDestroy;
FcCharSetSubtractCountFuncType FcCharSetSubtractCount;
FcGetVersionFuncType FcGetVersion;
FcConfigGetCacheDirsFuncType FcConfigGetCacheDirs;
Expand Down Expand Up @@ -1103,6 +1105,8 @@ Java_sun_font_FontConfigManager_getFontConfig
(FcFontSetDestroyFuncType)dlsym(libfontconfig, "FcFontSetDestroy");
FcCharSetUnion =
(FcCharSetUnionFuncType)dlsym(libfontconfig, "FcCharSetUnion");
FcCharSetDestroy =
(FcCharSetDestroyFuncType)dlsym(libfontconfig, "FcCharSetDestroy");
FcCharSetSubtractCount =
(FcCharSetSubtractCountFuncType)dlsym(libfontconfig,
"FcCharSetSubtractCount");
Expand All @@ -1118,6 +1122,7 @@ Java_sun_font_FontConfigManager_getFontConfig
FcPatternGetCharSet == NULL ||
FcFontSetDestroy == NULL ||
FcCharSetUnion == NULL ||
FcCharSetDestroy == NULL ||
FcGetVersion == NULL ||
FcCharSetSubtractCount == NULL) {/* problem with the library: return.*/
closeFontConfig(libfontconfig, JNI_FALSE);
Expand Down Expand Up @@ -1181,6 +1186,7 @@ Java_sun_font_FontConfigManager_getFontConfig
FcChar8 **family, **styleStr, **fullname, **file;
jarray fcFontArr = NULL;
FcCharSet *unionCharset = NULL;
FcCharSet *prevUnionCharset = NULL;

fcCompFontObj = (*env)->GetObjectArrayElement(env, fcCompFontArray, i);
fcNameStr =
Expand Down Expand Up @@ -1294,6 +1300,9 @@ Java_sun_font_FontConfigManager_getFontConfig
free(file);
(*FcPatternDestroy)(pattern);
(*FcFontSetDestroy)(fontset);
if (prevUnionCharset != NULL) {
(*FcCharSetDestroy)(prevUnionCharset);
}
closeFontConfig(libfontconfig, JNI_FALSE);
if (locale) {
(*env)->ReleaseStringUTFChars(env, localeStr, (const char*)locale);
Expand All @@ -1315,6 +1324,10 @@ Java_sun_font_FontConfigManager_getFontConfig
if ((*FcCharSetSubtractCount)(charset, unionCharset)
> minGlyphs) {
unionCharset = (* FcCharSetUnion)(unionCharset, charset);
if (prevUnionCharset != NULL) {
(*FcCharSetDestroy)(prevUnionCharset);
}
prevUnionCharset = unionCharset;
} else {
continue;
}
Expand All @@ -1333,6 +1346,11 @@ Java_sun_font_FontConfigManager_getFontConfig
}
}

// Release last instance of CharSet union
if (prevUnionCharset != NULL) {
(*FcCharSetDestroy)(prevUnionCharset);
}

/* Once we get here 'fontCount' is the number of returned fonts
* we actually want to use, so we create 'fcFontArr' of that length.
* The non-null entries of "family[]" etc are those fonts.
Expand Down

1 comment on commit 027302b

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.