File glib2-CVE-2021-27219-add-g_memdup2.patch of Package glib2.23780
diff --unified --recursive --text --new-file --color glib-2.62.6.old/docs/reference/glib/meson.build glib-2.62.6.new/docs/reference/glib/meson.build
--- glib-2.62.6.old/docs/reference/glib/meson.build 2021-03-03 08:49:45.483161256 +0800
+++ glib-2.62.6.new/docs/reference/glib/meson.build 2021-03-03 16:30:35.557438575 +0800
@@ -22,6 +22,7 @@
'gprintfint.h',
'gmirroringtable.h',
'gscripttable.h',
+ 'gstrfuncsprivate.h',
'glib-mirroring-tab',
'gnulib',
'pcre',
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gio/gdatainputstream.c glib-2.62.6.new/gio/gdatainputstream.c
--- glib-2.62.6.old/gio/gdatainputstream.c 2021-03-03 08:49:45.486494589 +0800
+++ glib-2.62.6.new/gio/gdatainputstream.c 2021-03-03 16:35:12.417797856 +0800
@@ -27,6 +27,7 @@
#include "gioenumtypes.h"
#include "gioerror.h"
#include "glibintl.h"
+#include "gstrfuncsprivate.h"
#include <string.h>
@@ -856,7 +857,7 @@
scan_for_chars (GDataInputStream *stream,
gsize *checked_out,
const char *stop_chars,
- gssize stop_chars_len)
+ gsize stop_chars_len)
{
GBufferedInputStream *bstream;
const char *buffer;
@@ -952,7 +953,7 @@
gsize checked;
gchar *stop_chars;
- gssize stop_chars_len;
+ gsize stop_chars_len;
gsize length;
} GDataInputStreamReadData;
@@ -1078,12 +1079,17 @@
{
GDataInputStreamReadData *data;
GTask *task;
+ gsize stop_chars_len_unsigned;
data = g_slice_new0 (GDataInputStreamReadData);
- if (stop_chars_len == -1)
- stop_chars_len = strlen (stop_chars);
- data->stop_chars = g_memdup (stop_chars, stop_chars_len);
- data->stop_chars_len = stop_chars_len;
+
+ if (stop_chars_len < 0)
+ stop_chars_len_unsigned = strlen (stop_chars);
+ else
+ stop_chars_len_unsigned = (gsize) stop_chars_len;
+
+ data->stop_chars = g_memdup2 (stop_chars, stop_chars_len_unsigned);
+ data->stop_chars_len = stop_chars_len_unsigned;
data->last_saw_cr = FALSE;
task = g_task_new (stream, cancellable, callback, user_data);
@@ -1338,17 +1344,20 @@
gssize found_pos;
gssize res;
char *data_until;
+ gsize stop_chars_len_unsigned;
g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
if (stop_chars_len < 0)
- stop_chars_len = strlen (stop_chars);
+ stop_chars_len_unsigned = strlen (stop_chars);
+ else
+ stop_chars_len_unsigned = (gsize) stop_chars_len;
bstream = G_BUFFERED_INPUT_STREAM (stream);
checked = 0;
- while ((found_pos = scan_for_chars (stream, &checked, stop_chars, stop_chars_len)) == -1)
+ while ((found_pos = scan_for_chars (stream, &checked, stop_chars, stop_chars_len_unsigned)) == -1)
{
if (g_buffered_input_stream_get_available (bstream) ==
g_buffered_input_stream_get_buffer_size (bstream))
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gio/gdbusconnection.c glib-2.62.6.new/gio/gdbusconnection.c
--- glib-2.62.6.old/gio/gdbusconnection.c 2021-03-03 08:49:45.486494589 +0800
+++ glib-2.62.6.new/gio/gdbusconnection.c 2021-03-03 16:30:43.814003313 +0800
@@ -110,6 +110,7 @@
#include "gasyncinitable.h"
#include "giostream.h"
#include "gasyncresult.h"
+#include "gstrfuncsprivate.h"
#include "gtask.h"
#include "gmarshal-internal.h"
@@ -3997,7 +3998,7 @@
/* Don't waste memory by copying padding - remember to update this
* when changing struct _GDBusInterfaceVTable in gdbusconnection.h
*/
- return g_memdup ((gconstpointer) vtable, 3 * sizeof (gpointer));
+ return g_memdup2 ((gconstpointer) vtable, 3 * sizeof (gpointer));
}
static void
@@ -4014,7 +4015,7 @@
/* Don't waste memory by copying padding - remember to update this
* when changing struct _GDBusSubtreeVTable in gdbusconnection.h
*/
- return g_memdup ((gconstpointer) vtable, 3 * sizeof (gpointer));
+ return g_memdup2 ((gconstpointer) vtable, 3 * sizeof (gpointer));
}
static void
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gio/gdbusinterfaceskeleton.c glib-2.62.6.new/gio/gdbusinterfaceskeleton.c
--- glib-2.62.6.old/gio/gdbusinterfaceskeleton.c 2021-03-03 08:49:45.486494589 +0800
+++ glib-2.62.6.new/gio/gdbusinterfaceskeleton.c 2021-03-03 16:30:43.814003313 +0800
@@ -28,6 +28,7 @@
#include "gdbusmethodinvocation.h"
#include "gdbusconnection.h"
#include "gmarshal-internal.h"
+#include "gstrfuncsprivate.h"
#include "gtask.h"
#include "gioerror.h"
@@ -701,7 +702,7 @@
* properly before building the hooked_vtable, so we create it
* once at the last minute.
*/
- interface_->priv->hooked_vtable = g_memdup (g_dbus_interface_skeleton_get_vtable (interface_), sizeof (GDBusInterfaceVTable));
+ interface_->priv->hooked_vtable = g_memdup2 (g_dbus_interface_skeleton_get_vtable (interface_), sizeof (GDBusInterfaceVTable));
interface_->priv->hooked_vtable->method_call = skeleton_intercept_handle_method_call;
}
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gio/gfile.c glib-2.62.6.new/gio/gfile.c
--- glib-2.62.6.old/gio/gfile.c 2021-03-03 08:49:45.486494589 +0800
+++ glib-2.62.6.new/gio/gfile.c 2021-03-03 16:30:43.814003313 +0800
@@ -60,6 +60,7 @@
#include "gasyncresult.h"
#include "gioerror.h"
#include "glibintl.h"
+#include "gstrfuncsprivate.h"
/**
@@ -7884,7 +7885,7 @@
g_main_context_invoke_full (g_task_get_context (task),
g_task_get_priority (task),
measure_disk_usage_invoke_progress,
- g_memdup (&progress, sizeof progress),
+ g_memdup2 (&progress, sizeof progress),
g_free);
}
@@ -7902,7 +7903,7 @@
data->progress_callback ? measure_disk_usage_progress : NULL, task,
&result.disk_usage, &result.num_dirs, &result.num_files,
&error))
- g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
+ g_task_return_pointer (task, g_memdup2 (&result, sizeof result), g_free);
else
g_task_return_error (task, error);
}
@@ -7926,7 +7927,7 @@
task = g_task_new (file, cancellable, callback, user_data);
g_task_set_source_tag (task, g_file_real_measure_disk_usage_async);
- g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
+ g_task_set_task_data (task, g_memdup2 (&data, sizeof data), g_free);
g_task_set_priority (task, io_priority);
g_task_run_in_thread (task, measure_disk_usage_thread);
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gio/gkeyfilesettingsbackend.c glib-2.62.6.new/gio/gkeyfilesettingsbackend.c
--- glib-2.62.6.old/gio/gkeyfilesettingsbackend.c 2021-03-03 08:49:45.489827922 +0800
+++ glib-2.62.6.new/gio/gkeyfilesettingsbackend.c 2021-03-03 16:41:10.538208337 +0800
@@ -33,6 +33,7 @@
#include "gfilemonitor.h"
#include "gsimplepermission.h"
#include "gsettingsbackendinternal.h"
+#include "gstrfuncsprivate.h"
#include "giomodule-priv.h"
#include "gportalsupport.h"
@@ -145,8 +146,8 @@
gchar **group,
gchar **basename)
{
- gint key_len = strlen (key);
- gint i;
+ gsize key_len = strlen (key);
+ const gchar *last_slash;
if (key_len < kfsb->prefix_len ||
memcmp (key, kfsb->prefix, kfsb->prefix_len) != 0)
@@ -155,38 +156,48 @@
key_len -= kfsb->prefix_len;
key += kfsb->prefix_len;
- for (i = key_len; i >= 0; i--)
- if (key[i] == '/')
- break;
+ last_slash = strrchr (key, '/');
+
+ /* Disallow empty group names or key names */
+ if (key_len == 0 ||
+ (last_slash != NULL &&
+ (*(last_slash + 1) == '\0' ||
+ last_slash == key)))
+ return FALSE;
if (kfsb->root_group)
{
/* if a root_group was specified, make sure the user hasn't given
* a path that ghosts that group name
*/
- if (i == kfsb->root_group_len && memcmp (key, kfsb->root_group, i) == 0)
+ if (last_slash != NULL && (last_slash - key) == kfsb->root_group_len && memcmp (key, kfsb->root_group, last_slash - key) == 0)
return FALSE;
}
else
{
/* if no root_group was given, ensure that the user gave a path */
- if (i == -1)
+ if (last_slash == NULL)
return FALSE;
}
if (group)
{
- if (i >= 0)
+ if (last_slash != NULL)
{
- *group = g_memdup (key, i + 1);
- (*group)[i] = '\0';
+ *group = g_memdup2 (key, (last_slash - key) + 1);
+ (*group)[(last_slash - key)] = '\0';
}
else
*group = g_strdup (kfsb->root_group);
}
if (basename)
- *basename = g_memdup (key + i + 1, key_len - i);
+ {
+ if (last_slash != NULL)
+ *basename = g_memdup2 (last_slash + 1, key_len - (last_slash - key));
+ else
+ *basename = g_strdup (key);
+ }
return TRUE;
}
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gio/gsettingsschema.c glib-2.62.6.new/gio/gsettingsschema.c
--- glib-2.62.6.old/gio/gsettingsschema.c 2021-03-03 08:49:45.493161255 +0800
+++ glib-2.62.6.new/gio/gsettingsschema.c 2021-03-03 16:30:43.817336605 +0800
@@ -20,6 +20,7 @@
#include "gsettingsschema-internal.h"
#include "gsettings.h"
+#include "gstrfuncsprivate.h"
#include "gvdb/gvdb-reader.h"
#include "strinfo.c"
@@ -1058,9 +1059,9 @@
if (g_str_has_suffix (key, "/"))
{
- gint length = strlen (key);
+ gsize length = strlen (key);
- strv[j] = g_memdup (key, length);
+ strv[j] = g_memdup2 (key, length);
strv[j][length - 1] = '\0';
j++;
}
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gio/gsocket.c glib-2.62.6.new/gio/gsocket.c
--- glib-2.62.6.old/gio/gsocket.c 2021-03-03 08:49:45.493161255 +0800
+++ glib-2.62.6.new/gio/gsocket.c 2021-03-03 16:35:32.327613237 +0800
@@ -75,6 +75,7 @@
#include "gcredentialsprivate.h"
#include "glibintl.h"
#include "gioprivate.h"
+#include "gstrfuncsprivate.h"
#ifdef G_OS_WIN32
/* For Windows XP runtime compatibility, but use the system's if_nametoindex() if available */
@@ -174,7 +175,7 @@
GError **error);
static GSocketAddress *
-cache_recv_address (GSocket *socket, struct sockaddr *native, int native_len);
+cache_recv_address (GSocket *socket, struct sockaddr *native, size_t native_len);
static gssize
g_socket_receive_message_with_timeout (GSocket *socket,
@@ -260,7 +261,7 @@
struct {
GSocketAddress *addr;
struct sockaddr *native;
- gint native_len;
+ gsize native_len;
guint64 last_used;
} recv_addr_cache[RECV_ADDR_CACHE_SIZE];
};
@@ -5211,14 +5212,14 @@
}
static GSocketAddress *
-cache_recv_address (GSocket *socket, struct sockaddr *native, int native_len)
+cache_recv_address (GSocket *socket, struct sockaddr *native, size_t native_len)
{
GSocketAddress *saddr;
gint i;
guint64 oldest_time = G_MAXUINT64;
gint oldest_index = 0;
- if (native_len <= 0)
+ if (native_len == 0)
return NULL;
saddr = NULL;
@@ -5226,7 +5227,7 @@
{
GSocketAddress *tmp = socket->priv->recv_addr_cache[i].addr;
gpointer tmp_native = socket->priv->recv_addr_cache[i].native;
- gint tmp_native_len = socket->priv->recv_addr_cache[i].native_len;
+ gsize tmp_native_len = socket->priv->recv_addr_cache[i].native_len;
if (!tmp)
continue;
@@ -5256,7 +5257,7 @@
g_free (socket->priv->recv_addr_cache[oldest_index].native);
}
- socket->priv->recv_addr_cache[oldest_index].native = g_memdup (native, native_len);
+ socket->priv->recv_addr_cache[oldest_index].native = g_memdup2 (native, native_len);
socket->priv->recv_addr_cache[oldest_index].native_len = native_len;
socket->priv->recv_addr_cache[oldest_index].addr = g_object_ref (saddr);
socket->priv->recv_addr_cache[oldest_index].last_used = g_get_monotonic_time ();
@@ -5404,6 +5405,9 @@
/* do it */
while (1)
{
+ /* addrlen has to be of type int because that’s how WSARecvFrom() is defined */
+ G_STATIC_ASSERT (sizeof addr <= G_MAXINT);
+
addrlen = sizeof addr;
if (address)
result = WSARecvFrom (socket->priv->fd,
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gio/gtlspassword.c glib-2.62.6.new/gio/gtlspassword.c
--- glib-2.62.6.old/gio/gtlspassword.c 2021-03-03 08:49:45.493161255 +0800
+++ glib-2.62.6.new/gio/gtlspassword.c 2021-03-03 16:39:37.198900994 +0800
@@ -23,6 +23,7 @@
#include "glibintl.h"
#include "gioenumtypes.h"
+#include "gstrfuncsprivate.h"
#include "gtlspassword.h"
#include <string.h>
@@ -287,9 +288,14 @@
g_return_if_fail (G_IS_TLS_PASSWORD (password));
if (length < 0)
- length = strlen ((gchar *)value);
+ {
+ /* FIXME: g_tls_password_set_value_full() doesn’t support unsigned gsize */
+ gsize length_unsigned = strlen ((gchar *) value);
+ g_return_if_fail (length_unsigned <= G_MAXSSIZE);
+ length = (gssize) length_unsigned;
+ }
- g_tls_password_set_value_full (password, g_memdup (value, length), length, g_free);
+ g_tls_password_set_value_full (password, g_memdup2 (value, (gsize) length), length, g_free);
}
/**
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gio/gwin32appinfo.c glib-2.62.6.new/gio/gwin32appinfo.c
--- glib-2.62.6.old/gio/gwin32appinfo.c 2021-03-03 08:49:45.493161255 +0800
+++ glib-2.62.6.new/gio/gwin32appinfo.c 2021-03-03 17:47:04.580196383 +0800
@@ -1,5 +1,5 @@
/* GIO - GLib Input, Output and Streaming Library
- *
+ *
* Copyright (C) 2006-2007 Red Hat, Inc.
* Copyright (C) 2014 Руслан Ижбулатов
*
@@ -464,15 +464,34 @@
/* Watch this key */
static GWin32RegistryKey *classes_root_key;
+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);
}
#define URL_ASSOCIATIONS L"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\"
@@ -2103,7 +2122,7 @@
&program_id_folded))
continue;
-
+
handler_rec = g_hash_table_lookup (handlers, program_id_folded);
if (handler_rec == NULL)
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gio/gwin32registrykey.c glib-2.62.6.new/gio/gwin32registrykey.c
--- glib-2.62.6.old/gio/gwin32registrykey.c 2021-03-03 08:49:45.493161255 +0800
+++ glib-2.62.6.new/gio/gwin32registrykey.c 2021-03-03 16:35:19.664396949 +0800
@@ -28,6 +28,8 @@
#include <ntstatus.h>
#include <winternl.h>
+#include "gstrfuncsprivate.h"
+
#ifndef _WDMDDK_
typedef enum _KEY_INFORMATION_CLASS {
KeyBasicInformation,
@@ -125,16 +127,34 @@
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);
}
/**
@@ -247,7 +267,7 @@
new_iter->value_name_size = iter->value_name_size;
if (iter->value_data != NULL)
- new_iter->value_data = g_memdup (iter->value_data, iter->value_data_size);
+ new_iter->value_data = g_memdup2 (iter->value_data, iter->value_data_size);
new_iter->value_data_size = iter->value_data_size;
@@ -268,8 +288,8 @@
new_iter->value_data_expanded_charsize = iter->value_data_expanded_charsize;
if (iter->value_data_expanded_u8 != NULL)
- new_iter->value_data_expanded_u8 = g_memdup (iter->value_data_expanded_u8,
- iter->value_data_expanded_charsize);
+ new_iter->value_data_expanded_u8 = g_memdup2 (iter->value_data_expanded_u8,
+ iter->value_data_expanded_charsize);
new_iter->value_data_expanded_u8_size = iter->value_data_expanded_charsize;
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gio/tests/async-close-output-stream.c glib-2.62.6.new/gio/tests/async-close-output-stream.c
--- glib-2.62.6.old/gio/tests/async-close-output-stream.c 2021-03-03 08:49:45.493161255 +0800
+++ glib-2.62.6.new/gio/tests/async-close-output-stream.c 2021-03-03 16:30:43.817336605 +0800
@@ -24,6 +24,8 @@
#include <stdlib.h>
#include <string.h>
+#include "gstrfuncsprivate.h"
+
#define DATA_TO_WRITE "Hello world\n"
typedef struct
@@ -147,9 +149,9 @@
data->expected_size = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (data->data_stream));
- g_assert_cmpint (data->expected_size, >, 0);
+ g_assert_cmpuint (data->expected_size, >, 0);
- data->expected_output = g_memdup (written, (guint)data->expected_size);
+ data->expected_output = g_memdup2 (written, data->expected_size);
/* then recreate the streams and prepare them for the asynchronous close */
destroy_streams (data);
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gio/tests/gdbus-export.c glib-2.62.6.new/gio/tests/gdbus-export.c
--- glib-2.62.6.old/gio/tests/gdbus-export.c 2021-03-03 08:49:45.496494586 +0800
+++ glib-2.62.6.new/gio/tests/gdbus-export.c 2021-03-03 16:30:43.817336605 +0800
@@ -23,6 +23,7 @@
#include <string.h>
#include "gdbus-tests.h"
+#include "gstrfuncsprivate.h"
/* all tests rely on a shared mainloop */
static GMainLoop *loop = NULL;
@@ -671,7 +672,7 @@
g_assert_not_reached ();
}
- return g_memdup (interfaces, 2 * sizeof (void *));
+ return g_memdup2 (interfaces, 2 * sizeof (void *));
}
static const GDBusInterfaceVTable *
@@ -727,7 +728,7 @@
{
const GDBusInterfaceInfo *interfaces[2] = { &dyna_interface_info, NULL };
- return g_memdup (interfaces, 2 * sizeof (void *));
+ return g_memdup2 (interfaces, 2 * sizeof (void *));
}
static const GDBusInterfaceVTable *
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gio/tests/gsettings.c glib-2.62.6.new/gio/tests/gsettings.c
--- glib-2.62.6.old/gio/tests/gsettings.c 2021-03-03 08:49:45.496494586 +0800
+++ glib-2.62.6.new/gio/tests/gsettings.c 2021-03-03 17:18:40.464332950 +0800
@@ -1,4 +1,5 @@
#include <stdlib.h>
+#include <errno.h>
#include <locale.h>
#include <libintl.h>
#include <unistd.h>
@@ -10,6 +11,38 @@
#include "testenum.h"
+/**
+ * g_assert_no_errno:
+ * @expr: the expression to check
+ *
+ * Debugging macro to check that an expression has a non-negative return value,
+ * as used by traditional POSIX functions (such as `rmdir()`) to indicate
+ * success.
+ *
+ * If the assertion fails (i.e. the @expr returns a negative value), an error
+ * message is logged and the testcase is marked as failed. The error message
+ * will contain the value of `errno` and its human-readable message from
+ * g_strerror().
+ *
+ * This macro will clear the value of `errno` before executing @expr.
+ *
+ * Since: 2.66
+ * Backported here as a private macro for testing.
+ */
+#define g_assert_no_errno(expr) G_STMT_START { \
+ int __ret, __errsv; \
+ errno = 0; \
+ __ret = expr; \
+ __errsv = errno; \
+ if (__ret < 0) \
+ { \
+ gchar *__msg; \
+ __msg = g_strdup_printf ("assertion failed (" #expr " >= 0): errno %i: %s", __errsv, g_strerror (__errsv)); \
+ g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, __msg); \
+ g_free (__msg); \
+ } \
+ } G_STMT_END
+
static const gchar *locale_dir = ".";
static gboolean backend_set;
@@ -1740,6 +1773,14 @@
(*b) = TRUE;
}
+typedef struct
+{
+ const gchar *path;
+ const gchar *root_group;
+ const gchar *keyfile_group;
+ const gchar *root_path;
+} KeyfileTestData;
+
/*
* Test that using a keyfile works
*/
@@ -1834,7 +1875,11 @@
g_free (str);
g_settings_set (settings, "farewell", "s", "cheerio");
-
+
+ /* Check that empty keys/groups are not allowed. */
+ g_assert_false (g_settings_is_writable (settings, ""));
+ g_assert_false (g_settings_is_writable (settings, "/"));
+
/* When executing as root, changing the mode of the keyfile will have
* no effect on the writability of the settings.
*/
@@ -1866,6 +1911,149 @@
g_free (keyfile_path);
}
+/*
+ * Test that using a keyfile works with a schema with no path set.
+ */
+static void
+test_keyfile_no_path (Fixture *fixture,
+ gconstpointer user_data)
+{
+ const KeyfileTestData *test_data = user_data;
+ GSettingsBackend *kf_backend;
+ GSettings *settings;
+ GKeyFile *keyfile;
+ gboolean writable;
+ gchar *key = NULL;
+ GError *error = NULL;
+ gchar *keyfile_path = NULL, *store_path = NULL;
+
+ keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
+ store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
+ kf_backend = g_keyfile_settings_backend_new (store_path, test_data->root_path, test_data->root_group);
+ settings = g_settings_new_with_backend_and_path ("org.gtk.test.no-path", kf_backend, test_data->path);
+ g_object_unref (kf_backend);
+
+ g_settings_reset (settings, "test-boolean");
+ g_assert_true (g_settings_get_boolean (settings, "test-boolean"));
+
+ writable = g_settings_is_writable (settings, "test-boolean");
+ g_assert_true (writable);
+ g_settings_set (settings, "test-boolean", "b", FALSE);
+
+ g_assert_false (g_settings_get_boolean (settings, "test-boolean"));
+
+ g_settings_delay (settings);
+ g_settings_set (settings, "test-boolean", "b", TRUE);
+ g_settings_apply (settings);
+
+ keyfile = g_key_file_new ();
+ g_assert_true (g_key_file_load_from_file (keyfile, store_path, 0, NULL));
+
+ g_assert_true (g_key_file_get_boolean (keyfile, test_data->keyfile_group, "test-boolean", NULL));
+
+ g_key_file_free (keyfile);
+
+ g_settings_reset (settings, "test-boolean");
+ g_settings_apply (settings);
+ keyfile = g_key_file_new ();
+ g_assert_true (g_key_file_load_from_file (keyfile, store_path, 0, NULL));
+
+ g_assert_false (g_key_file_get_string (keyfile, test_data->keyfile_group, "test-boolean", &error));
+ g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
+ g_clear_error (&error);
+
+ /* Check that empty keys/groups are not allowed. */
+ g_assert_false (g_settings_is_writable (settings, ""));
+ g_assert_false (g_settings_is_writable (settings, "/"));
+
+ /* Keys which ghost the root group name are not allowed. This can only be
+ * tested when the path is `/` as otherwise it acts as a prefix and prevents
+ * any ghosting. */
+ if (g_str_equal (test_data->path, "/"))
+ {
+ key = g_strdup_printf ("%s/%s", test_data->root_group, "");
+ g_assert_false (g_settings_is_writable (settings, key));
+ g_free (key);
+
+ key = g_strdup_printf ("%s/%s", test_data->root_group, "/");
+ g_assert_false (g_settings_is_writable (settings, key));
+ g_free (key);
+
+ key = g_strdup_printf ("%s/%s", test_data->root_group, "test-boolean");
+ g_assert_false (g_settings_is_writable (settings, key));
+ g_free (key);
+ }
+
+ g_key_file_free (keyfile);
+ g_object_unref (settings);
+
+ /* Clean up the temporary directory. */
+ g_assert_no_errno (g_chmod (keyfile_path, 0777));
+ g_assert_no_errno (g_remove (store_path));
+ g_assert_no_errno (g_rmdir (keyfile_path));
+ g_free (store_path);
+ g_free (keyfile_path);
+}
+
+/*
+ * Test that a keyfile rejects writes to keys outside its root path.
+ */
+static void
+test_keyfile_outside_root_path (Fixture *fixture,
+ gconstpointer user_data)
+{
+ GSettingsBackend *kf_backend;
+ GSettings *settings;
+ gchar *keyfile_path = NULL, *store_path = NULL;
+
+ keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
+ store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
+ kf_backend = g_keyfile_settings_backend_new (store_path, "/tests/basic-types/", "root");
+ settings = g_settings_new_with_backend_and_path ("org.gtk.test.no-path", kf_backend, "/tests/");
+ g_object_unref (kf_backend);
+
+ g_assert_false (g_settings_is_writable (settings, "test-boolean"));
+
+ g_object_unref (settings);
+
+ /* Clean up the temporary directory. The keyfile probably doesn’t exist, so
+ * don’t error on failure. */
+ g_remove (store_path);
+ g_assert_no_errno (g_rmdir (keyfile_path));
+ g_free (store_path);
+ g_free (keyfile_path);
+}
+
+/*
+ * Test that a keyfile rejects writes to keys in the root if no root group is set.
+ */
+static void
+test_keyfile_no_root_group (Fixture *fixture,
+ gconstpointer user_data)
+{
+ GSettingsBackend *kf_backend;
+ GSettings *settings;
+ gchar *keyfile_path = NULL, *store_path = NULL;
+
+ keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
+ store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
+ kf_backend = g_keyfile_settings_backend_new (store_path, "/", NULL);
+ settings = g_settings_new_with_backend_and_path ("org.gtk.test.no-path", kf_backend, "/");
+ g_object_unref (kf_backend);
+
+ g_assert_false (g_settings_is_writable (settings, "test-boolean"));
+ g_assert_true (g_settings_is_writable (settings, "child/test-boolean"));
+
+ g_object_unref (settings);
+
+ /* Clean up the temporary directory. The keyfile probably doesn’t exist, so
+ * don’t error on failure. */
+ g_remove (store_path);
+ g_assert_no_errno (g_rmdir (keyfile_path));
+ g_free (store_path);
+ g_free (keyfile_path);
+}
+
/* Test that getting child schemas works
*/
static void
@@ -2844,6 +3032,14 @@
gchar *override_text;
gchar *enums;
gint result;
+ const KeyfileTestData keyfile_test_data_explicit_path = { "/tests/", "root", "tests", "/" };
+ const KeyfileTestData keyfile_test_data_empty_path = { "/", "root", "root", "/" };
+ const KeyfileTestData keyfile_test_data_long_path = {
+ "/tests/path/is/very/long/and/this/makes/some/comparisons/take/a/different/branch/",
+ "root",
+ "tests/path/is/very/long/and/this/makes/some/comparisons/take/a/different/branch",
+ "/"
+ };
/* Meson build sets this */
#ifdef TEST_LOCALE_PATH
@@ -2967,6 +3163,11 @@
}
g_test_add ("/gsettings/keyfile", Fixture, NULL, setup, test_keyfile, teardown);
+ g_test_add ("/gsettings/keyfile/explicit-path", Fixture, &keyfile_test_data_explicit_path, setup, test_keyfile_no_path, teardown);
+ g_test_add ("/gsettings/keyfile/empty-path", Fixture, &keyfile_test_data_empty_path, setup, test_keyfile_no_path, teardown);
+ g_test_add ("/gsettings/keyfile/long-path", Fixture, &keyfile_test_data_long_path, setup, test_keyfile_no_path, teardown);
+ g_test_add ("/gsettings/keyfile/outside-root-path", Fixture, NULL, setup, test_keyfile_outside_root_path, teardown);
+ g_test_add ("/gsettings/keyfile/no-root-group", Fixture, NULL, setup, test_keyfile_no_root_group, teardown);
g_test_add_func ("/gsettings/child-schema", test_child_schema);
g_test_add_func ("/gsettings/strinfo", test_strinfo);
g_test_add_func ("/gsettings/enums", test_enums);
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gio/tests/tls-interaction.c glib-2.62.6.new/gio/tests/tls-interaction.c
--- glib-2.62.6.old/gio/tests/tls-interaction.c 2021-03-03 08:49:45.499827919 +0800
+++ glib-2.62.6.new/gio/tests/tls-interaction.c 2021-03-03 16:39:42.345528415 +0800
@@ -174,6 +174,38 @@
}
+/* Return a copy of @str that is allocated in a silly way, to exercise
+ * custom free-functions. The returned pointer points to a copy of @str
+ * in a buffer of the form "BEFORE \0 str \0 AFTER". */
+static guchar *
+special_dup (const char *str)
+{
+ GString *buf = g_string_new ("BEFORE");
+ guchar *ret;
+
+ g_string_append_c (buf, '\0');
+ g_string_append (buf, str);
+ g_string_append_c (buf, '\0');
+ g_string_append (buf, "AFTER");
+ ret = (guchar *) g_string_free (buf, FALSE);
+ return ret + strlen ("BEFORE") + 1;
+}
+
+
+/* Free a copy of @str that was made with special_dup(), after asserting
+ * that it has not been corrupted. */
+static void
+special_free (gpointer p)
+{
+ gchar *s = p;
+ gchar *buf = s - strlen ("BEFORE") - 1;
+
+ g_assert_cmpstr (buf, ==, "BEFORE");
+ g_assert_cmpstr (s + strlen (s) + 1, ==, "AFTER");
+ g_free (buf);
+}
+
+
static GTlsInteractionResult
test_interaction_ask_password_sync_success (GTlsInteraction *interaction,
GTlsPassword *password,
@@ -181,6 +213,8 @@
GError **error)
{
TestInteraction *self;
+ const guchar *value;
+ gsize len;
g_assert (TEST_IS_INTERACTION (interaction));
self = TEST_INTERACTION (interaction);
@@ -192,6 +226,27 @@
g_assert (error != NULL);
g_assert (*error == NULL);
+ /* Exercise different ways to set the value */
+ g_tls_password_set_value (password, (const guchar *) "foo", 4);
+ len = 0;
+ value = g_tls_password_get_value (password, &len);
+ g_assert_cmpmem (value, len, "foo", 4);
+
+ g_tls_password_set_value (password, (const guchar *) "bar", -1);
+ len = 0;
+ value = g_tls_password_get_value (password, &len);
+ g_assert_cmpmem (value, len, "bar", 3);
+
+ g_tls_password_set_value_full (password, special_dup ("baa"), 4, special_free);
+ len = 0;
+ value = g_tls_password_get_value (password, &len);
+ g_assert_cmpmem (value, len, "baa", 4);
+
+ g_tls_password_set_value_full (password, special_dup ("baz"), -1, special_free);
+ len = 0;
+ value = g_tls_password_get_value (password, &len);
+ g_assert_cmpmem (value, len, "baz", 3);
+
/* Don't do this in real life. Include a null terminator for testing */
g_tls_password_set_value (password, (const guchar *)"the password", 13);
return G_TLS_INTERACTION_HANDLED;
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gio/win32/gwinhttpfile.c glib-2.62.6.new/gio/win32/gwinhttpfile.c
--- glib-2.62.6.old/gio/win32/gwinhttpfile.c 2021-03-03 08:49:45.499827919 +0800
+++ glib-2.62.6.new/gio/win32/gwinhttpfile.c 2021-03-03 16:35:06.734517818 +0800
@@ -29,6 +29,7 @@
#include "gio/gfile.h"
#include "gio/gfileattribute.h"
#include "gio/gfileinfo.h"
+#include "gstrfuncsprivate.h"
#include "gwinhttpfile.h"
#include "gwinhttpfileinputstream.h"
#include "gwinhttpfileoutputstream.h"
@@ -393,10 +394,10 @@
child = g_object_new (G_TYPE_WINHTTP_FILE, NULL);
child->vfs = winhttp_file->vfs;
child->url = winhttp_file->url;
- child->url.lpszScheme = g_memdup (winhttp_file->url.lpszScheme, (winhttp_file->url.dwSchemeLength+1)*2);
- child->url.lpszHostName = g_memdup (winhttp_file->url.lpszHostName, (winhttp_file->url.dwHostNameLength+1)*2);
- child->url.lpszUserName = g_memdup (winhttp_file->url.lpszUserName, (winhttp_file->url.dwUserNameLength+1)*2);
- child->url.lpszPassword = g_memdup (winhttp_file->url.lpszPassword, (winhttp_file->url.dwPasswordLength+1)*2);
+ child->url.lpszScheme = g_memdup2 (winhttp_file->url.lpszScheme, ((gsize) winhttp_file->url.dwSchemeLength + 1) * 2);
+ child->url.lpszHostName = g_memdup2 (winhttp_file->url.lpszHostName, ((gsize) winhttp_file->url.dwHostNameLength + 1) * 2);
+ child->url.lpszUserName = g_memdup2 (winhttp_file->url.lpszUserName, ((gsize) winhttp_file->url.dwUserNameLength + 1) * 2);
+ child->url.lpszPassword = g_memdup2 (winhttp_file->url.lpszPassword, ((gsize) winhttp_file->url.dwPasswordLength + 1) * 2);
child->url.lpszUrlPath = wnew_path;
child->url.dwUrlPathLength = wcslen (wnew_path);
child->url.lpszExtraInfo = NULL;
diff --unified --recursive --text --new-file --color glib-2.62.6.old/glib/gbytes.c glib-2.62.6.new/glib/gbytes.c
--- glib-2.62.6.old/glib/gbytes.c 2021-03-03 08:49:45.499827919 +0800
+++ glib-2.62.6.new/glib/gbytes.c 2021-03-03 16:32:33.262736897 +0800
@@ -34,6 +34,8 @@
#include <string.h>
+#include "gstrfuncsprivate.h"
+
/**
* GBytes:
*
@@ -95,7 +97,7 @@
{
g_return_val_if_fail (data != NULL || size == 0, NULL);
- return g_bytes_new_take (g_memdup (data, size), size);
+ return g_bytes_new_take (g_memdup2 (data, size), size);
}
/**
@@ -499,7 +501,7 @@
* Copy: Non g_malloc (or compatible) allocator, or static memory,
* so we have to copy, and then unref.
*/
- result = g_memdup (bytes->data, bytes->size);
+ result = g_memdup2 (bytes->data, bytes->size);
*size = bytes->size;
g_bytes_unref (bytes);
}
diff --unified --recursive --text --new-file --color glib-2.62.6.old/glib/gdir.c glib-2.62.6.new/glib/gdir.c
--- glib-2.62.6.old/glib/gdir.c 2021-03-03 08:49:45.499827919 +0800
+++ glib-2.62.6.new/glib/gdir.c 2021-03-03 16:32:33.262736897 +0800
@@ -37,6 +37,7 @@
#include "gconvert.h"
#include "gfileutils.h"
#include "gstrfuncs.h"
+#include "gstrfuncsprivate.h"
#include "gtestutils.h"
#include "glibintl.h"
@@ -112,7 +113,7 @@
return NULL;
#endif
- return g_memdup (&dir, sizeof dir);
+ return g_memdup2 (&dir, sizeof dir);
}
/**
diff --unified --recursive --text --new-file --color glib-2.62.6.old/glib/ghash.c glib-2.62.6.new/glib/ghash.c
--- glib-2.62.6.old/glib/ghash.c 2021-03-03 08:49:45.499827919 +0800
+++ glib-2.62.6.new/glib/ghash.c 2021-03-03 16:32:33.262736897 +0800
@@ -34,6 +34,7 @@
#include "gmacros.h"
#include "glib-private.h"
#include "gstrfuncs.h"
+#include "gstrfuncsprivate.h"
#include "gatomic.h"
#include "gtestutils.h"
#include "gslice.h"
@@ -964,7 +965,7 @@
if (hash_table->have_big_keys)
{
if (key != value)
- hash_table->values = g_memdup (hash_table->keys, sizeof (gpointer) * hash_table->size);
+ hash_table->values = g_memdup2 (hash_table->keys, sizeof (gpointer) * hash_table->size);
/* Keys and values are both big now, so no need for further checks */
return;
}
@@ -972,7 +973,7 @@
{
if (key != value)
{
- hash_table->values = g_memdup (hash_table->keys, sizeof (guint) * hash_table->size);
+ hash_table->values = g_memdup2 (hash_table->keys, sizeof (guint) * hash_table->size);
is_a_set = FALSE;
}
}
@@ -1000,7 +1001,7 @@
/* Just split if necessary */
if (is_a_set && key != value)
- hash_table->values = g_memdup (hash_table->keys, sizeof (gpointer) * hash_table->size);
+ hash_table->values = g_memdup2 (hash_table->keys, sizeof (gpointer) * hash_table->size);
#endif
}
diff --unified --recursive --text --new-file --color glib-2.62.6.old/glib/giochannel.c glib-2.62.6.new/glib/giochannel.c
--- glib-2.62.6.old/glib/giochannel.c 2021-03-03 08:49:45.503161252 +0800
+++ glib-2.62.6.new/glib/giochannel.c 2021-03-03 16:39:15.155737252 +0800
@@ -22,10 +22,10 @@
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
- * GLib at ftp://ftp.gtk.org/pub/gtk/.
+ * GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
-/*
+/*
* MT safe
*/
@@ -37,6 +37,7 @@
#include "giochannel.h"
#include "gstrfuncs.h"
+#include "gstrfuncsprivate.h"
#include "gtestutils.h"
#include "glibintl.h"
@@ -176,10 +177,10 @@
* g_io_channel_init:
* @channel: a #GIOChannel
*
- * Initializes a #GIOChannel struct.
+ * Initializes a #GIOChannel struct.
*
- * This is called by each of the above functions when creating a
- * #GIOChannel, and so is not often needed by the application
+ * This is called by each of the above functions when creating a
+ * #GIOChannel, and so is not often needed by the application
* programmer (unless you are creating a new type of #GIOChannel).
*/
void
@@ -225,7 +226,7 @@
*
* Decrements the reference count of a #GIOChannel.
*/
-void
+void
g_io_channel_unref (GIOChannel *channel)
{
gboolean is_zero;
@@ -269,7 +270,7 @@
return G_IO_ERROR_AGAIN;
case G_IO_STATUS_ERROR:
g_return_val_if_fail (err != NULL, G_IO_ERROR_UNKNOWN);
-
+
if (err->domain != G_IO_CHANNEL_ERROR)
return G_IO_ERROR_UNKNOWN;
switch (err->code)
@@ -287,20 +288,20 @@
/**
* g_io_channel_read:
* @channel: a #GIOChannel
- * @buf: a buffer to read the data into (which should be at least
+ * @buf: a buffer to read the data into (which should be at least
* count bytes long)
* @count: the number of bytes to read from the #GIOChannel
* @bytes_read: returns the number of bytes actually read
- *
- * Reads data from a #GIOChannel.
- *
- * Returns: %G_IO_ERROR_NONE if the operation was successful.
+ *
+ * Reads data from a #GIOChannel.
+ *
+ * Returns: %G_IO_ERROR_NONE if the operation was successful.
*
* Deprecated:2.2: Use g_io_channel_read_chars() instead.
**/
-GIOError
-g_io_channel_read (GIOChannel *channel,
- gchar *buf,
+GIOError
+g_io_channel_read (GIOChannel *channel,
+ gchar *buf,
gsize count,
gsize *bytes_read)
{
@@ -336,16 +337,16 @@
* @buf: the buffer containing the data to write
* @count: the number of bytes to write
* @bytes_written: the number of bytes actually written
- *
- * Writes data to a #GIOChannel.
- *
+ *
+ * Writes data to a #GIOChannel.
+ *
* Returns: %G_IO_ERROR_NONE if the operation was successful.
*
* Deprecated:2.2: Use g_io_channel_write_chars() instead.
**/
-GIOError
-g_io_channel_write (GIOChannel *channel,
- const gchar *buf,
+GIOError
+g_io_channel_write (GIOChannel *channel,
+ const gchar *buf,
gsize count,
gsize *bytes_written)
{
@@ -369,22 +370,22 @@
/**
* g_io_channel_seek:
* @channel: a #GIOChannel
- * @offset: an offset, in bytes, which is added to the position specified
+ * @offset: an offset, in bytes, which is added to the position specified
* by @type
* @type: the position in the file, which can be %G_SEEK_CUR (the current
- * position), %G_SEEK_SET (the start of the file), or %G_SEEK_END
+ * position), %G_SEEK_SET (the start of the file), or %G_SEEK_END
* (the end of the file)
- *
- * Sets the current position in the #GIOChannel, similar to the standard
- * library function fseek().
- *
+ *
+ * Sets the current position in the #GIOChannel, similar to the standard
+ * library function fseek().
+ *
* Returns: %G_IO_ERROR_NONE if the operation was successful.
*
* Deprecated:2.2: Use g_io_channel_seek_position() instead.
**/
-GIOError
+GIOError
g_io_channel_seek (GIOChannel *channel,
- gint64 offset,
+ gint64 offset,
GSeekType type)
{
GError *err = NULL;
@@ -438,10 +439,10 @@
/**
* g_io_channel_close:
* @channel: A #GIOChannel
- *
+ *
* Close an IO channel. Any pending data to be written will be
* flushed, ignoring errors. The channel will not be freed until the
- * last reference is dropped using g_io_channel_unref().
+ * last reference is dropped using g_io_channel_unref().
*
* Deprecated:2.2: Use g_io_channel_shutdown() instead.
**/
@@ -449,7 +450,7 @@
g_io_channel_close (GIOChannel *channel)
{
GError *err = NULL;
-
+
g_return_if_fail (channel != NULL);
g_io_channel_purge (channel);
@@ -461,7 +462,7 @@
g_warning ("Error closing channel: %s", err->message);
g_error_free (err);
}
-
+
channel->close_on_unref = FALSE; /* Because we already did */
channel->is_readable = FALSE;
channel->is_writeable = FALSE;
@@ -473,7 +474,7 @@
* @channel: a #GIOChannel
* @flush: if %TRUE, flush pending
* @err: location to store a #GIOChannelError
- *
+ *
* Close an IO channel. Any pending data to be written will be
* flushed if @flush is %TRUE. The channel will not be freed until the
* last reference is dropped using g_io_channel_unref().
@@ -487,7 +488,7 @@
{
GIOStatus status, result;
GError *tmperr = NULL;
-
+
g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
g_return_val_if_fail (err == NULL || *err == NULL, G_IO_STATUS_ERROR);
@@ -496,7 +497,7 @@
if (flush)
{
GIOFlags flags;
-
+
/* Set the channel to blocking, to avoid a busy loop
*/
flags = g_io_channel_get_flags (channel);
@@ -592,12 +593,12 @@
* @channel: a #GIOChannel to watch
* @condition: conditions to watch for
*
- * Creates a #GSource that's dispatched when @condition is met for the
- * given @channel. For example, if condition is #G_IO_IN, the source will
+ * Creates a #GSource that's dispatched when @condition is met for the
+ * given @channel. For example, if condition is #G_IO_IN, the source will
* be dispatched when there's data available for reading.
*
- * g_io_add_watch() is a simpler interface to this same functionality, for
- * the case where you want to add the source to the default main loop context
+ * g_io_add_watch() is a simpler interface to this same functionality, for
+ * the case where you want to add the source to the default main loop context
* at the default priority.
*
* On Windows, polling a #GSource created to watch a channel for a socket
@@ -633,7 +634,7 @@
*
* Returns: the event source id
*/
-guint
+guint
g_io_add_watch_full (GIOChannel *channel,
gint priority,
GIOCondition condition,
@@ -643,7 +644,7 @@
{
GSource *source;
guint id;
-
+
g_return_val_if_fail (channel != NULL, 0);
source = g_io_create_watch (channel, condition);
@@ -696,7 +697,7 @@
* A bitwise combination representing a condition to watch for on an
* event source.
**/
-guint
+guint
g_io_add_watch (GIOChannel *channel,
GIOCondition condition,
GIOFunc func,
@@ -710,7 +711,7 @@
* @channel: A #GIOChannel
*
* This function returns a #GIOCondition depending on whether there
- * is data to be read/space to write data in the internal buffers in
+ * is data to be read/space to write data in the internal buffers in
* the #GIOChannel. Only the flags %G_IO_IN and %G_IO_OUT may be set.
*
* Returns: A #GIOCondition
@@ -743,7 +744,7 @@
*
* Converts an `errno` error number to a #GIOChannelError.
*
- * Returns: a #GIOChannelError error number, e.g.
+ * Returns: a #GIOChannelError error number, e.g.
* %G_IO_CHANNEL_ERROR_INVAL.
**/
GIOChannelError
@@ -832,7 +833,7 @@
* @size: the size of the buffer, or 0 to let GLib pick a good size
*
* Sets the buffer size.
- **/
+ **/
void
g_io_channel_set_buffer_size (GIOChannel *channel,
gsize size)
@@ -855,7 +856,7 @@
* Gets the buffer size.
*
* Returns: the size of the buffer.
- **/
+ **/
gsize
g_io_channel_get_buffer_size (GIOChannel *channel)
{
@@ -883,17 +884,26 @@
const gchar *line_term,
gint length)
{
+ guint length_unsigned;
+
g_return_if_fail (channel != NULL);
g_return_if_fail (line_term == NULL || length != 0); /* Disallow "" */
if (line_term == NULL)
- length = 0;
- else if (length < 0)
- length = strlen (line_term);
+ length_unsigned = 0;
+ else if (length >= 0)
+ length_unsigned = (guint) length;
+ else
+ {
+ /* FIXME: We’re constrained by line_term_len being a guint here */
+ gsize length_size = strlen (line_term);
+ g_return_if_fail (length_size <= G_MAXUINT);
+ length_unsigned = (guint) length_size;
+ }
g_free (channel->line_term);
- channel->line_term = line_term ? g_memdup (line_term, length) : NULL;
- channel->line_term_len = length;
+ channel->line_term = line_term ? g_memdup2 (line_term, length_unsigned) : NULL;
+ channel->line_term_len = length_unsigned;
}
/**
@@ -928,7 +938,7 @@
*
* Sets the (writeable) flags in @channel to (@flags & %G_IO_FLAG_SET_MASK).
*
- * Returns: the status of the operation.
+ * Returns: the status of the operation.
**/
/**
* GIOFlags:
@@ -1252,7 +1262,7 @@
*
* Returns whether @channel is buffered.
*
- * Return Value: %TRUE if the @channel is buffered.
+ * Return Value: %TRUE if the @channel is buffered.
**/
gboolean
g_io_channel_get_buffered (GIOChannel *channel)
@@ -1268,15 +1278,15 @@
* @encoding: (nullable): the encoding type
* @error: location to store an error of type #GConvertError
*
- * Sets the encoding for the input/output of the channel.
- * The internal encoding is always UTF-8. The default encoding
+ * Sets the encoding for the input/output of the channel.
+ * The internal encoding is always UTF-8. The default encoding
* for the external file is UTF-8.
*
* The encoding %NULL is safe to use with binary data.
*
* The encoding can only be set if one of the following conditions
* is true:
- *
+ *
* - The channel was just created, and has not been written to or read from yet.
*
* - The channel is write-only.
@@ -1289,18 +1299,18 @@
*
* - One of the (new API) read functions has just returned %G_IO_STATUS_EOF
* (or, in the case of g_io_channel_read_to_end(), %G_IO_STATUS_NORMAL).
- *
- * - One of the functions g_io_channel_read_chars() or
- * g_io_channel_read_unichar() has returned %G_IO_STATUS_AGAIN or
- * %G_IO_STATUS_ERROR. This may be useful in the case of
+ *
+ * - One of the functions g_io_channel_read_chars() or
+ * g_io_channel_read_unichar() has returned %G_IO_STATUS_AGAIN or
+ * %G_IO_STATUS_ERROR. This may be useful in the case of
* %G_CONVERT_ERROR_ILLEGAL_SEQUENCE.
* Returning one of these statuses from g_io_channel_read_line(),
* g_io_channel_read_line_string(), or g_io_channel_read_to_end()
* does not guarantee that the encoding can be changed.
*
* Channels which do not meet one of the above conditions cannot call
- * g_io_channel_seek_position() with an offset of %G_SEEK_CUR, and, if
- * they are "seekable", cannot call g_io_channel_write_chars() after
+ * g_io_channel_seek_position() with an offset of %G_SEEK_CUR, and, if
+ * they are "seekable", cannot call g_io_channel_write_chars() after
* calling one of the API "read" functions.
*
* Return Value: %G_IO_STATUS_NORMAL if the encoding was successfully set
@@ -1437,8 +1447,8 @@
* g_io_channel_get_encoding:
* @channel: a #GIOChannel
*
- * Gets the encoding for the input/output of the channel.
- * The internal encoding is always UTF-8. The encoding %NULL
+ * Gets the encoding for the input/output of the channel.
+ * The internal encoding is always UTF-8. The encoding %NULL
* makes the channel safe for binary data.
*
* Returns: A string containing the encoding, this string is
@@ -1629,7 +1639,7 @@
* @channel: a #GIOChannel
* @str_return: (out): The line read from the #GIOChannel, including the
* line terminator. This data should be freed with g_free()
- * when no longer needed. This is a nul-terminated string.
+ * when no longer needed. This is a nul-terminated string.
* If a @length of zero is returned, this will be %NULL instead.
* @length: (out) (optional): location to store length of the read data, or %NULL
* @terminator_pos: (out) (optional): location to store position of line terminator, or %NULL
@@ -1652,7 +1662,7 @@
{
GIOStatus status;
gsize got_length;
-
+
g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
g_return_val_if_fail ((error == NULL) || (*error == NULL),
@@ -1672,7 +1682,7 @@
}
else
*str_return = NULL;
-
+
return status;
}
@@ -1902,7 +1912,7 @@
*
* Reads all the remaining data from the file.
*
- * Returns: %G_IO_STATUS_NORMAL on success.
+ * Returns: %G_IO_STATUS_NORMAL on success.
* This function never returns %G_IO_STATUS_EOF.
**/
GIOStatus
@@ -1912,7 +1922,7 @@
GError **error)
{
GIOStatus status;
-
+
g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
g_return_val_if_fail ((error == NULL) || (*error == NULL),
G_IO_STATUS_ERROR);
@@ -2208,10 +2218,10 @@
if (!channel->use_buffer)
{
gsize tmp_bytes;
-
+
g_assert (!channel->write_buf || channel->write_buf->len == 0);
g_assert (channel->partial_write_buf[0] == '\0');
-
+
status = channel->funcs->io_write (channel, buf, count_unsigned,
&tmp_bytes, error);
diff --unified --recursive --text --new-file --color glib-2.62.6.old/glib/gslice.c glib-2.62.6.new/glib/gslice.c
--- glib-2.62.6.old/glib/gslice.c 2021-03-03 08:49:45.503161252 +0800
+++ glib-2.62.6.new/glib/gslice.c 2021-03-03 16:32:33.262736897 +0800
@@ -41,6 +41,7 @@
#include "gmain.h"
#include "gmem.h" /* gslice.h */
#include "gstrfuncs.h"
+#include "gstrfuncsprivate.h"
#include "gutils.h"
#include "gtrashstack.h"
#include "gtestutils.h"
@@ -350,7 +351,7 @@
array[i++] = allocator->contention_counters[address];
array[i++] = allocator_get_magazine_threshold (allocator, address);
*n_values = i;
- return g_memdup (array, sizeof (array[0]) * *n_values);
+ return g_memdup2 (array, sizeof (array[0]) * *n_values);
default:
return NULL;
}
diff --unified --recursive --text --new-file --color glib-2.62.6.old/glib/gstrfuncsprivate.h glib-2.62.6.new/glib/gstrfuncsprivate.h
--- glib-2.62.6.old/glib/gstrfuncsprivate.h 1970-01-01 08:00:00.000000000 +0800
+++ glib-2.62.6.new/glib/gstrfuncsprivate.h 2021-03-03 16:29:56.861262840 +0800
@@ -0,0 +1,55 @@
+/* GLIB - Library of useful routines for C programming
+ * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <glib.h>
+#include <string.h>
+
+/*
+ * g_memdup2:
+ * @mem: (nullable): the memory to copy.
+ * @byte_size: the number of bytes to copy.
+ *
+ * Allocates @byte_size bytes of memory, and copies @byte_size bytes into it
+ * from @mem. If @mem is %NULL it returns %NULL.
+ *
+ * This replaces g_memdup(), which was prone to integer overflows when
+ * converting the argument from a #gsize to a #guint.
+ *
+ * This static inline version is a backport of the new public API from
+ * GLib 2.68, kept internal to GLib for backport to older stable releases.
+ * See https://gitlab.gnome.org/GNOME/glib/-/issues/2319.
+ *
+ * Returns: (nullable): a pointer to the newly-allocated copy of the memory,
+ * or %NULL if @mem is %NULL.
+ * Since: 2.68
+ */
+static inline gpointer
+g_memdup2 (gconstpointer mem,
+ gsize byte_size)
+{
+ gpointer new_mem;
+
+ if (mem && byte_size != 0)
+ {
+ new_mem = g_malloc (byte_size);
+ memcpy (new_mem, mem, byte_size);
+ }
+ else
+ new_mem = NULL;
+
+ return new_mem;
+}
diff --unified --recursive --text --new-file --color glib-2.62.6.old/glib/gtestutils.c glib-2.62.6.new/glib/gtestutils.c
--- glib-2.62.6.old/glib/gtestutils.c 2021-03-03 08:49:45.503161252 +0800
+++ glib-2.62.6.new/glib/gtestutils.c 2021-03-03 16:32:33.266070197 +0800
@@ -49,6 +49,7 @@
#include "gpattern.h"
#include "grand.h"
#include "gstrfuncs.h"
+#include "gstrfuncsprivate.h"
#include "gtimer.h"
#include "gslice.h"
#include "gspawn.h"
@@ -3798,7 +3799,7 @@
if (p <= tbuffer->data->str + mlength)
{
g_string_erase (tbuffer->data, 0, mlength);
- tbuffer->msgs = g_slist_prepend (tbuffer->msgs, g_memdup (&msg, sizeof (msg)));
+ tbuffer->msgs = g_slist_prepend (tbuffer->msgs, g_memdup2 (&msg, sizeof (msg)));
return TRUE;
}
diff --unified --recursive --text --new-file --color glib-2.62.6.old/glib/gvariant.c glib-2.62.6.new/glib/gvariant.c
--- glib-2.62.6.old/glib/gvariant.c 2021-03-03 08:49:45.506494584 +0800
+++ glib-2.62.6.new/glib/gvariant.c 2021-03-03 16:32:33.266070197 +0800
@@ -33,6 +33,7 @@
#include <string.h>
+#include "gstrfuncsprivate.h"
/**
* SECTION:gvariant
@@ -725,7 +726,7 @@
g_variant_ref_sink (value);
return g_variant_new_from_children (G_VARIANT_TYPE_VARIANT,
- g_memdup (&value, sizeof value),
+ g_memdup2 (&value, sizeof value),
1, g_variant_is_trusted (value));
}
@@ -1229,7 +1230,7 @@
return NULL;
}
- data = g_memdup (elements, n_elements * element_size);
+ data = g_memdup2 (elements, n_elements * element_size);
value = g_variant_new_from_data (array_type, data,
n_elements * element_size,
FALSE, g_free, data);
@@ -1908,7 +1909,7 @@
if (length)
*length = size;
- return g_memdup (original, size + 1);
+ return g_memdup2 (original, size + 1);
}
/**
diff --unified --recursive --text --new-file --color glib-2.62.6.old/glib/gvarianttype.c glib-2.62.6.new/glib/gvarianttype.c
--- glib-2.62.6.old/glib/gvarianttype.c 2021-03-03 08:49:45.506494584 +0800
+++ glib-2.62.6.new/glib/gvarianttype.c 2021-03-03 16:32:33.266070197 +0800
@@ -28,6 +28,7 @@
#include <string.h>
+#include "gstrfuncsprivate.h"
/**
* SECTION:gvarianttype
@@ -1181,7 +1182,7 @@
g_assert (offset < sizeof buffer);
buffer[offset++] = ')';
- return (GVariantType *) g_memdup (buffer, offset);
+ return (GVariantType *) g_memdup2 (buffer, offset);
}
/**
diff --unified --recursive --text --new-file --color glib-2.62.6.old/glib/meson.build glib-2.62.6.new/glib/meson.build
--- glib-2.62.6.old/glib/meson.build 2021-03-03 08:49:45.506494584 +0800
+++ glib-2.62.6.new/glib/meson.build 2021-03-03 16:29:56.864596132 +0800
@@ -268,6 +268,7 @@
'gslist.c',
'gstdio.c',
'gstrfuncs.c',
+ 'gstrfuncsprivate.h',
'gstring.c',
'gstringchunk.c',
'gtestutils.c',
diff --unified --recursive --text --new-file --color glib-2.62.6.old/glib/tests/array-test.c glib-2.62.6.new/glib/tests/array-test.c
--- glib-2.62.6.old/glib/tests/array-test.c 2021-03-03 08:49:45.506494584 +0800
+++ glib-2.62.6.new/glib/tests/array-test.c 2021-03-03 16:32:33.266070197 +0800
@@ -29,6 +29,8 @@
#include <string.h>
#include "glib.h"
+#include "gstrfuncsprivate.h"
+
/* Test data to be passed to any function which calls g_array_new(), providing
* the parameters for that call. Most #GArray tests should be repeated for all
* possible values of #ArrayTestData. */
@@ -1642,7 +1644,7 @@
GByteArray *gbarray;
guint8 *data;
- data = g_memdup ("woooweeewow", 11);
+ data = g_memdup2 ("woooweeewow", 11);
gbarray = g_byte_array_new_take (data, 11);
g_assert (gbarray->data == data);
g_assert_cmpuint (gbarray->len, ==, 11);
diff --unified --recursive --text --new-file --color glib-2.62.6.old/glib/tests/option-context.c glib-2.62.6.new/glib/tests/option-context.c
--- glib-2.62.6.old/glib/tests/option-context.c 2021-03-03 08:49:45.523161247 +0800
+++ glib-2.62.6.new/glib/tests/option-context.c 2021-03-03 16:32:33.266070197 +0800
@@ -27,6 +27,8 @@
#include <string.h>
#include <locale.h>
+#include "gstrfuncsprivate.h"
+
static GOptionEntry main_entries[] = {
{ "main-switch", 0, 0,
G_OPTION_ARG_NONE, NULL,
@@ -256,7 +258,7 @@
static char **
copy_stringv (char **argv, int argc)
{
- return g_memdup (argv, sizeof (char *) * (argc + 1));
+ return g_memdup2 (argv, sizeof (char *) * (argc + 1));
}
static void
@@ -2323,7 +2325,7 @@
g_option_context_add_group (context, group);
argv = split_string ("program --test arg1 -f arg2 --group-test arg3 --frob arg4 -z arg5", &argc);
- orig_argv = g_memdup (argv, (argc + 1) * sizeof (char *));
+ orig_argv = g_memdup2 (argv, (argc + 1) * sizeof (char *));
retval = g_option_context_parse (context, &argc, &argv, &error);
diff --unified --recursive --text --new-file --color glib-2.62.6.old/glib/tests/strfuncs.c glib-2.62.6.new/glib/tests/strfuncs.c
--- glib-2.62.6.old/glib/tests/strfuncs.c 2021-03-03 08:49:45.529827914 +0800
+++ glib-2.62.6.new/glib/tests/strfuncs.c 2021-03-03 16:29:56.864596132 +0800
@@ -32,6 +32,8 @@
#include <string.h>
#include "glib.h"
+#include "gstrfuncsprivate.h"
+
#if defined (_MSC_VER) && (_MSC_VER <= 1800)
#define isnan(x) _isnan(x)
@@ -219,6 +221,26 @@
g_free (str_dup);
}
+/* Testing g_memdup2() function with various positive and negative cases */
+static void
+test_memdup2 (void)
+{
+ gchar *str_dup = NULL;
+ const gchar *str = "The quick brown fox jumps over the lazy dog";
+
+ /* Testing negative cases */
+ g_assert_null (g_memdup2 (NULL, 1024));
+ g_assert_null (g_memdup2 (str, 0));
+ g_assert_null (g_memdup2 (NULL, 0));
+
+ /* Testing normal usage cases */
+ str_dup = g_memdup2 (str, strlen (str) + 1);
+ g_assert_nonnull (str_dup);
+ g_assert_cmpstr (str, ==, str_dup);
+
+ g_free (str_dup);
+}
+
/* Testing g_strpcpy() function with various positive and negative cases */
static void
test_stpcpy (void)
@@ -2523,6 +2545,7 @@
g_test_add_func ("/strfuncs/has-prefix", test_has_prefix);
g_test_add_func ("/strfuncs/has-suffix", test_has_suffix);
g_test_add_func ("/strfuncs/memdup", test_memdup);
+ g_test_add_func ("/strfuncs/memdup2", test_memdup2);
g_test_add_func ("/strfuncs/stpcpy", test_stpcpy);
g_test_add_func ("/strfuncs/str_match_string", test_str_match_string);
g_test_add_func ("/strfuncs/str_tokenize_and_fold", test_str_tokenize_and_fold);
diff --unified --recursive --text --new-file --color glib-2.62.6.old/glib/tests/uri.c glib-2.62.6.new/glib/tests/uri.c
--- glib-2.62.6.old/glib/tests/uri.c 2021-03-03 08:49:45.529827914 +0800
+++ glib-2.62.6.new/glib/tests/uri.c 2021-03-03 16:34:51.847992046 +0800
@@ -81,7 +81,7 @@
* and would not occur in real life.
*/
{ "/:", NULL, "file:///:"},
- { "/?&=", NULL, "file:///%3F&="},
+ { "/?&=", NULL, "file:///%3F&="},
{ "/", "0123456789-", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
{ "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/"},
{ "/", "abcdefghijklmnopqrstuvwxyz", "file://abcdefghijklmnopqrstuvwxyz/"},
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gobject/gsignal.c glib-2.62.6.new/gobject/gsignal.c
--- glib-2.62.6.old/gobject/gsignal.c 2021-03-03 08:49:45.536494577 +0800
+++ glib-2.62.6.new/gobject/gsignal.c 2021-03-03 16:32:27.979461331 +0800
@@ -28,6 +28,7 @@
#include <signal.h>
#include "gsignal.h"
+#include "gstrfuncsprivate.h"
#include "gtype-private.h"
#include "gbsearcharray.h"
#include "gvaluecollector.h"
@@ -1730,7 +1731,7 @@
node->single_va_closure_is_valid = FALSE;
node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
node->n_params = n_params;
- node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
+ node->param_types = g_memdup2 (param_types, sizeof (GType) * n_params);
node->return_type = return_type;
node->class_closure_bsa = NULL;
if (accumulator)
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gobject/gtype.c glib-2.62.6.new/gobject/gtype.c
--- glib-2.62.6.old/gobject/gtype.c 2021-03-03 08:49:45.539827910 +0800
+++ glib-2.62.6.new/gobject/gtype.c 2021-03-03 16:32:27.979461331 +0800
@@ -33,6 +33,7 @@
#include "glib-private.h"
#include "gconstructor.h"
+#include "gstrfuncsprivate.h"
#ifdef G_OS_WIN32
#include <windows.h>
@@ -1470,7 +1471,7 @@
iholder->next = iface_node_get_holders_L (iface);
iface_node_set_holders_W (iface, iholder);
iholder->instance_type = NODE_TYPE (node);
- iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
+ iholder->info = info ? g_memdup2 (info, sizeof (*info)) : NULL;
iholder->plugin = plugin;
/* create an iface entry for this type */
@@ -1731,7 +1732,7 @@
INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
check_interface_info_I (iface, instance_type, &tmp_info);
- iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
+ iholder->info = g_memdup2 (&tmp_info, sizeof (tmp_info));
}
return iholder; /* we don't modify write lock upon returning NULL */
@@ -2016,10 +2017,10 @@
IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
if (pentry)
- vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
+ vtable = g_memdup2 (pentry->vtable, iface->data->iface.vtable_size);
}
if (!vtable)
- vtable = g_memdup (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
+ vtable = g_memdup2 (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
entry->vtable = vtable;
vtable->g_type = NODE_TYPE (iface);
vtable->g_instance_type = NODE_TYPE (node);
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gobject/gtypemodule.c glib-2.62.6.new/gobject/gtypemodule.c
--- glib-2.62.6.old/gobject/gtypemodule.c 2021-03-03 08:49:45.546494577 +0800
+++ glib-2.62.6.new/gobject/gtypemodule.c 2021-03-03 16:32:27.979461331 +0800
@@ -19,6 +19,7 @@
#include <stdlib.h>
+#include "gstrfuncsprivate.h"
#include "gtypeplugin.h"
#include "gtypemodule.h"
@@ -436,7 +437,7 @@
module_type_info->loaded = TRUE;
module_type_info->info = *type_info;
if (type_info->value_table)
- module_type_info->info.value_table = g_memdup (type_info->value_table,
+ module_type_info->info.value_table = g_memdup2 (type_info->value_table,
sizeof (GTypeValueTable));
return module_type_info->type;
diff --unified --recursive --text --new-file --color glib-2.62.6.old/gobject/tests/param.c glib-2.62.6.new/gobject/tests/param.c
--- glib-2.62.6.old/gobject/tests/param.c 2021-03-03 08:49:45.549827907 +0800
+++ glib-2.62.6.new/gobject/tests/param.c 2021-03-03 16:32:27.979461331 +0800
@@ -2,6 +2,8 @@
#include <glib-object.h>
#include <stdlib.h>
+#include "gstrfuncsprivate.h"
+
static void
test_param_value (void)
{
@@ -851,7 +853,7 @@
test_path = g_strdup_printf ("/param/implement/subprocess/%d-%d-%d-%d",
data.change_this_flag, data.change_this_type,
data.use_this_flag, data.use_this_type);
- test_data = g_memdup (&data, sizeof (TestParamImplementData));
+ test_data = g_memdup2 (&data, sizeof (TestParamImplementData));
g_test_add_data_func_full (test_path, test_data, test_param_implement_child, g_free);
g_free (test_path);
}