summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-04-28 17:29:37 +0200
committerThomas Haller <thaller@redhat.com>2020-05-04 12:47:11 +0200
commit42aea87d51c09219950c626ddd9a835725886f4d (patch)
tree25336bc9bf0e600fe44b6c61b988cc8981568a81
parentff84211cf69b52c6afcd8bd176e529a92c3ea65f (diff)
downloadNetworkManager-42aea87d51c09219950c626ddd9a835725886f4d.tar.gz
keyfile: use NMStrBuf in nm_keyfile_plugin_kf_set_integer_list_uint8()
Previously, we were preallocating a string buffer of fixed size. For guint8 we reserved 3 characters per number, which is sufficient. However, it is not obviously sufficient. NMStrBuf would grow as needed. Next, I will add nm_keyfile_plugin_kf_set_integer_list_uint(), where it is more unclear how large the string can be at most. To avoid that question from the start, it will use NMStrBuf. To keep the implementations similar, use NMStrBuf also in this case.
-rw-r--r--shared/nm-keyfile/nm-keyfile-utils.c13
-rw-r--r--shared/nm-keyfile/nm-keyfile-utils.h11
2 files changed, 9 insertions, 15 deletions
diff --git a/shared/nm-keyfile/nm-keyfile-utils.c b/shared/nm-keyfile/nm-keyfile-utils.c
index 6356f8c444..627f529978 100644
--- a/shared/nm-keyfile/nm-keyfile-utils.c
+++ b/shared/nm-keyfile/nm-keyfile-utils.c
@@ -9,6 +9,8 @@
#include <stdlib.h>
+#include "nm-glib-aux/nm-str-buf.h"
+
#include "nm-keyfile-internal.h"
#include "nm-setting-wired.h"
#include "nm-setting-wireless.h"
@@ -158,21 +160,18 @@ nm_keyfile_plugin_kf_set_integer_list_uint8 (GKeyFile *kf,
const guint8 *data,
gsize length)
{
+ nm_auto_str_buf NMStrBuf strbuf = { };
gsize i;
- gsize l = length * 4 + 2;
- gs_free char *value = g_malloc (l);
- char *s = value;
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]);
- value[0] = '\0';
+ nm_str_buf_init (&strbuf, length * 4u + 2u, FALSE);
for (i = 0; i < length; i++)
- nm_utils_strbuf_append (&s, &l, "%d;", (int) data[i]);
- nm_assert (l > 0);
- nm_keyfile_plugin_kf_set_value (kf, group, key, value);
+ nm_str_buf_append_printf (&strbuf, "%u;", (guint) data[i]);
+ nm_keyfile_plugin_kf_set_value (kf, group, key, nm_str_buf_get_str (&strbuf));
}
#define DEFINE_KF_WRAPPER_GET(fcn_name, get_ctype, key_file_get_fcn) \
diff --git a/shared/nm-keyfile/nm-keyfile-utils.h b/shared/nm-keyfile/nm-keyfile-utils.h
index 98b0129a59..719c32c2fc 100644
--- a/shared/nm-keyfile/nm-keyfile-utils.h
+++ b/shared/nm-keyfile/nm-keyfile-utils.h
@@ -19,20 +19,15 @@ const char *nm_keyfile_plugin_get_setting_name_for_alias (const char *alias);
/*****************************************************************************/
-void nm_keyfile_plugin_kf_set_integer_list_uint8 (GKeyFile *kf,
- const char *group,
- const char *key,
- const guint8 *list,
- gsize length);
-
int *nm_keyfile_plugin_kf_get_integer_list (GKeyFile *kf, const char *group, const char *key, gsize *out_length, GError **error);
char **nm_keyfile_plugin_kf_get_string_list (GKeyFile *kf, const char *group, const char *key, gsize *out_length, GError **error);
char *nm_keyfile_plugin_kf_get_string (GKeyFile *kf, const char *group, const char *key, GError **error);
gboolean nm_keyfile_plugin_kf_get_boolean (GKeyFile *kf, const char *group, const char *key, GError **error);
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 (GKeyFile *kf, const char *group, const char *key, int *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_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_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);
void nm_keyfile_plugin_kf_set_boolean (GKeyFile *kf, const char *group, const char *key, gboolean value);