all: merge branch 'th/strv-cleanup'

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1807
This commit is contained in:
Thomas Haller 2023-12-01 14:00:35 +01:00
commit 54f963ee5b
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
13 changed files with 56 additions and 64 deletions

View file

@ -103,7 +103,7 @@ nm_vpn_editor_plugin_get_service_add_details(NMVpnEditorPlugin *plugin, const ch
if (vt.fcn_get_service_add_details)
details = vt.fcn_get_service_add_details(plugin, service_name);
if (!details)
return g_new0(char *, 1);
return nm_strv_empty_new();
return details;
}

View file

@ -190,7 +190,7 @@ next:
if (arr && arr->len > 0)
nameservers_new = nm_strv_dup((char **) arr->pdata, arr->len, FALSE);
else
nameservers_new = g_new0(char *, 1);
nameservers_new = nm_strv_empty_new();
}
nm_assert(nameservers_new);
}

View file

@ -6146,7 +6146,7 @@ get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
switch (prop_id) {
case PROP_DNS:
g_value_take_boxed(value, _nm_utils_ptrarray_to_strv(priv->dns));
g_value_take_boxed(value, nm_strv_ptrarray_to_strv(priv->dns));
break;
case PROP_ADDRESSES:
g_value_take_boxed(value,

View file

@ -1384,8 +1384,7 @@ get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
break;
case NM_TEAM_ATTRIBUTE_MASTER_RUNNER_TX_HASH:
v_ptrarr = priv->team_setting->d.master.runner_tx_hash;
g_value_take_boxed(value,
v_ptrarr ? _nm_utils_ptrarray_to_strv((GPtrArray *) v_ptrarr) : NULL);
g_value_take_boxed(value, nm_strv_ptrarray_to_strv_full(v_ptrarr, FALSE));
break;
case NM_TEAM_ATTRIBUTE_LINK_WATCHERS:
g_value_take_boxed(value,

View file

@ -897,42 +897,6 @@ _nm_utils_slist_to_strv(const GSList *slist, gboolean deep_copy)
return strv;
}
GPtrArray *
nm_strv_to_ptrarray(char **strv)
{
GPtrArray *ptrarray;
gsize i, l;
l = NM_PTRARRAY_LEN(strv);
ptrarray = g_ptr_array_new_full(l, g_free);
if (strv) {
for (i = 0; strv[i]; i++)
g_ptr_array_add(ptrarray, g_strdup(strv[i]));
}
return ptrarray;
}
char **
_nm_utils_ptrarray_to_strv(const GPtrArray *ptrarray)
{
char **strv;
guint i;
if (!ptrarray)
return g_new0(char *, 1);
strv = g_new(char *, ptrarray->len + 1);
for (i = 0; i < ptrarray->len; i++)
strv[i] = g_strdup(ptrarray->pdata[i]);
strv[i] = NULL;
return strv;
}
/*****************************************************************************/
static gboolean

View file

@ -728,7 +728,7 @@ nm_vpn_plugin_info_list_get_service_types(GSList *list,
if (l->len <= 0) {
g_ptr_array_free(l, TRUE);
return g_new0(char *, 1);
return nm_strv_empty_new();
}
/* sort the result and remove duplicates. */

View file

@ -347,9 +347,6 @@ GPtrArray *_nm_utils_copy_object_array(const GPtrArray *array);
GSList *nm_strv_to_gslist(char **strv, gboolean deep_copy);
char **_nm_utils_slist_to_strv(const GSList *slist, gboolean deep_copy);
GPtrArray *nm_strv_to_ptrarray(char **strv);
char **_nm_utils_ptrarray_to_strv(const GPtrArray *ptrarray);
gboolean _nm_utils_check_file(const char *filename,
gint64 check_owner,
NMUtilsCheckFilePredicate check_file,

View file

@ -1937,7 +1937,7 @@ nm_utils_strsplit_quoted(const char *str)
}
if (!arr)
return g_new0(char *, 1);
return nm_strv_empty_new();
/* We want to return an optimally sized strv array, with no excess
* memory allocated. Hence, clone once more. */
@ -3580,6 +3580,9 @@ nm_strv_make_deep_copied_n(const char **strv, gsize len)
* the returned array must be freed with g_strfreev(). Otherwise, the
* strings themself are not copied. You must take care of who owns the
* strings yourself.
* @preserved_empty: affects how to handle if the strv array is empty (length 0).
* If TRUE, results in a non-NULL, empty, allocated strv array. If FALSE,
* returns NULL instead of an empty strv array.
*
* Like g_strdupv(), with two differences:
*
@ -3598,7 +3601,10 @@ nm_strv_make_deep_copied_n(const char **strv, gsize len)
* cloned or not.
*/
char **
_nm_strv_dup(const char *const *strv, gssize len, gboolean deep_copied)
_nm_strv_dup_full(const char *const *strv,
gssize len,
gboolean deep_copied,
gboolean preserve_empty)
{
gsize i, l;
char **v;
@ -3607,13 +3613,16 @@ _nm_strv_dup(const char *const *strv, gssize len, gboolean deep_copied)
l = NM_PTRARRAY_LEN(strv);
else
l = len;
if (l == 0) {
/* this function never returns an empty strv array. If you
* need that, handle it yourself. */
if (l == 0 && !preserve_empty) {
/* An empty strv array is not returned (as requested by
* !preserved_empty). Instead, return NULL. */
return NULL;
}
v = g_new(char *, l + 1);
nm_assert(l < G_MAXSIZE);
v = g_new(char *, l + 1u);
for (i = 0; i < l; i++) {
if (G_UNLIKELY(!strv[i])) {
/* NULL strings are not allowed. Clear the remainder of the array

View file

@ -332,9 +332,10 @@ gboolean nm_utils_memeqzero(gconstpointer data, gsize length);
extern const void *const _NM_PTRARRAY_EMPTY[1];
#define NM_PTRARRAY_EMPTY(type) ((type const *) _NM_PTRARRAY_EMPTY)
#define NM_STRV_EMPTY() ((char **) _NM_PTRARRAY_EMPTY)
#define NM_STRV_EMPTY_CC() NM_PTRARRAY_EMPTY(const char *)
#define NM_PTRARRAY_EMPTY(type) ((type const *) _NM_PTRARRAY_EMPTY)
#define NM_STRV_EMPTY() ((char **) _NM_PTRARRAY_EMPTY)
#define NM_STRV_EMPTY_CC() NM_PTRARRAY_EMPTY(const char *)
#define NM_PTRARRAY_EMPTY_NEW(type) (g_new0(type, 1))
static inline void
nm_strbuf_init(char *buf, gsize len, char **p_buf_ptr, gsize *p_buf_len)
@ -1850,6 +1851,8 @@ int nm_utils_hashtable_cmp(const GHashTable *a,
GCompareDataFunc cmp_values,
gpointer user_data);
#define nm_strv_empty_new() NM_PTRARRAY_EMPTY_NEW(char *)
char **nm_strv_make_deep_copied(const char **strv);
char **nm_strv_make_deep_copied_n(const char **strv, gsize len);
@ -1857,13 +1860,18 @@ char **nm_strv_make_deep_copied_n(const char **strv, gsize len);
static inline char **
nm_strv_make_deep_copied_nonnull(const char **strv)
{
return nm_strv_make_deep_copied(strv) ?: g_new0(char *, 1);
return nm_strv_make_deep_copied(strv) ?: nm_strv_empty_new();
}
char **_nm_strv_dup(const char *const *strv, gssize len, gboolean deep_copied);
char **_nm_strv_dup_full(const char *const *strv,
gssize len,
gboolean deep_copied,
gboolean preserve_empty);
#define nm_strv_dup(strv, len, deep_copied) \
_nm_strv_dup(NM_CAST_STRV_CC(strv), (len), (deep_copied))
#define nm_strv_dup_full(strv, len, deep_copied, preserve_empty) \
_nm_strv_dup_full(NM_CAST_STRV_CC(strv), (len), (deep_copied), (preserve_empty))
#define nm_strv_dup(strv, len, deep_copied) nm_strv_dup_full((strv), (len), (deep_copied), FALSE)
const char **_nm_strv_dup_packed(const char *const *strv, gssize len);
@ -2528,6 +2536,21 @@ nm_strv_ptrarray_get_unsafe(GPtrArray *arr, guint *out_len)
return (const char *const *) arr->pdata;
}
static inline char **
nm_strv_ptrarray_to_strv_full(const GPtrArray *a, gboolean not_null)
{
if (!a)
return not_null ? nm_strv_empty_new() : NULL;
return nm_strv_dup_full((const char *const *) a->pdata, a->len, TRUE, TRUE);
}
static inline char **
nm_strv_ptrarray_to_strv(const GPtrArray *a)
{
/* Returns never NULL ("not_null"!) */
return nm_strv_ptrarray_to_strv_full(a, TRUE);
}
static inline GPtrArray *
nm_strv_ptrarray_clone(const GPtrArray *src, gboolean null_if_empty)
{
@ -3107,7 +3130,7 @@ nm_strvarray_get_strv_full_dup(const GArray *arr,
{
if (!arr) {
NM_SET_OUT(length, 0);
return not_null ? g_new0(char *, 1) : NULL;
return not_null ? nm_strv_empty_new() : NULL;
}
nm_assert(sizeof(char *) == g_array_get_element_size((GArray *) arr));
@ -3116,7 +3139,7 @@ nm_strvarray_get_strv_full_dup(const GArray *arr,
if (arr->len == 0) {
if (preserve_empty || not_null)
return g_new0(char *, 1);
return nm_strv_empty_new();
return NULL;
}

View file

@ -1228,7 +1228,7 @@ nmtst_rand_perm_strv(const char *const *strv)
/* this returns a (scrambled) SHALLOW copy of the strv array! */
n = NM_PTRARRAY_LEN(strv);
res = (const char **) (nm_strv_dup(strv, n, FALSE) ?: g_new0(char *, 1));
res = (const char **) (nm_strv_dup(strv, n, FALSE) ?: nm_strv_empty_new());
nmtst_rand_perm(NULL, res, res, sizeof(char *), n);
return res;
}

View file

@ -450,7 +450,7 @@ _strv_cmp_fuzz_input(const char *const *in,
if (l < 0)
ss = g_strdupv((char **) in);
else if (l == 0) {
ss = nmtst_get_rand_bool() ? NULL : g_new0(char *, 1);
ss = nmtst_get_rand_bool() ? NULL : nm_strv_empty_new();
} else {
ss = nm_memdup(in, sizeof(const char *) * l);
for (i = 0; i < (gsize) l; i++)

View file

@ -497,7 +497,7 @@ nmc_string_to_arg_array(const char *line,
arr0 = nm_strsplit_set(line ?: "", delim ?: " \t");
if (!arr0)
arr = g_new0(char *, 1);
arr = nm_strv_empty_new();
else
arr = g_strdupv((char **) arr0);

View file

@ -194,7 +194,7 @@ nmt_address_list_set_property(GObject *object,
g_strfreev(priv->strings);
priv->strings = g_value_dup_boxed(value);
if (!priv->strings)
priv->strings = g_new0(char *, 1);
priv->strings = nm_strv_empty_new();
nmt_widget_list_set_length(NMT_WIDGET_LIST(object), g_strv_length(priv->strings));
break;
default: