From a51316890218c89fa3350178cbe2c79146a01d2e Mon Sep 17 00:00:00 2001
From: ImThour <[EMAIL REDACTED]>
Date: Mon, 17 Feb 2025 21:00:30 +0530
Subject: [PATCH] Fixed Memory/Resource Leaks (#12304)
---
src/video/windows/SDL_windowsmouse.c | 44 ++++++++++++++++++++--------
1 file changed, 31 insertions(+), 13 deletions(-)
diff --git a/src/video/windows/SDL_windowsmouse.c b/src/video/windows/SDL_windowsmouse.c
index 983ef1e1e428b..e27c4c1857ae5 100644
--- a/src/video/windows/SDL_windowsmouse.c
+++ b/src/video/windows/SDL_windowsmouse.c
@@ -72,18 +72,22 @@ static SDL_Cursor *WIN_CreateCursorAndData(HCURSOR hcursor)
}
SDL_Cursor *cursor = (SDL_Cursor *)SDL_calloc(1, sizeof(*cursor));
- if (cursor) {
- SDL_CursorData *data = (SDL_CursorData *)SDL_calloc(1, sizeof(*data));
- if (!data) {
- SDL_free(cursor);
- return NULL;
- }
- data->cursor = hcursor;
- cursor->internal = data;
+ if (!cursor) {
+ return NULL;
+ }
+
+ SDL_CursorData *data = (SDL_CursorData *)SDL_calloc(1, sizeof(*data));
+ if (!data) {
+ SDL_free(cursor);
+ return NULL;
}
+
+ data->cursor = hcursor;
+ cursor->internal = data;
return cursor;
}
+
static bool IsMonochromeSurface(SDL_Surface *surface)
{
int x, y;
@@ -129,6 +133,9 @@ static HBITMAP CreateColorBitmap(SDL_Surface *surface)
bitmap = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, &pixels, NULL, 0);
if (!bitmap || !pixels) {
WIN_SetError("CreateDIBSection()");
+ if (bitmap) {
+ DeleteObject(bitmap);
+ }
return NULL;
}
@@ -158,6 +165,7 @@ static HBITMAP CreateMaskBitmap(SDL_Surface *surface, bool is_monochrome)
pixels = SDL_small_alloc(Uint8, size * (is_monochrome ? 2 : 1), &isstack);
if (!pixels) {
+ SDL_OutOfMemory();
return NULL;
}
@@ -220,16 +228,20 @@ static HCURSOR WIN_CreateHCursor(SDL_Surface *surface, int hot_x, int hot_y)
}
hcursor = CreateIconIndirect(&ii);
+ if (!hcursor) {
+ WIN_SetError("CreateIconIndirect()");
+ DeleteObject(ii.hbmMask);
+ if (ii.hbmColor) {
+ DeleteObject(ii.hbmColor);
+ }
+ return NULL;
+ }
DeleteObject(ii.hbmMask);
if (ii.hbmColor) {
DeleteObject(ii.hbmColor);
}
- if (!hcursor) {
- WIN_SetError("CreateIconIndirect()");
- return NULL;
- }
return hcursor;
}
@@ -359,7 +371,9 @@ static void WIN_FreeCursor(SDL_Cursor *cursor)
while (data->cache) {
CachedCursor *entry = data->cache;
data->cache = entry->next;
- DestroyCursor(entry->cursor);
+ if (entry->cursor) {
+ DestroyCursor(entry->cursor);
+ }
SDL_free(entry);
}
if (data->cursor) {
@@ -404,6 +418,10 @@ static HCURSOR GetCachedCursor(SDL_Cursor *cursor)
entry = (CachedCursor *)SDL_malloc(sizeof(*entry));
if (!entry) {
+ if (hcursor) {
+ DestroyCursor(hcursor);
+ }
+ SDL_free(entry);
goto error;
}
entry->cursor = hcursor;