summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-04-28 17:31:33 +0200
committerThomas Haller <thaller@redhat.com>2020-05-04 12:47:11 +0200
commit93285a465fc9caae665974d57fce9b9d80d86ad6 (patch)
treeeb6ef5e1c1abe0f4b90d94802bb696b1450727ee
parent42aea87d51c09219950c626ddd9a835725886f4d (diff)
downloadNetworkManager-93285a465fc9caae665974d57fce9b9d80d86ad6.tar.gz
keyfile: refactor writing of G_TYPE_ARRAY list of unsigned integers
Keyfile handles GObject properties of type G_TYPE_ARRAY as a GArray of unsigned ints. That is correct, because all our properties of this GType happen to be of this kind. However, then the function was using nm_keyfile_plugin_kf_set_integer_list(), which only can handle signed integers. There was thus an assertion that all integers were non-negative. Which, probably was also correct, because NMSettingDcb would validate that all values of such kind are in fact positive. Anyway, that is an unexpected limitation (if not a bug). Fix that by handling the array as unsigned list of integers. Also, since glib doesn't provide an API for storing lists of unsigend integers, we have to implement our own. but that is no loss. We probably do it better anyway.
-rw-r--r--shared/nm-keyfile/nm-keyfile-utils.c22
-rw-r--r--shared/nm-keyfile/nm-keyfile-utils.h2
-rw-r--r--shared/nm-keyfile/nm-keyfile.c24
3 files changed, 31 insertions, 17 deletions
diff --git a/shared/nm-keyfile/nm-keyfile-utils.c b/shared/nm-keyfile/nm-keyfile-utils.c
index 627f529978..751ff23e4b 100644
--- a/shared/nm-keyfile/nm-keyfile-utils.c
+++ b/shared/nm-keyfile/nm-keyfile-utils.c
@@ -150,10 +150,30 @@ fcn_name (GKeyFile *kf, \
key_file_set_fcn (kf, alias ?: group, key, list, length); \
}
-DEFINE_KF_LIST_WRAPPER_SET (nm_keyfile_plugin_kf_set_integer_list, int *, g_key_file_set_integer_list);
DEFINE_KF_LIST_WRAPPER_SET (nm_keyfile_plugin_kf_set_string_list, const char*const*, g_key_file_set_string_list);
void
+nm_keyfile_plugin_kf_set_integer_list_uint (GKeyFile *kf,
+ const char *group,
+ const char *key,
+ const guint *data,
+ gsize length)
+{
+ nm_auto_str_buf NMStrBuf strbuf = { };
+ gsize i;
+
+ g_return_if_fail (kf);
+ g_return_if_fail (!length || data);
+ g_return_if_fail (group && group[0]);
+ g_return_if_fail (key && key[0]);
+
+ nm_str_buf_init (&strbuf, length * 4u + 2u, FALSE);
+ for (i = 0; i < length; i++)
+ nm_str_buf_append_printf (&strbuf, "%u;", data[i]);
+ nm_keyfile_plugin_kf_set_value (kf, group, key, nm_str_buf_get_str (&strbuf));
+}
+
+void
nm_keyfile_plugin_kf_set_integer_list_uint8 (GKeyFile *kf,
const char *group,
const char *key,
diff --git a/shared/nm-keyfile/nm-keyfile-utils.h b/shared/nm-keyfile/nm-keyfile-utils.h
index 719c32c2fc..ab9f44dc7f 100644
--- a/shared/nm-keyfile/nm-keyfile-utils.h
+++ b/shared/nm-keyfile/nm-keyfile-utils.h
@@ -26,7 +26,7 @@ gboolean nm_keyfile_plugin_kf_get_boolean (GKeyFile *kf, const char *group,
char *nm_keyfile_plugin_kf_get_value (GKeyFile *kf, const char *group, const char *key, GError **error);
void nm_keyfile_plugin_kf_set_integer_list_uint8 (GKeyFile *kf, const char *group, const char *key, const guint8 *list, gsize length);
-void nm_keyfile_plugin_kf_set_integer_list (GKeyFile *kf, const char *group, const char *key, int *list, gsize length);
+void nm_keyfile_plugin_kf_set_integer_list_uint (GKeyFile *kf, const char *group, const char *key, const guint *list, gsize length);
void nm_keyfile_plugin_kf_set_string_list (GKeyFile *kf, const char *group, const char *key, const char *const*list, gsize length);
void nm_keyfile_plugin_kf_set_string (GKeyFile *kf, const char *group, const char *key, const char *value);
diff --git a/shared/nm-keyfile/nm-keyfile.c b/shared/nm-keyfile/nm-keyfile.c
index 879b1b004f..474377bfcf 100644
--- a/shared/nm-keyfile/nm-keyfile.c
+++ b/shared/nm-keyfile/nm-keyfile.c
@@ -1851,25 +1851,19 @@ write_array_of_uint (GKeyFile *file,
const GValue *value)
{
GArray *array;
- guint i;
- gs_free int *tmp_array = NULL;
- array = (GArray *) g_value_get_boxed (value);
- if (!array || !array->len)
- return;
+ array = g_value_get_boxed (value);
- g_return_if_fail (g_array_get_element_size (array) == sizeof (guint));
-
- tmp_array = g_new (int, array->len);
- for (i = 0; i < array->len; i++) {
- guint v = g_array_index (array, guint, i);
+ nm_assert (!array || g_array_get_element_size (array) == sizeof (guint));
- if (v > G_MAXINT)
- g_return_if_reached ();
- tmp_array[i] = (int) v;
- }
+ if (!array || !array->len)
+ return;
- nm_keyfile_plugin_kf_set_integer_list (file, nm_setting_get_name (setting), key, tmp_array, array->len);
+ nm_keyfile_plugin_kf_set_integer_list_uint (file,
+ nm_setting_get_name (setting),
+ key,
+ (const guint *) array->data,
+ array->len);
}
static void