From 3c846baa830ed81bdeade774d88cf0393c5a242d Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 31 Jul 2020 16:21:33 +0200 Subject: [PATCH] shared: add nm_strv_ptrarray_get_unsafe() helper It's called "unsafe" because the returned pointer array is not NULL terminated. This is due to a limitation of GPtrArray which doesn't support that. If you want a NULL terminated strv array, use a GArray based API, like nm_strvarray_*(). --- shared/nm-glib-aux/nm-shared-utils.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h index d44321d34e..7d7ed16fe7 100644 --- a/shared/nm-glib-aux/nm-shared-utils.h +++ b/shared/nm-glib-aux/nm-shared-utils.h @@ -1821,6 +1821,23 @@ nm_strv_ptrarray_ensure (GPtrArray **p_arr) return *p_arr; } +static inline const char *const* +nm_strv_ptrarray_get_unsafe (GPtrArray *arr, + guint *out_len) +{ + /* warning: the GPtrArray is not NULL terminated. So, it + * isn't really a strv array (sorry the misnomer). That's why + * the function is potentially "unsafe" and you must provide a + * out_len parameter. */ + if ( !arr + || arr->len == 0) { + *out_len = 0; + return NULL; + } + *out_len = arr->len; + return (const char *const*) arr->pdata; +} + static inline GPtrArray * nm_strv_ptrarray_clone (const GPtrArray *src, gboolean null_if_empty) {