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

Backport-of: b7545a69a27f255cbf26071be5b88f6e3e6b3cd6
  • Loading branch information
MBaesken committed Dec 4, 2023
1 parent 229ddde commit 1df80ba
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

1 comment on commit 1df80ba

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