Blame SOURCES/0009-gsocket-Use-gsize-to-track-native-sockaddr-s-size.patch

e5da31
From 4d5c5d6af772f5fe6121eec403305a1b4340327d Mon Sep 17 00:00:00 2001
e5da31
From: Philip Withnall <pwithnall@endlessos.org>
e5da31
Date: Thu, 4 Feb 2021 14:00:53 +0000
e5da31
Subject: [PATCH 09/12] =?UTF-8?q?gsocket:=20Use=20gsize=20to=20track=20nat?=
e5da31
 =?UTF-8?q?ive=20sockaddr=E2=80=99s=20size?=
e5da31
MIME-Version: 1.0
e5da31
Content-Type: text/plain; charset=UTF-8
e5da31
Content-Transfer-Encoding: 8bit
e5da31
e5da31
Don’t use an `int`, that’s potentially too small. In practical terms,
e5da31
this is not a problem, since no socket address is going to be that big.
e5da31
e5da31
By making these changes we can use `g_memdup2()` without warnings,
e5da31
though. Fewer warnings is good.
e5da31
e5da31
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
e5da31
Helps: #2319
e5da31
---
e5da31
 gio/gsocket.c | 17 +++++++++++------
e5da31
 1 file changed, 11 insertions(+), 6 deletions(-)
e5da31
e5da31
diff --git a/gio/gsocket.c b/gio/gsocket.c
e5da31
index b4a941eb1..7f41ffd3c 100644
e5da31
--- a/gio/gsocket.c
e5da31
+++ b/gio/gsocket.c
e5da31
@@ -80,6 +80,8 @@
e5da31
 #include "gwin32networking.h"
e5da31
 #endif
e5da31
 
e5da31
+#include "gstrfuncsprivate.h"
e5da31
+
e5da31
 /**
e5da31
  * SECTION:gsocket
e5da31
  * @short_description: Low-level socket object
e5da31
@@ -173,7 +175,7 @@ static gboolean     g_socket_datagram_based_condition_wait       (GDatagramBased
e5da31
                                                                   GError          **error);
e5da31
 
e5da31
 static GSocketAddress *
e5da31
-cache_recv_address (GSocket *socket, struct sockaddr *native, int native_len);
e5da31
+cache_recv_address (GSocket *socket, struct sockaddr *native, size_t native_len);
e5da31
 
e5da31
 static gssize
e5da31
 g_socket_receive_message_with_timeout  (GSocket                 *socket,
e5da31
@@ -270,7 +272,7 @@ struct _GSocketPrivate
e5da31
   struct {
e5da31
     GSocketAddress *addr;
e5da31
     struct sockaddr *native;
e5da31
-    gint native_len;
e5da31
+    gsize native_len;
e5da31
     guint64 last_used;
e5da31
   } recv_addr_cache[RECV_ADDR_CACHE_SIZE];
e5da31
 };
e5da31
@@ -5018,14 +5020,14 @@ g_socket_send_messages_with_timeout (GSocket        *socket,
e5da31
 }
e5da31
 
e5da31
 static GSocketAddress *
e5da31
-cache_recv_address (GSocket *socket, struct sockaddr *native, int native_len)
e5da31
+cache_recv_address (GSocket *socket, struct sockaddr *native, size_t native_len)
e5da31
 {
e5da31
   GSocketAddress *saddr;
e5da31
   gint i;
e5da31
   guint64 oldest_time = G_MAXUINT64;
e5da31
   gint oldest_index = 0;
e5da31
 
e5da31
-  if (native_len <= 0)
e5da31
+  if (native_len == 0)
e5da31
     return NULL;
e5da31
 
e5da31
   saddr = NULL;
e5da31
@@ -5033,7 +5035,7 @@ cache_recv_address (GSocket *socket, struct sockaddr *native, int native_len)
e5da31
     {
e5da31
       GSocketAddress *tmp = socket->priv->recv_addr_cache[i].addr;
e5da31
       gpointer tmp_native = socket->priv->recv_addr_cache[i].native;
e5da31
-      gint tmp_native_len = socket->priv->recv_addr_cache[i].native_len;
e5da31
+      gsize tmp_native_len = socket->priv->recv_addr_cache[i].native_len;
e5da31
 
e5da31
       if (!tmp)
e5da31
         continue;
e5da31
@@ -5063,7 +5065,7 @@ cache_recv_address (GSocket *socket, struct sockaddr *native, int native_len)
e5da31
       g_free (socket->priv->recv_addr_cache[oldest_index].native);
e5da31
     }
e5da31
 
e5da31
-  socket->priv->recv_addr_cache[oldest_index].native = g_memdup (native, native_len);
e5da31
+  socket->priv->recv_addr_cache[oldest_index].native = g_memdup2 (native, native_len);
e5da31
   socket->priv->recv_addr_cache[oldest_index].native_len = native_len;
e5da31
   socket->priv->recv_addr_cache[oldest_index].addr = g_object_ref (saddr);
e5da31
   socket->priv->recv_addr_cache[oldest_index].last_used = g_get_monotonic_time ();
e5da31
@@ -5213,6 +5215,9 @@ g_socket_receive_message_with_timeout (GSocket                 *socket,
e5da31
       {
e5da31
         win32_unset_event_mask (socket, FD_READ);
e5da31
 
e5da31
+        /* addrlen has to be of type int because that’s how WSARecvFrom() is defined */
e5da31
+        G_STATIC_ASSERT (sizeof addr <= G_MAXINT);
e5da31
+
e5da31
 	addrlen = sizeof addr;
e5da31
 	if (address)
e5da31
 	  result = WSARecvFrom (socket->priv->fd,
e5da31
-- 
e5da31
2.31.1
e5da31