11/*
2- * Copyright (c) 1998, 2019 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 1998, 2023 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
@@ -170,6 +170,8 @@ static void GetRGBPixels(jint x, jint y, jint width, jint height, jintArray pixe
170170 // create an offscreen bitmap
171171 hbitmap = ::CreateCompatibleBitmap (hdcScreen, width, height);
172172 if (hbitmap == NULL ) {
173+ ::DeleteDC (hdcMem);
174+ ::DeleteDC (hdcScreen);
173175 throw std::bad_alloc ();
174176 }
175177 hOldBitmap = (HBITMAP)::SelectObject (hdcMem, hbitmap);
@@ -189,9 +191,21 @@ static void GetRGBPixels(jint x, jint y, jint width, jint height, jintArray pixe
189191 static const int BITS_PER_PIXEL = 32 ;
190192 static const int BYTES_PER_PIXEL = BITS_PER_PIXEL/8 ;
191193
192- if (!IS_SAFE_SIZE_MUL (width, height)) throw std::bad_alloc ();
194+ if (!IS_SAFE_SIZE_MUL (width, height)) {
195+ ::DeleteObject (hbitmap);
196+ ::DeleteDC (hdcMem);
197+ ::DeleteDC (hdcScreen);
198+ throw std::bad_alloc ();
199+ }
200+
193201 int numPixels = width*height;
194- if (!IS_SAFE_SIZE_MUL (BYTES_PER_PIXEL, numPixels)) throw std::bad_alloc ();
202+ if (!IS_SAFE_SIZE_MUL (BYTES_PER_PIXEL, numPixels)) {
203+ ::DeleteObject (hbitmap);
204+ ::DeleteDC (hdcMem);
205+ ::DeleteDC (hdcScreen);
206+ throw std::bad_alloc ();
207+ }
208+
195209 int pixelDataSize = BYTES_PER_PIXEL*numPixels;
196210 DASSERT (pixelDataSize > 0 && pixelDataSize % 4 == 0 );
197211 // allocate memory for BITMAPINFO + pixel data
@@ -202,6 +216,9 @@ static void GetRGBPixels(jint x, jint y, jint width, jint height, jintArray pixe
202216 // See MSDN docs for BITMAPINFOHEADER -bchristi
203217
204218 if (!IS_SAFE_SIZE_ADD (sizeof (BITMAPINFOHEADER) + 3 * sizeof (RGBQUAD), pixelDataSize)) {
219+ ::DeleteObject (hbitmap);
220+ ::DeleteDC (hdcMem);
221+ ::DeleteDC (hdcScreen);
205222 throw std::bad_alloc ();
206223 }
207224 BITMAPINFO * pinfo = (BITMAPINFO *)(new BYTE[sizeof (BITMAPINFOHEADER) + 3 * sizeof (RGBQUAD) + pixelDataSize]);
0 commit comments