Skip to content

Commit

Permalink
8282862: AwtWindow::SetIconData leaks old icon handles if an exceptio…
Browse files Browse the repository at this point in the history
…n is detected

Reviewed-by: aivanov, dmarkov, prr, honkar, azvegint
  • Loading branch information
rajamah committed Jan 23, 2025
1 parent 356e2a8 commit 48ece07
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions src/java.desktop/windows/native/libawt/windows/awt_Window.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2025, 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 @@ -2110,20 +2110,49 @@ HICON CreateIconFromRaster(JNIEnv* env, jintArray iconRaster, jint w, jint h)
void AwtWindow::SetIconData(JNIEnv* env, jintArray iconRaster, jint w, jint h,
jintArray smallIconRaster, jint smw, jint smh)
{
HICON hNewIcon = NULL;
HICON hNewIconSm = NULL;

try {
hNewIcon = CreateIconFromRaster(env, iconRaster, w, h);
if (env->ExceptionCheck()) {
if (hNewIcon != NULL) {
DestroyIcon(hNewIcon);
}
return;
}

hNewIconSm = CreateIconFromRaster(env, smallIconRaster, smw, smh);
if (env->ExceptionCheck()) {
if (hNewIcon != NULL) {
DestroyIcon(hNewIcon);
}
if (hNewIconSm != NULL) {
DestroyIcon(hNewIconSm);
}
return;
}
} catch (...) {
if (hNewIcon != NULL) {
DestroyIcon(hNewIcon);
}
if (hNewIconSm != NULL) {
DestroyIcon(hNewIconSm);
}
return;
}

HICON hOldIcon = NULL;
HICON hOldIconSm = NULL;
//Destroy previous icon if it isn't inherited
if ((m_hIcon != NULL) && !m_iconInherited) {
hOldIcon = m_hIcon;
}
m_hIcon = NULL;
if ((m_hIconSm != NULL) && !m_iconInherited) {
hOldIconSm = m_hIconSm;
}
m_hIconSm = NULL;
m_hIcon = CreateIconFromRaster(env, iconRaster, w, h);
JNU_CHECK_EXCEPTION(env);
m_hIconSm = CreateIconFromRaster(env, smallIconRaster, smw, smh);

m_hIcon = hNewIcon;
m_hIconSm = hNewIconSm;

m_iconInherited = (m_hIcon == NULL);
if (m_iconInherited) {
Expand All @@ -2136,8 +2165,11 @@ void AwtWindow::SetIconData(JNIEnv* env, jintArray iconRaster, jint w, jint h,
m_iconInherited = FALSE;
}
}

DoUpdateIcon();
EnumThreadWindows(AwtToolkit::MainThread(), UpdateOwnedIconCallback, (LPARAM)this);

// Destroy previous icons if they were not inherited
if (hOldIcon != NULL) {
DestroyIcon(hOldIcon);
}
Expand Down

1 comment on commit 48ece07

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