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.
This commit is contained in:
Thomas Haller 2020-04-28 17:31:33 +02:00
parent 42aea87d51
commit 93285a465f
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
3 changed files with 31 additions and 17 deletions

View file

@ -150,9 +150,29 @@ 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,

View file

@ -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);

View file

@ -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);
array = g_value_get_boxed (value);
nm_assert (!array || g_array_get_element_size (array) == sizeof (guint));
if (!array || !array->len)
return;
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);
if (v > G_MAXINT)
g_return_if_reached ();
tmp_array[i] = (int) v;
}
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