Skip to content

Commit

Permalink
8284680: sun.font.FontConfigManager.getFontConfig() leaks charset
Browse files Browse the repository at this point in the history
Reviewed-by: prr, andrew
  • Loading branch information
zhengyu123 committed May 12, 2022
1 parent 40f43c6 commit dea6e88
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
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,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 @@ -808,6 +809,7 @@ Java_sun_font_FontConfigManager_getFontConfig
FcFontSortFuncType FcFontSort;
FcFontSetDestroyFuncType FcFontSetDestroy;
FcCharSetUnionFuncType FcCharSetUnion;
FcCharSetDestroyFuncType FcCharSetDestroy;
FcCharSetSubtractCountFuncType FcCharSetSubtractCount;
FcGetVersionFuncType FcGetVersion;
FcConfigGetCacheDirsFuncType FcConfigGetCacheDirs;
Expand Down Expand Up @@ -887,6 +889,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 @@ -902,6 +906,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 @@ -967,6 +972,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 @@ -1080,6 +1086,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 @@ -1101,6 +1110,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 @@ -1119,6 +1132,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 dea6e88

@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.