Use SDL_calloc() instead of SDL_malloc()

This automatically initializes memory to zero so you don't have uninitialized memory bugs
This commit is contained in:
Sam Lantinga
2025-01-18 13:41:23 -08:00
parent 354d2c390c
commit 049a8f0e52
2 changed files with 8 additions and 16 deletions

View File

@@ -407,12 +407,11 @@ SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip)
gtk_thread_active = true; 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) { if (!tray) {
return NULL; return NULL;
} }
SDL_memset((void *) tray, 0, sizeof(*tray));
/* On success, g_mkdtemp edits its argument in-place to replace the Xs /* On success, g_mkdtemp edits its argument in-place to replace the Xs
* with a random directory name, which it creates safely and atomically. * with a random directory name, which it creates safely and atomically.
* On failure, it sets errno. */ * 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) 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) { if (!tray->menu) {
return NULL; return NULL;
} }
@@ -497,7 +496,7 @@ SDL_TrayMenu *SDL_CreateTraySubmenu(SDL_TrayEntry *entry)
return NULL; return NULL;
} }
entry->submenu = (SDL_TrayMenu *)SDL_malloc(sizeof(*entry->submenu)); entry->submenu = (SDL_TrayMenu *)SDL_calloc(1, sizeof(*entry->submenu));
if (!entry->submenu) { if (!entry->submenu) {
return NULL; return NULL;
} }
@@ -573,12 +572,11 @@ SDL_TrayEntry *SDL_InsertTrayEntryAt(SDL_TrayMenu *menu, int pos, const char *la
pos = menu->nEntries; pos = menu->nEntries;
} }
SDL_TrayEntry *entry = (SDL_TrayEntry *)SDL_malloc(sizeof(*entry)); SDL_TrayEntry *entry = (SDL_TrayEntry *)SDL_calloc(1, sizeof(*entry));
if (!entry) { if (!entry) {
return NULL; return NULL;
} }
SDL_memset((void *) entry, 0, sizeof(*entry));
entry->parent = menu; entry->parent = menu;
entry->item = NULL; entry->item = NULL;
entry->ignore_signal = false; entry->ignore_signal = false;

View File

@@ -211,7 +211,7 @@ static HICON load_default_icon()
SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip) 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) { if (!tray) {
return NULL; return NULL;
@@ -294,14 +294,12 @@ void SDL_SetTrayTooltip(SDL_Tray *tray, const char *tooltip)
SDL_TrayMenu *SDL_CreateTrayMenu(SDL_Tray *tray) 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) { if (!tray->menu) {
return NULL; return NULL;
} }
SDL_memset((void *) tray->menu, 0, sizeof(*tray->menu));
tray->menu->hMenu = CreatePopupMenu(); tray->menu->hMenu = CreatePopupMenu();
tray->menu->parent_tray = tray; tray->menu->parent_tray = tray;
tray->menu->parent_entry = NULL; 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; 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) { if (!entry) {
return NULL; return NULL;
} }
SDL_memset((void *) entry, 0, sizeof(*entry));
wchar_t *label_w = NULL; wchar_t *label_w = NULL;
if (label && (label_w = escape_label(label)) == 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 : ""); SDL_snprintf(entry->label_cache, sizeof(entry->label_cache), "%s", label ? label : "");
if (label != NULL && flags & SDL_TRAYENTRY_SUBMENU) { 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) { if (!entry->submenu) {
SDL_free(entry); SDL_free(entry);
SDL_free(label_w); SDL_free(label_w);
return NULL; return NULL;
} }
SDL_memset((void *) entry->submenu, 0, sizeof(*entry->submenu));
entry->submenu->hMenu = CreatePopupMenu(); entry->submenu->hMenu = CreatePopupMenu();
entry->submenu->nEntries = 0; entry->submenu->nEntries = 0;
entry->submenu->entries = NULL; entry->submenu->entries = NULL;