SDL_net: Fixed signed/unsigned comparison warnings.

From cbe7b71f66dab850c7ce320733c8e32c75b5d15c Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Wed, 27 Sep 2023 12:13:51 -0400
Subject: [PATCH] Fixed signed/unsigned comparison warnings.

---
 examples/voipchat.c |  8 ++++----
 src/SDL_net.c       | 22 ++++++++++++++--------
 2 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/examples/voipchat.c b/examples/voipchat.c
index 0b8c552..439deec 100644
--- a/examples/voipchat.c
+++ b/examples/voipchat.c
@@ -85,14 +85,14 @@ static void ClearOldVoices(const Uint64 now)
     }
 }
 
-static const size_t extra = (sizeof (Uint64) * 2);
+static const int extra = (int) (sizeof (Uint64) * 2);
 static void SendClientAudioToServer(void)
 {
     const int br = SDL_GetAudioStreamData(capture_stream, scratch_area + extra, max_datagram - extra);
     if (br > 0) {
         ((Uint64 *) scratch_area)[0] = SDL_SwapLE64(0);  /* just being nice and leaving space in the buffer for the server to replace. */
         ((Uint64 *) scratch_area)[1] = SDL_SwapLE64(++next_idnum);
-        SDL_Log("CLIENT: Sending %d new bytes to server at %s:%d...", (int) (br + extra), SDLNet_GetAddressString(server_addr), (int) server_port);
+        SDL_Log("CLIENT: Sending %d new bytes to server at %s:%d...", br + extra, SDLNet_GetAddressString(server_addr), (int) server_port);
         SDLNet_SendDatagram(sock, server_addr, server_port, scratch_area, br + extra);
     }
 }
@@ -149,7 +149,7 @@ static void mainloop(void)
             } else {  /* we're the client. */
                 if ((dgram->port != server_port) || (SDLNet_CompareAddresses(dgram->addr, server_addr) != 0)) {
                     SDL_Log("CLIENT: Got packet from non-server address %s:%d. Ignoring.", SDLNet_GetAddressString(dgram->addr), (int) dgram->port);
-                } else if (dgram->buflen < (sizeof (Uint64) * 2)) {
+                } else if (dgram->buflen < extra) {
                     SDL_Log("CLIENT: Got bogus packet from the server. Ignoring.");
                 } else {
                     const Uint64 idnum = SDL_SwapLE64(((const Uint64 *) dgram->buf)[0]);
@@ -226,7 +226,7 @@ static void mainloop(void)
             if (!last_send_ticks || ((now - last_send_ticks) > 5000)) {  /* send a keepalive packet if we haven't transmitted for a bit. */
                 ((Uint64 *) scratch_area)[0] = SDL_SwapLE64(0);
                 ((Uint64 *) scratch_area)[1] = SDL_SwapLE64(++next_idnum);
-                SDL_Log("CLIENT: Sending %d keepalive bytes to server at %s:%d...", (int) extra, SDLNet_GetAddressString(server_addr), (int) server_port);
+                SDL_Log("CLIENT: Sending %d keepalive bytes to server at %s:%d...", extra, SDLNet_GetAddressString(server_addr), (int) server_port);
                 SDLNet_SendDatagram(sock, server_addr, server_port, scratch_area, extra);
                 last_send_ticks = now;
             }
diff --git a/src/SDL_net.c b/src/SDL_net.c
index 9d82042..f17c412 100644
--- a/src/SDL_net.c
+++ b/src/SDL_net.c
@@ -402,7 +402,7 @@ void SDLNet_Quit(void)
     if (resolver_lock && resolver_condition) {
         SDL_LockMutex(resolver_lock);
         SDL_AtomicSet(&resolver_shutdown, 1);
-        for (int i = 0; i < SDL_arraysize(resolver_threads); i++) {
+        for (int i = 0; i < ((int) SDL_arraysize(resolver_threads)); i++) {
             if (resolver_threads[i]) {
                 SDL_BroadcastCondition(resolver_condition);
                 SDL_UnlockMutex(resolver_lock);
@@ -464,7 +464,7 @@ SDLNet_Address *SDLNet_ResolveHostname(const char *host)
     //SDL_Log("num_threads=%d, num_requests=%d", num_threads, num_requests);
     if ((num_requests >= num_threads) && (num_threads < MAX_RESOLVER_THREADS)) {  // all threads are busy? Maybe spawn a new one.
         // if this didn't actually spin one up, it is what it is...the existing threads will eventually get there.
-        for (int i = 0; i < SDL_arraysize(resolver_threads); i++) {
+        for (int i = 0; i < ((int) SDL_arraysize(resolver_threads)); i++) {
             if (!resolver_threads[i]) {
                 SpinResolverThread(i);
                 break;
@@ -1094,11 +1094,14 @@ int SDLNet_WriteToStreamSocket(SDLNet_StreamSocket *sock, const void *buf, int b
     }
 
     // queue this up for sending later.
-    const size_t min_alloc = sock->pending_output_len + buflen;
+    const int min_alloc = sock->pending_output_len + buflen;
     if (min_alloc > sock->pending_output_allocation) {
-        size_t newlen = SDL_max(1, sock->pending_output_allocation);
+        int newlen = SDL_max(1, sock->pending_output_allocation);
         while (newlen < min_alloc) {
             newlen *= 2;
+            if (newlen < 0) {  // uhoh, overflowed! That's a lot of memory!!
+                return SDL_OutOfMemory();
+            }
         }
         void *ptr = SDL_realloc(sock->pending_output_buffer, newlen);
         if (!ptr) {
@@ -1362,11 +1365,14 @@ int SDLNet_SendDatagram(SDLNet_DatagramSocket *sock, SDLNet_Address *addr, Uint1
     }
 
     // queue this up for sending later.
-    const size_t min_alloc = sock->pending_output_len + 1;
+    const int min_alloc = sock->pending_output_len + 1;
     if (min_alloc > sock->pending_output_allocation) {
-        size_t newlen = SDL_max(1, sock->pending_output_allocation);
+        int newlen = SDL_max(1, sock->pending_output_allocation);
         while (newlen < min_alloc) {
             newlen *= 2;
+            if (newlen < 0) {  // uhoh, overflowed! That's a lot of memory!!
+                return SDL_OutOfMemory();
+            }
         }
         void *ptr = SDL_realloc(sock->pending_output, newlen * sizeof (SDLNet_Datagram *));
         if (!ptr) {
@@ -1512,7 +1518,7 @@ void SDLNet_DestroyDatagramSocket(SDLNet_DatagramSocket *sock)
         if (sock->handle != INVALID_SOCKET) {
             CloseSocketHandle(sock->handle);  // !!! FIXME: what does this do with non-blocking sockets? Release the descriptor but the kernel continues sending queued buffers behind the scenes?
         }
-        for (int i = 0; i < SDL_arraysize(sock->latest_recv_addrs); i++) {
+        for (int i = 0; i < ((int) SDL_arraysize(sock->latest_recv_addrs)); i++) {
             SDLNet_UnrefAddress(sock->latest_recv_addrs[i]);
         }
         for (int i = 0; i < sock->pending_output_len; i++) {
@@ -1546,7 +1552,7 @@ int SDLNet_WaitUntilInputAvailable(void **vsockets, int numsockets, int timeoutm
     struct pollfd *pfds = stack_pfds;
     struct pollfd *malloced_pfds = NULL;
 
-    if (numsockets > SDL_arraysize(stack_pfds)) {  // allocate if there's a _ton_ of these.
+    if (numsockets > ((int) SDL_arraysize(stack_pfds))) {  // allocate if there's a _ton_ of these.
         malloced_pfds = (struct pollfd *) SDL_malloc(numsockets * sizeof (*pfds));
         if (!malloced_pfds) {
             return SDL_OutOfMemory();