summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2023-11-17 09:10:52 +0100
committerThomas Haller <thaller@redhat.com>2023-11-30 15:53:21 +0100
commit541979d09838cabc9038feef898fef2213449dd9 (patch)
tree22733391a6d3a5c6817fc4a4b661f66d96c546d8
parent503a76f6049b2405dc7773e03789bd6aa17d8b44 (diff)
glib-aux: add nm_strv_dup_full() helper to preserve empty strv
-rw-r--r--src/libnm-glib-aux/nm-shared-utils.c19
-rw-r--r--src/libnm-glib-aux/nm-shared-utils.h11
2 files changed, 22 insertions, 8 deletions
diff --git a/src/libnm-glib-aux/nm-shared-utils.c b/src/libnm-glib-aux/nm-shared-utils.c
index 40bb59200d..7db45e1b90 100644
--- a/src/libnm-glib-aux/nm-shared-utils.c
+++ b/src/libnm-glib-aux/nm-shared-utils.c
@@ -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
diff --git a/src/libnm-glib-aux/nm-shared-utils.h b/src/libnm-glib-aux/nm-shared-utils.h
index 11c87bec8f..74d7b1b921 100644
--- a/src/libnm-glib-aux/nm-shared-utils.h
+++ b/src/libnm-glib-aux/nm-shared-utils.h
@@ -1863,10 +1863,15 @@ nm_strv_make_deep_copied_nonnull(const char **strv)
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);