Skip to content

Commit

Permalink
8313164: src/java.desktop/windows/native/libawt/windows/awt_Robot.cpp…
Browse files Browse the repository at this point in the history
… GetRGBPixels adjust releasing of resources

Reviewed-by: stuefe
  • Loading branch information
MBaesken committed Jul 27, 2023
1 parent 36d578c commit b7545a6
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/java.desktop/windows/native/libawt/windows/awt_Robot.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -170,6 +170,8 @@ static void GetRGBPixels(jint x, jint y, jint width, jint height, jintArray pixe
// create an offscreen bitmap
hbitmap = ::CreateCompatibleBitmap(hdcScreen, width, height);
if (hbitmap == NULL) {
::DeleteDC(hdcMem);
::DeleteDC(hdcScreen);
throw std::bad_alloc();
}
hOldBitmap = (HBITMAP)::SelectObject(hdcMem, hbitmap);
Expand All @@ -189,9 +191,21 @@ static void GetRGBPixels(jint x, jint y, jint width, jint height, jintArray pixe
static const int BITS_PER_PIXEL = 32;
static const int BYTES_PER_PIXEL = BITS_PER_PIXEL/8;

if (!IS_SAFE_SIZE_MUL(width, height)) throw std::bad_alloc();
if (!IS_SAFE_SIZE_MUL(width, height)) {
::DeleteObject(hbitmap);
::DeleteDC(hdcMem);
::DeleteDC(hdcScreen);
throw std::bad_alloc();
}

int numPixels = width*height;
if (!IS_SAFE_SIZE_MUL(BYTES_PER_PIXEL, numPixels)) throw std::bad_alloc();
if (!IS_SAFE_SIZE_MUL(BYTES_PER_PIXEL, numPixels)) {
::DeleteObject(hbitmap);
::DeleteDC(hdcMem);
::DeleteDC(hdcScreen);
throw std::bad_alloc();
}

int pixelDataSize = BYTES_PER_PIXEL*numPixels;
DASSERT(pixelDataSize > 0 && pixelDataSize % 4 == 0);
// allocate memory for BITMAPINFO + pixel data
Expand All @@ -202,6 +216,9 @@ static void GetRGBPixels(jint x, jint y, jint width, jint height, jintArray pixe
// See MSDN docs for BITMAPINFOHEADER -bchristi

if (!IS_SAFE_SIZE_ADD(sizeof(BITMAPINFOHEADER) + 3 * sizeof(RGBQUAD), pixelDataSize)) {
::DeleteObject(hbitmap);
::DeleteDC(hdcMem);
::DeleteDC(hdcScreen);
throw std::bad_alloc();
}
BITMAPINFO * pinfo = (BITMAPINFO *)(new BYTE[sizeof(BITMAPINFOHEADER) + 3 * sizeof(RGBQUAD) + pixelDataSize]);
Expand Down

7 comments on commit b7545a6

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@MBaesken
Copy link
Member Author

Choose a reason for hiding this comment

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

/backport jdk21u

@openjdk
Copy link

@openjdk openjdk bot commented on b7545a6 Sep 20, 2023

Choose a reason for hiding this comment

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

@MBaesken the backport was successfully created on the branch MBaesken-backport-b7545a69 in my personal fork of openjdk/jdk21u. To create a pull request with this backport targeting openjdk/jdk21u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit b7545a69 from the openjdk/jdk repository.

The commit being backported was authored by Matthias Baesken on 27 Jul 2023 and was reviewed by Thomas Stuefe.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk21u:

$ git fetch https://github.com/openjdk-bots/jdk21u.git MBaesken-backport-b7545a69:MBaesken-backport-b7545a69
$ git checkout MBaesken-backport-b7545a69
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk21u.git MBaesken-backport-b7545a69

@MBaesken
Copy link
Member Author

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on b7545a6 Oct 26, 2023

Choose a reason for hiding this comment

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

@MBaesken the backport was successfully created on the branch MBaesken-backport-b7545a69 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit b7545a69 from the openjdk/jdk repository.

The commit being backported was authored by Matthias Baesken on 27 Jul 2023 and was reviewed by Thomas Stuefe.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev.git MBaesken-backport-b7545a69:MBaesken-backport-b7545a69
$ git checkout MBaesken-backport-b7545a69
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git MBaesken-backport-b7545a69

@MBaesken
Copy link
Member Author

Choose a reason for hiding this comment

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

/backport jdk11u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on b7545a6 Dec 1, 2023

Choose a reason for hiding this comment

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

@MBaesken the backport was successfully created on the branch MBaesken-backport-b7545a69 in my personal fork of openjdk/jdk11u-dev. To create a pull request with this backport targeting openjdk/jdk11u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit b7545a69 from the openjdk/jdk repository.

The commit being backported was authored by Matthias Baesken on 27 Jul 2023 and was reviewed by Thomas Stuefe.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk11u-dev:

$ git fetch https://github.com/openjdk-bots/jdk11u-dev.git MBaesken-backport-b7545a69:MBaesken-backport-b7545a69
$ git checkout MBaesken-backport-b7545a69
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk11u-dev.git MBaesken-backport-b7545a69

Please sign in to comment.