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

View file

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

View file

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