From 049a8f0e524c0e426ac5e778888b279005299358 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 18 Jan 2025 13:41:23 -0800
Subject: [PATCH] Use SDL_calloc() instead of SDL_malloc()
This automatically initializes memory to zero so you don't have uninitialized memory bugs
---
src/tray/unix/SDL_tray.c | 10 ++++------
src/tray/windows/SDL_tray.c | 14 ++++----------
2 files changed, 8 insertions(+), 16 deletions(-)
diff --git a/src/tray/unix/SDL_tray.c b/src/tray/unix/SDL_tray.c
index 39deab4e5db64..e1bd27c095a02 100644
--- a/src/tray/unix/SDL_tray.c
+++ b/src/tray/unix/SDL_tray.c
@@ -407,12 +407,11 @@ SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip)
gtk_thread_active = true;
}
- SDL_Tray *tray = (SDL_Tray *)SDL_malloc(sizeof(*tray));
+ SDL_Tray *tray = (SDL_Tray *)SDL_calloc(1, sizeof(*tray));
if (!tray) {
return NULL;
}
- SDL_memset((void *) tray, 0, sizeof(*tray));
/* On success, g_mkdtemp edits its argument in-place to replace the Xs
* with a random directory name, which it creates safely and atomically.
* On failure, it sets errno. */
@@ -464,7 +463,7 @@ void SDL_SetTrayTooltip(SDL_Tray *tray, const char *tooltip)
SDL_TrayMenu *SDL_CreateTrayMenu(SDL_Tray *tray)
{
- tray->menu = (SDL_TrayMenu *)SDL_malloc(sizeof(*tray->menu));
+ tray->menu = (SDL_TrayMenu *)SDL_calloc(1, sizeof(*tray->menu));
if (!tray->menu) {
return NULL;
}
@@ -497,7 +496,7 @@ SDL_TrayMenu *SDL_CreateTraySubmenu(SDL_TrayEntry *entry)
return NULL;
}
- entry->submenu = (SDL_TrayMenu *)SDL_malloc(sizeof(*entry->submenu));
+ entry->submenu = (SDL_TrayMenu *)SDL_calloc(1, sizeof(*entry->submenu));
if (!entry->submenu) {
return NULL;
}
@@ -573,12 +572,11 @@ SDL_TrayEntry *SDL_InsertTrayEntryAt(SDL_TrayMenu *menu, int pos, const char *la
pos = menu->nEntries;
}
- SDL_TrayEntry *entry = (SDL_TrayEntry *)SDL_malloc(sizeof(*entry));
+ SDL_TrayEntry *entry = (SDL_TrayEntry *)SDL_calloc(1, sizeof(*entry));
if (!entry) {
return NULL;
}
- SDL_memset((void *) entry, 0, sizeof(*entry));
entry->parent = menu;
entry->item = NULL;
entry->ignore_signal = false;
diff --git a/src/tray/windows/SDL_tray.c b/src/tray/windows/SDL_tray.c
index 256f3436e5e27..fe072eb017c0f 100644
--- a/src/tray/windows/SDL_tray.c
+++ b/src/tray/windows/SDL_tray.c
@@ -211,7 +211,7 @@ static HICON load_default_icon()
SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip)
{
- SDL_Tray *tray = (SDL_Tray *)SDL_malloc(sizeof(*tray));
+ SDL_Tray *tray = (SDL_Tray *)SDL_calloc(1, sizeof(*tray));
if (!tray) {
return NULL;
@@ -294,14 +294,12 @@ void SDL_SetTrayTooltip(SDL_Tray *tray, const char *tooltip)
SDL_TrayMenu *SDL_CreateTrayMenu(SDL_Tray *tray)
{
- tray->menu = (SDL_TrayMenu *)SDL_malloc(sizeof(*tray->menu));
+ tray->menu = (SDL_TrayMenu *)SDL_calloc(1, sizeof(*tray->menu));
if (!tray->menu) {
return NULL;
}
- SDL_memset((void *) tray->menu, 0, sizeof(*tray->menu));
-
tray->menu->hMenu = CreatePopupMenu();
tray->menu->parent_tray = tray;
tray->menu->parent_entry = NULL;
@@ -391,13 +389,11 @@ SDL_TrayEntry *SDL_InsertTrayEntryAt(SDL_TrayMenu *menu, int pos, const char *la
windows_compatible_pos = -1;
}
- SDL_TrayEntry *entry = (SDL_TrayEntry *)SDL_malloc(sizeof(*entry));
+ SDL_TrayEntry *entry = (SDL_TrayEntry *)SDL_calloc(1, sizeof(*entry));
if (!entry) {
return NULL;
}
- SDL_memset((void *) entry, 0, sizeof(*entry));
-
wchar_t *label_w = NULL;
if (label && (label_w = escape_label(label)) == NULL) {
@@ -413,15 +409,13 @@ SDL_TrayEntry *SDL_InsertTrayEntryAt(SDL_TrayMenu *menu, int pos, const char *la
SDL_snprintf(entry->label_cache, sizeof(entry->label_cache), "%s", label ? label : "");
if (label != NULL && flags & SDL_TRAYENTRY_SUBMENU) {
- entry->submenu = (SDL_TrayMenu *)SDL_malloc(sizeof(*entry->submenu));
+ entry->submenu = (SDL_TrayMenu *)SDL_calloc(1, sizeof(*entry->submenu));
if (!entry->submenu) {
SDL_free(entry);
SDL_free(label_w);
return NULL;
}
- SDL_memset((void *) entry->submenu, 0, sizeof(*entry->submenu));
-
entry->submenu->hMenu = CreatePopupMenu();
entry->submenu->nEntries = 0;
entry->submenu->entries = NULL;