SDL: Switched nEntries to int, to avoid signed/unsigned comparison warnings

From 8a9b7ae8d179a6af80895d976b3ca4f3e9a5d7da Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 6 Jan 2025 11:29:28 -0800
Subject: [PATCH] Switched nEntries to int, to avoid signed/unsigned comparison
 warnings

Also standardized memory allocation calls to match SDL style
---
 src/tray/cocoa/SDL_tray.m   |  3 +--
 src/tray/unix/SDL_tray.c    | 23 +++++++++--------------
 src/tray/windows/SDL_tray.c | 37 ++++++++++++++++---------------------
 3 files changed, 26 insertions(+), 37 deletions(-)

diff --git a/src/tray/cocoa/SDL_tray.m b/src/tray/cocoa/SDL_tray.m
index 8cb0619314c5e..911285bcb3c68 100644
--- a/src/tray/cocoa/SDL_tray.m
+++ b/src/tray/cocoa/SDL_tray.m
@@ -281,8 +281,7 @@ void SDL_SetTrayTooltip(SDL_Tray *tray, const char *tooltip)
     if (size) {
         *size = menu->nEntries;
     }
-
-    return (const SDL_TrayEntry **) menu->entries;
+    return menu->entries;
 }
 
 void SDL_RemoveTrayEntry(SDL_TrayEntry *entry)
diff --git a/src/tray/unix/SDL_tray.c b/src/tray/unix/SDL_tray.c
index e4f0208f4ab57..afa06ac8e64eb 100644
--- a/src/tray/unix/SDL_tray.c
+++ b/src/tray/unix/SDL_tray.c
@@ -292,7 +292,7 @@ static bool init_gtk(void)
 struct SDL_TrayMenu {
     GtkMenuShell *menu;
 
-    size_t nEntries;
+    int nEntries;
     SDL_TrayEntry **entries;
 
     SDL_Tray *parent_tray;
@@ -390,8 +390,7 @@ SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip)
         gtk_thread_active = true;
     }
 
-    SDL_Tray *tray = (SDL_Tray *) SDL_malloc(sizeof(SDL_Tray));
-
+    SDL_Tray *tray = (SDL_Tray *)SDL_malloc(sizeof(*tray));
     if (!tray) {
         return NULL;
     }
@@ -436,8 +435,7 @@ void SDL_SetTrayTooltip(SDL_Tray *tray, const char *tooltip)
 
 SDL_TrayMenu *SDL_CreateTrayMenu(SDL_Tray *tray)
 {
-    tray->menu = SDL_malloc(sizeof(SDL_TrayMenu));
-
+    tray->menu = (SDL_TrayMenu *)SDL_malloc(sizeof(*tray->menu));
     if (!tray->menu) {
         return NULL;
     }
@@ -470,8 +468,7 @@ SDL_TrayMenu *SDL_CreateTraySubmenu(SDL_TrayEntry *entry)
         return NULL;
     }
 
-    entry->submenu = SDL_malloc(sizeof(SDL_TrayMenu));
-
+    entry->submenu = (SDL_TrayMenu *)SDL_malloc(sizeof(*entry->submenu));
     if (!entry->submenu) {
         return NULL;
     }
@@ -497,8 +494,7 @@ const SDL_TrayEntry **SDL_GetTrayEntries(SDL_TrayMenu *menu, int *size)
     if (size) {
         *size = menu->nEntries;
     }
-
-    return (const SDL_TrayEntry **) menu->entries;
+    return menu->entries;
 }
 
 void SDL_RemoveTrayEntry(SDL_TrayEntry *entry)
@@ -525,7 +521,7 @@ void SDL_RemoveTrayEntry(SDL_TrayEntry *entry)
     }
 
     menu->nEntries--;
-    SDL_TrayEntry ** new_entries = SDL_realloc(menu->entries, (menu->nEntries + 1) * sizeof(SDL_TrayEntry *));
+    SDL_TrayEntry **new_entries = (SDL_TrayEntry **)SDL_realloc(menu->entries, (menu->nEntries + 1) * sizeof(*new_entries));
 
     /* Not sure why shrinking would fail, but even if it does, we can live with a "too big" array */
     if (new_entries) {
@@ -539,7 +535,7 @@ void SDL_RemoveTrayEntry(SDL_TrayEntry *entry)
 
 SDL_TrayEntry *SDL_InsertTrayEntryAt(SDL_TrayMenu *menu, int pos, const char *label, SDL_TrayEntryFlags flags)
 {
-    if (pos < -1 || pos > (int) menu->nEntries) {
+    if (pos < -1 || pos > menu->nEntries) {
         SDL_InvalidParamError("pos");
         return NULL;
     }
@@ -548,8 +544,7 @@ SDL_TrayEntry *SDL_InsertTrayEntryAt(SDL_TrayMenu *menu, int pos, const char *la
         pos = menu->nEntries;
     }
 
-    SDL_TrayEntry *entry = SDL_malloc(sizeof(SDL_TrayEntry));
-
+    SDL_TrayEntry *entry = (SDL_TrayEntry *)SDL_malloc(sizeof(*entry));
     if (!entry) {
         return NULL;
     }
@@ -574,7 +569,7 @@ SDL_TrayEntry *SDL_InsertTrayEntryAt(SDL_TrayMenu *menu, int pos, const char *la
 
     gtk_widget_set_sensitive(entry->item, !(flags & SDL_TRAYENTRY_DISABLED));
 
-    SDL_TrayEntry **new_entries = (SDL_TrayEntry **) SDL_realloc(menu->entries, (menu->nEntries + 2) * sizeof(SDL_TrayEntry *));
+    SDL_TrayEntry **new_entries = (SDL_TrayEntry **)SDL_realloc(menu->entries, (menu->nEntries + 2) * sizeof(*new_entries));
 
     if (!new_entries) {
         SDL_free(entry);
diff --git a/src/tray/windows/SDL_tray.c b/src/tray/windows/SDL_tray.c
index 5b123e343d14e..e68660dd067d4 100644
--- a/src/tray/windows/SDL_tray.c
+++ b/src/tray/windows/SDL_tray.c
@@ -42,7 +42,7 @@
 struct SDL_TrayMenu {
     HMENU hMenu;
 
-    size_t nEntries;
+    int nEntries;
     SDL_TrayEntry **entries;
 
     SDL_Tray *parent_tray;
@@ -75,7 +75,7 @@ static UINT_PTR get_next_id(void)
 
 static SDL_TrayEntry *find_entry_in_menu(SDL_TrayMenu *menu, UINT_PTR id)
 {
-    for (size_t i = 0; i < menu->nEntries; i++) {
+    for (int i = 0; i < menu->nEntries; i++) {
         SDL_TrayEntry *entry = menu->entries[i];
 
         if (entry->id == id) {
@@ -145,7 +145,7 @@ LRESULT CALLBACK TrayWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lPar
 
 static void DestroySDLMenu(SDL_TrayMenu *menu)
 {
-    for (size_t i = 0; i < menu->nEntries; i++) {
+    for (int i = 0; i < menu->nEntries; i++) {
         if (menu->entries[i] && menu->entries[i]->submenu) {
             DestroySDLMenu(menu->entries[i]->submenu);
         }
@@ -166,8 +166,7 @@ static wchar_t *escape_label(const char *in)
         len += (*c == '&') ? 2 : 1;
     }
 
-    char *escaped = SDL_malloc(SDL_strlen(in) + len + 1);
-
+    char *escaped = (char *)SDL_malloc(SDL_strlen(in) + len + 1);
     if (!escaped) {
         return NULL;
     }
@@ -190,7 +189,7 @@ static wchar_t *escape_label(const char *in)
 
 SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip)
 {
-    SDL_Tray *tray = SDL_malloc(sizeof(SDL_Tray));
+    SDL_Tray *tray = (SDL_Tray *)SDL_malloc(sizeof(*tray));
 
     if (!tray) {
         return NULL;
@@ -273,7 +272,7 @@ void SDL_SetTrayTooltip(SDL_Tray *tray, const char *tooltip)
 
 SDL_TrayMenu *SDL_CreateTrayMenu(SDL_Tray *tray)
 {
-    tray->menu = SDL_malloc(sizeof(SDL_TrayMenu));
+    tray->menu = (SDL_TrayMenu *)SDL_malloc(sizeof(*tray->menu));
 
     if (!tray->menu) {
         return NULL;
@@ -309,10 +308,9 @@ SDL_TrayMenu *SDL_GetTraySubmenu(SDL_TrayEntry *entry)
 const SDL_TrayEntry **SDL_GetTrayEntries(SDL_TrayMenu *menu, int *size)
 {
     if (size) {
-        *size = (int) menu->nEntries;
+        *size = menu->nEntries;
     }
-
-    return (const SDL_TrayEntry **) menu->entries;
+    return menu->entries;
 }
 
 void SDL_RemoveTrayEntry(SDL_TrayEntry *entry)
@@ -324,7 +322,7 @@ void SDL_RemoveTrayEntry(SDL_TrayEntry *entry)
     SDL_TrayMenu *menu = entry->parent;
 
     bool found = false;
-    for (size_t i = 0; i < menu->nEntries - 1; i++) {
+    for (int i = 0; i < menu->nEntries - 1; i++) {
         if (menu->entries[i] == entry) {
             found = true;
         }
@@ -339,7 +337,7 @@ void SDL_RemoveTrayEntry(SDL_TrayEntry *entry)
     }
 
     menu->nEntries--;
-    SDL_TrayEntry ** new_entries = SDL_realloc(menu->entries, (menu->nEntries + 1) * sizeof(SDL_TrayEntry *));
+    SDL_TrayEntry **new_entries = (SDL_TrayEntry **)SDL_realloc(menu->entries, (menu->nEntries + 1) * sizeof(*new_entries));
 
     /* Not sure why shrinking would fail, but even if it does, we can live with a "too big" array */
     if (new_entries) {
@@ -357,7 +355,7 @@ void SDL_RemoveTrayEntry(SDL_TrayEntry *entry)
 
 SDL_TrayEntry *SDL_InsertTrayEntryAt(SDL_TrayMenu *menu, int pos, const char *label, SDL_TrayEntryFlags flags)
 {
-    if (pos < -1 || pos > (int) menu->nEntries) {
+    if (pos < -1 || pos > menu->nEntries) {
         SDL_InvalidParamError("pos");
         return NULL;
     }
@@ -365,13 +363,12 @@ SDL_TrayEntry *SDL_InsertTrayEntryAt(SDL_TrayMenu *menu, int pos, const char *la
     int windows_compatible_pos = pos;
 
     if (pos == -1) {
-        pos = (int) menu->nEntries;
+        pos = menu->nEntries;
     } else if (pos == menu->nEntries) {
         windows_compatible_pos = -1;
     }
 
-    SDL_TrayEntry *entry = SDL_malloc(sizeof(SDL_TrayEntry));
-
+    SDL_TrayEntry *entry = (SDL_TrayEntry *)SDL_malloc(sizeof(*entry));
     if (!entry) {
         return NULL;
     }
@@ -391,8 +388,7 @@ 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_malloc(sizeof(SDL_TrayMenu));
-
+        entry->submenu = (SDL_TrayMenu *)SDL_malloc(sizeof(*entry->submenu));
         if (!entry->submenu) {
             SDL_free(entry);
             SDL_free(label_w);
@@ -408,8 +404,7 @@ SDL_TrayEntry *SDL_InsertTrayEntryAt(SDL_TrayMenu *menu, int pos, const char *la
         entry->id = get_next_id();
     }
 
-    SDL_TrayEntry **new_entries = (SDL_TrayEntry **) SDL_realloc(menu->entries, (menu->nEntries + 2) * sizeof(SDL_TrayEntry **));
-
+    SDL_TrayEntry **new_entries = (SDL_TrayEntry **)SDL_realloc(menu->entries, (menu->nEntries + 2) * sizeof(*new_entries));
     if (!new_entries) {
         SDL_free(entry);
         SDL_free(label_w);
@@ -423,7 +418,7 @@ SDL_TrayEntry *SDL_InsertTrayEntryAt(SDL_TrayMenu *menu, int pos, const char *la
     menu->entries = new_entries;
     menu->nEntries++;
 
-    for (int i = (int) menu->nEntries - 1; i > pos; i--) {
+    for (int i = menu->nEntries - 1; i > pos; i--) {
         menu->entries[i] = menu->entries[i - 1];
     }