Blame SOURCES/0007-gwin32-Use-gsize-internally-in-g_wcsdup.patch

e5da31
From 9878d5eaeb18bc05131dee9a316f74e717626018 Mon Sep 17 00:00:00 2001
e5da31
From: Philip Withnall <pwithnall@endlessos.org>
e5da31
Date: Thu, 4 Feb 2021 13:50:37 +0000
e5da31
Subject: [PATCH 07/12] gwin32: Use gsize internally in g_wcsdup()
e5da31
MIME-Version: 1.0
e5da31
Content-Type: text/plain; charset=UTF-8
e5da31
Content-Transfer-Encoding: 8bit
e5da31
e5da31
This allows it to handle strings up to length `G_MAXSIZE` — previously
e5da31
it would overflow with such strings.
e5da31
e5da31
Update the several copies of it identically.
e5da31
e5da31
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
e5da31
Helps: #2319
e5da31
---
e5da31
 gio/gwin32registrykey.c | 34 ++++++++++++++++++++++++++--------
e5da31
 1 file changed, 26 insertions(+), 8 deletions(-)
e5da31
e5da31
diff --git a/gio/gwin32registrykey.c b/gio/gwin32registrykey.c
e5da31
index 619fd48af..fbd65311a 100644
e5da31
--- a/gio/gwin32registrykey.c
e5da31
+++ b/gio/gwin32registrykey.c
e5da31
@@ -127,16 +127,34 @@ typedef enum
e5da31
   G_WIN32_REGISTRY_UPDATED_PATH = 1,
e5da31
 } GWin32RegistryKeyUpdateFlag;
e5da31
 
e5da31
+static gsize
e5da31
+g_utf16_len (const gunichar2 *str)
e5da31
+{
e5da31
+  gsize result;
e5da31
+
e5da31
+  for (result = 0; str[0] != 0; str++, result++)
e5da31
+    ;
e5da31
+
e5da31
+  return result;
e5da31
+}
e5da31
+
e5da31
 static gunichar2 *
e5da31
-g_wcsdup (const gunichar2 *str,
e5da31
-          gssize           str_size)
e5da31
+g_wcsdup (const gunichar2 *str, gssize str_len)
e5da31
 {
e5da31
-  if (str_size == -1)
e5da31
-    {
e5da31
-      str_size = wcslen (str) + 1;
e5da31
-      str_size *= sizeof (gunichar2);
e5da31
-    }
e5da31
-  return g_memdup (str, str_size);
e5da31
+  gsize str_len_unsigned;
e5da31
+  gsize str_size;
e5da31
+
e5da31
+  g_return_val_if_fail (str != NULL, NULL);
e5da31
+
e5da31
+  if (str_len < 0)
e5da31
+    str_len_unsigned = g_utf16_len (str);
e5da31
+  else
e5da31
+    str_len_unsigned = (gsize) str_len;
e5da31
+
e5da31
+  g_assert (str_len_unsigned <= G_MAXSIZE / sizeof (gunichar2) - 1);
e5da31
+  str_size = (str_len_unsigned + 1) * sizeof (gunichar2);
e5da31
+
e5da31
+  return g_memdup2 (str, str_size);
e5da31
 }
e5da31
 
e5da31
 /**
e5da31
-- 
e5da31
2.31.1
e5da31