summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Withnall <pwithnall@endlessos.org>2021-02-04 13:50:37 +0000
committerPhilip Withnall <pwithnall@endlessos.org>2021-02-04 17:51:01 +0000
commit2aaf593a9eb96d84fe3be740aca2810a97d95592 (patch)
treeec95e8c13f34664dcbae37c137505f1e80588ebd
parentf9ee2275cbc312c0b4cdbc338a4fbb76eb36fb9a (diff)
downloadglib-2aaf593a9eb96d84fe3be740aca2810a97d95592.tar.gz
gwin32: Use gsize internally in g_wcsdup()
This allows it to handle strings up to length `G_MAXSIZE` — previously it would overflow with such strings. Update the several copies of it identically. Signed-off-by: Philip Withnall <pwithnall@endlessos.org> Helps: #2319
-rw-r--r--gio/giowin32-private.c20
-rw-r--r--gio/gwin32registrykey.c34
2 files changed, 38 insertions, 16 deletions
diff --git a/gio/giowin32-private.c b/gio/giowin32-private.c
index 7120ae0ea..47e840805 100644
--- a/gio/giowin32-private.c
+++ b/gio/giowin32-private.c
@@ -16,11 +16,12 @@
* along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
+#include "gstrfuncsprivate.h"
-static gssize
+static gsize
g_utf16_len (const gunichar2 *str)
{
- gssize result;
+ gsize result;
for (result = 0; str[0] != 0; str++, result++)
;
@@ -31,17 +32,20 @@ g_utf16_len (const gunichar2 *str)
static gunichar2 *
g_wcsdup (const gunichar2 *str, gssize str_len)
{
- gssize str_size;
+ gsize str_len_unsigned;
+ gsize str_size;
g_return_val_if_fail (str != NULL, NULL);
- if (str_len == -1)
- str_len = g_utf16_len (str);
+ if (str_len < 0)
+ str_len_unsigned = g_utf16_len (str);
+ else
+ str_len_unsigned = (gsize) str_len;
- g_assert (str_len <= G_MAXSIZE / sizeof (gunichar2) - 1);
- str_size = (str_len + 1) * sizeof (gunichar2);
+ g_assert (str_len_unsigned <= G_MAXSIZE / sizeof (gunichar2) - 1);
+ str_size = (str_len_unsigned + 1) * sizeof (gunichar2);
- return g_memdup (str, str_size);
+ return g_memdup2 (str, str_size);
}
static const gunichar2 *
diff --git a/gio/gwin32registrykey.c b/gio/gwin32registrykey.c
index 548a94188..2eb67daf8 100644
--- a/gio/gwin32registrykey.c
+++ b/gio/gwin32registrykey.c
@@ -127,16 +127,34 @@ typedef enum
G_WIN32_REGISTRY_UPDATED_PATH = 1,
} GWin32RegistryKeyUpdateFlag;
+static gsize
+g_utf16_len (const gunichar2 *str)
+{
+ gsize result;
+
+ for (result = 0; str[0] != 0; str++, result++)
+ ;
+
+ return result;
+}
+
static gunichar2 *
-g_wcsdup (const gunichar2 *str,
- gssize str_size)
+g_wcsdup (const gunichar2 *str, gssize str_len)
{
- if (str_size == -1)
- {
- str_size = wcslen (str) + 1;
- str_size *= sizeof (gunichar2);
- }
- return g_memdup (str, str_size);
+ gsize str_len_unsigned;
+ gsize str_size;
+
+ g_return_val_if_fail (str != NULL, NULL);
+
+ if (str_len < 0)
+ str_len_unsigned = g_utf16_len (str);
+ else
+ str_len_unsigned = (gsize) str_len;
+
+ g_assert (str_len_unsigned <= G_MAXSIZE / sizeof (gunichar2) - 1);
+ str_size = (str_len_unsigned + 1) * sizeof (gunichar2);
+
+ return g_memdup2 (str, str_size);
}
/**