Skip to content

Commit 48ece07

Browse files
committed
8282862: AwtWindow::SetIconData leaks old icon handles if an exception is detected
Reviewed-by: aivanov, dmarkov, prr, honkar, azvegint
1 parent 356e2a8 commit 48ece07

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

src/java.desktop/windows/native/libawt/windows/awt_Window.cpp

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2025, 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
@@ -2110,20 +2110,49 @@ HICON CreateIconFromRaster(JNIEnv* env, jintArray iconRaster, jint w, jint h)
21102110
void AwtWindow::SetIconData(JNIEnv* env, jintArray iconRaster, jint w, jint h,
21112111
jintArray smallIconRaster, jint smw, jint smh)
21122112
{
2113+
HICON hNewIcon = NULL;
2114+
HICON hNewIconSm = NULL;
2115+
2116+
try {
2117+
hNewIcon = CreateIconFromRaster(env, iconRaster, w, h);
2118+
if (env->ExceptionCheck()) {
2119+
if (hNewIcon != NULL) {
2120+
DestroyIcon(hNewIcon);
2121+
}
2122+
return;
2123+
}
2124+
2125+
hNewIconSm = CreateIconFromRaster(env, smallIconRaster, smw, smh);
2126+
if (env->ExceptionCheck()) {
2127+
if (hNewIcon != NULL) {
2128+
DestroyIcon(hNewIcon);
2129+
}
2130+
if (hNewIconSm != NULL) {
2131+
DestroyIcon(hNewIconSm);
2132+
}
2133+
return;
2134+
}
2135+
} catch (...) {
2136+
if (hNewIcon != NULL) {
2137+
DestroyIcon(hNewIcon);
2138+
}
2139+
if (hNewIconSm != NULL) {
2140+
DestroyIcon(hNewIconSm);
2141+
}
2142+
return;
2143+
}
2144+
21132145
HICON hOldIcon = NULL;
21142146
HICON hOldIconSm = NULL;
2115-
//Destroy previous icon if it isn't inherited
21162147
if ((m_hIcon != NULL) && !m_iconInherited) {
21172148
hOldIcon = m_hIcon;
21182149
}
2119-
m_hIcon = NULL;
21202150
if ((m_hIconSm != NULL) && !m_iconInherited) {
21212151
hOldIconSm = m_hIconSm;
21222152
}
2123-
m_hIconSm = NULL;
2124-
m_hIcon = CreateIconFromRaster(env, iconRaster, w, h);
2125-
JNU_CHECK_EXCEPTION(env);
2126-
m_hIconSm = CreateIconFromRaster(env, smallIconRaster, smw, smh);
2153+
2154+
m_hIcon = hNewIcon;
2155+
m_hIconSm = hNewIconSm;
21272156

21282157
m_iconInherited = (m_hIcon == NULL);
21292158
if (m_iconInherited) {
@@ -2136,8 +2165,11 @@ void AwtWindow::SetIconData(JNIEnv* env, jintArray iconRaster, jint w, jint h,
21362165
m_iconInherited = FALSE;
21372166
}
21382167
}
2168+
21392169
DoUpdateIcon();
21402170
EnumThreadWindows(AwtToolkit::MainThread(), UpdateOwnedIconCallback, (LPARAM)this);
2171+
2172+
// Destroy previous icons if they were not inherited
21412173
if (hOldIcon != NULL) {
21422174
DestroyIcon(hOldIcon);
21432175
}

0 commit comments

Comments
 (0)