]> xenbits.xensource.com Git - libvirt.git/commitdiff
virTypedParamsGetStringList: Refactor and adjust docs
authorPeter Krempa <pkrempa@redhat.com>
Fri, 27 Sep 2024 09:15:06 +0000 (11:15 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 1 Oct 2024 10:57:02 +0000 (12:57 +0200)
Use automatic freeing, declare one variable per line and return early
when possible. As this is an internal helper there's no need to check
that the caller passed non-NULL @values.

Modify the documentation to be accurate and warn callers to not free the
strings just the array.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
src/util/virtypedparam.c

index 634385aa977a21798d1dafdacf40baa9a01523a7..86fbaf5e9d4f2eb667d4a99cb6cec7753eb6d394 100644 (file)
@@ -432,13 +432,13 @@ virTypedParamsFilter(virTypedParameterPtr params,
  * @values: array of returned values
  *
  * Finds all parameters with desired @name within @params and
- * store their values into @values. The @values array is self
- * allocated and its length is stored into @picked. When no
- * longer needed, caller should free the returned array, but not
- * the items since they are taken from @params array.
+ * store their values into @values.
  *
- * Returns amount of strings in @values array on success,
- * -1 otherwise.
+ * Important: The strings in the returned string list @values are borrowed from
+ * @params and thus caller must free only the pointer returned as @values, but
+ * not the contents.
+ *
+ * Returns amount of strings in @values array on success.
  */
 int
 virTypedParamsGetStringList(virTypedParameterPtr params,
@@ -446,31 +446,26 @@ virTypedParamsGetStringList(virTypedParameterPtr params,
                             const char *name,
                             const char ***values)
 {
-    size_t i, n;
+    size_t i;
+    size_t n = 0;
     size_t nfiltered;
-    virTypedParameterPtr *filtered = NULL;
+    g_autofree virTypedParameterPtr *filtered = NULL;
 
-    virCheckNonNullArgGoto(values, error);
     *values = NULL;
 
     nfiltered = virTypedParamsFilter(params, nparams, name, &filtered);
 
-    if (nfiltered)
-        *values = g_new0(const char *, nfiltered);
+    if (nfiltered == 0)
+        return 0;
+
+    *values = g_new0(const char *, nfiltered);
 
-    for (n = 0, i = 0; i < nfiltered; i++) {
+    for (i = 0; i < nfiltered; i++) {
         if (filtered[i]->type == VIR_TYPED_PARAM_STRING)
             (*values)[n++] = filtered[i]->value.s;
     }
 
-    VIR_FREE(filtered);
     return n;
-
- error:
-    if (values)
-        VIR_FREE(*values);
-    VIR_FREE(filtered);
-    return -1;
 }