-
Notifications
You must be signed in to change notification settings - Fork 5.8k
8282628: Potential memory leak in sun.font.FontConfigManager.getFontConfig() #7691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
👋 Welcome back zgu! A progress list of the required criteria for merging this PR into |
@zhengyu123 The following labels will be automatically applied to this pull request:
When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command. |
Webrevs
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
@zhengyu123 This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 60 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. ➡️ To integrate this PR with the above commit message to the |
/reviewers 3 |
@prrace |
I've increased the number of reviewers because as well as the current reviewer it should have If there were no change to that file it would have been simpler. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have just inlined JNU_CHECK_EXCEPTION and added the cleanup code directly. The additional macro wasn't really necessary and would not really be usable for any kind of general cleanup actions beyond a one-liner.
But it is okay.
Thanks.
Thanks for the review, @dholmes-ora . The macros are used to hide the different syntax of c and c++ to avoid polluting code here, it seems to be a common pattern in client code. |
That's to allow for general use by C or C++ source files. But you are only needing this in a C source file so you would only need the C variant of the calling syntax. |
@@ -935,7 +935,7 @@ Java_sun_font_FontConfigManager_getFontConfig | |||
if (cacheDirs != NULL) { | |||
while ((cnt < max) && (cacheDir = (*FcStrListNext)(cacheDirs))) { | |||
jstr = (*env)->NewStringUTF(env, (const char*)cacheDir); | |||
JNU_CHECK_EXCEPTION(env); | |||
JNU_CHECK_EXCEPTION_AND_CLEANUP(env, (*FcStrListDone)(cacheDirs)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you do not need to create an additional macro here, just inline it and call "(*FcStrListDone)(cacheDirs);" directly. Something like:
if (IS_NULL(jstr) {
(*FcStrListDone)(cacheDirs);
return;
}
Note that the "IS_NULL" is used in this file after NewStringUTF. Any objections?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Elsewhere in this file IS_NULL is always used to check NewStringUTF, so the existing code was inconsistent in checking for a pending exception.
Checking for NULL and checking for a pending exception are logically equivalent as long as the passed in pointer is not NULL.
@@ -935,7 +935,7 @@ Java_sun_font_FontConfigManager_getFontConfig | |||
if (cacheDirs != NULL) { | |||
while ((cnt < max) && (cacheDir = (*FcStrListNext)(cacheDirs))) { | |||
jstr = (*env)->NewStringUTF(env, (const char*)cacheDir); | |||
JNU_CHECK_EXCEPTION(env); | |||
JNU_CHECK_EXCEPTION_AND_CLEANUP(env, (*FcStrListDone)(cacheDirs)); | |||
|
|||
(*env)->SetObjectArrayElement(env, cacheDirArray, cnt++, jstr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably we should add the check+cleanup after the SetObjectArrayElement? Otherwise, we may call NewStringUTF while an exception is raised by the SetObjectArrayElement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
??? You want to check and cleanup if NewStringUTF fails. You only want to call SetObjectElementArray if NewStringUTF succeeds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can SetObjectElementArray
raise an exception?
The index is within the array length and we store a string. I assume cacheDirArray
is a string array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
??? You want to check and cleanup if NewStringUTF fails. You only want to call SetObjectElementArray if NewStringUTF succeeds.
If the SetObjectArrayElement will throw an exception we will call the NewStringUTF while the exception is raised which is a kind of "jni" issue. If such an exception is possible we should check and cleanup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A quick search for SetObjectArrayElement
in java.desktop
module shows the call to SetObjectArrayElement
is rarely followed by exception check.
What kind of exception can SetObjectArrayElement
raise if the index is within the range and the stored element is of correct type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think NewStringUTF()
can throw OOM and also return NULL
, just which one you pick.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
NewStringUTF()
can throw OOM and also returnNULL
, just which one you pick.
Yes.
But we're discussing whether a check for exception is necessary after (*env)->SetObjectArrayElement
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SetObjectArrayElement()
may throw ArrayIndexOutOfBoundsException
and ArrayStoreException
, but I don't see it is possible here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's exactly my point. Therefore the check for exception after SetObjectArrayElement
is unnecessary.
I don't like the macros. I can argue all I did a quick grep, there are a few suspicious spots, e.g. AwtWindow::SetIconData(), I have yet take a close look. |
It doesn't leak the icon handle here, it's assigned to the member of the object (probably |
I am not familiar with this code. What I see is that, it assigns |
You're right. The old icon handles in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still looks good to me.
I've submitted JDK-8282862: AwtWindow::SetIconData leaks old icon handles if an exception is detected |
/label remove core-libs |
@AlanBateman |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current version seems fine.
Thanks,
David
Thanks for the review and suggestions, @tstuefe, @dholmes-ora , @mrserb, @aivanov-jdk |
/integrate |
Going to push as commit 5df2a05.
Your commit was automatically rebased without conflicts. |
@zhengyu123 Pushed as commit 5df2a05. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
Thanks for the confirmation. |
Please review this small patch that fixes a potential memory leak that exception return fails to release allocated
cacheDirs
Test:
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/7691/head:pull/7691
$ git checkout pull/7691
Update a local copy of the PR:
$ git checkout pull/7691
$ git pull https://git.openjdk.java.net/jdk pull/7691/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 7691
View PR using the GUI difftool:
$ git pr show -t 7691
Using diff file
Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/7691.diff