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
8277299: STACK_OVERFLOW in Java_sun_awt_shell_Win32ShellFolder2_getIconBits #6473
Conversation
…onBits Made colorBits and maskBits arraus dynamic so they allocated on heap instead of stack. Added regression test.
👋 Welcome back kizune! A progress list of the required criteria for merging this PR into |
@azuev-java The following label 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 list. If you would like to change these labels, use the /label pull request command. |
Webrevs
|
@@ -1056,7 +1056,8 @@ JNIEXPORT jintArray JNICALL Java_sun_awt_shell_Win32ShellFolder2_getIconBits | |||
bmi.bmiHeader.biCompression = BI_RGB; | |||
// Extract the color bitmap | |||
int nBits = iconSize * iconSize; | |||
long colorBits[MAX_ICON_SIZE * MAX_ICON_SIZE]; | |||
long * colorBits; | |||
colorBits = (long*)safe_Malloc(MAX_ICON_SIZE * MAX_ICON_SIZE * sizeof(long)); |
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 am not sure that the bad_alloc will be properly handled in this Java_sun_awt_shell_Win32ShellFolder2_getIconBits method.
+Probably it will be better to merge assigning into one line.
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 am not sure that the bad_alloc will be properly handled in this Java_sun_awt_shell_Win32ShellFolder2_getIconBits method.
I can't see any try-catch.
Is it better to use malloc
and check for NULL
?
Probably it will be better to merge assigning into one line.
I agree. The line doesn't get too long.
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 TRY/CATCH_BAD_ALLOC_RET(NULL); could be used. It depends on how we will clean the stuff on exception.
@@ -1056,7 +1056,8 @@ JNIEXPORT jintArray JNICALL Java_sun_awt_shell_Win32ShellFolder2_getIconBits | |||
bmi.bmiHeader.biCompression = BI_RGB; | |||
// Extract the color bitmap | |||
int nBits = iconSize * iconSize; | |||
long colorBits[MAX_ICON_SIZE * MAX_ICON_SIZE]; | |||
long * colorBits; |
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.
Pointer declarations aren't consistent in the file, the function parameter is declared as JNIEnv* env
. However, in the majority of cases the asterisk is near the variable name as in const char *pLibName
. In either case, there's only one space:
long *maskBits;
or
long* maskBits;
@@ -1056,7 +1056,8 @@ JNIEXPORT jintArray JNICALL Java_sun_awt_shell_Win32ShellFolder2_getIconBits | |||
bmi.bmiHeader.biCompression = BI_RGB; | |||
// Extract the color bitmap | |||
int nBits = iconSize * iconSize; | |||
long colorBits[MAX_ICON_SIZE * MAX_ICON_SIZE]; | |||
long * colorBits; | |||
colorBits = (long*)safe_Malloc(MAX_ICON_SIZE * MAX_ICON_SIZE * sizeof(long)); |
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 am not sure that the bad_alloc will be properly handled in this Java_sun_awt_shell_Win32ShellFolder2_getIconBits method.
I can't see any try-catch.
Is it better to use malloc
and check for NULL
?
Probably it will be better to merge assigning into one line.
I agree. The line doesn't get too long.
* @test | ||
* @bug 8277299 | ||
* @requires (os.family == "windows") | ||
* @summary STACK_OVERFLOW in Java_sun_awt_shell_Win32ShellFolder2_getIconBits |
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.
Maybe this could be spelt with regular case rather than caps?
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.
Maybe this could be spelt with regular case rather than caps?
Well, it is just copy-paste from the bug synopsis which is copy-paste from the error message of the VM crash.
Fixed insets Declaration and assignment are now joined
free(colorBits); | ||
} | ||
|
||
CATCH_BAD_ALLOC_RET(NULL); |
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 believe we leak dc
as well as iconInfo.hbmColor
and iconInfo.hbmMask
if std::bad_alloc
is thrown.
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.
Fixed. Now we will release these resources on exit in case of bad_alloc.
try { | ||
entry_point(); | ||
colorBits = (long*)safe_Malloc(MAX_ICON_SIZE * MAX_ICON_SIZE * sizeof(long)); | ||
} catch(std::bad_alloc&) { | ||
handle_bad_alloc(); | ||
} | ||
|
||
if (colorBits != NULL) { |
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 this way defeats the use of exception to handle allocation error. You could use:
long *colorBits = (long*)malloc(MAX_ICON_SIZE * MAX_ICON_SIZE * sizeof(long));
if (colorBits != NULL) {
to achieve the same effect, which is shorter and clearer.
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.
Yes, but then handle_bad_alloc() will not be called which will not raise the OutOfMemoryError on the java side.
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.
Yes, but then handle_bad_alloc() will not be called which will not raise the OutOfMemoryError on the java side.
Can't we call it explicitly in the case where colorBits == NULL
?
Alternatively, since you don't use the macros for try and catch, you can re-arrange code and perform cleanup after the catch-block. Just an idea… It may not work.
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't we call it explicitly in the case where colorBits == NULL?
I would use the default handlers for bad_alloc since they can be changed in feature and keeping code that is intended to be used in two places is usually not a good idea.
Alternatively, since you don't use the macros for try and catch, you can re-arrange code and perform cleanup after the catch-block. Just an idea… It may not work.
That might require a lot of code reformatting and the benefits are unclear.
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.
Alternatively, since you don't use the macros for try and catch, you can re-arrange code and perform cleanup after the catch-block. Just an idea… It may not work.
That might require a lot of code reformatting and the benefits are unclear.
I tried it, it doesn't, see this diff. It's close to your latest code, however, there's only one catch
that handles both allocations and there are no if
's inside the try
-block.
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.
What about this approach?
Compared to master
Compared to your latest code
I think it's cleaner in the use of exceptions. Each try-block allocates memory that it uses and frees it before the catch-block. There's no need for if's: if exception is thrown, the memory wasn't allocated, therefore the memory is freed only if no exception is thrown.
What do you think?
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 like the singular try/catch block approach better so i did that in the latest commit.
// Release DC | ||
ReleaseDC(NULL, dc); |
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.
The DC has to be released even if colorBits
allocation failed, so this needs to be after if (colorBits != NULL)
.
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.
Fixed.
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, you hasn't pushed the change for releasing the DC.
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.
Indeed.
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 admit I prefer the 2nd version with two try-catch blocks better because it avoids using if's completely.
Anyway, this version looks good to me. Thanks.
// Extract the mask bitmap | ||
|
||
maskBits = (long*)safe_Malloc(MAX_ICON_SIZE * MAX_ICON_SIZE * sizeof(long)); | ||
|
||
GetDIBits(dc, iconInfo.hbmMask, 0, iconSize, maskBits, &bmi, DIB_RGB_COLORS); |
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 the empty lines here aren't necessary.
@azuev-java 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 345 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 |
Made colorBits and maskBits arrays dynamic so they are allocated on heap instead of stack.
Added regression test.
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/6473/head:pull/6473
$ git checkout pull/6473
Update a local copy of the PR:
$ git checkout pull/6473
$ git pull https://git.openjdk.java.net/jdk pull/6473/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 6473
View PR using the GUI difftool:
$ git pr show -t 6473
Using diff file
Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/6473.diff