]> xenbits.xensource.com Git - libvirt.git/commitdiff
virTypedParamsGetStringList: Ensure that returned array is NULL if there are no match...
authorPeter Krempa <pkrempa@redhat.com>
Fri, 27 Sep 2024 11:01:22 +0000 (13:01 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 1 Oct 2024 10:57:02 +0000 (12:57 +0200)
'virTypedParamsGetStringList' fills the returned array only with string
parameters with matching name. The filtering code though leaves the
possibility that all items are filtered out but the return array is
still (over)allocated.

Since 'virTypedParamsFilter()' now also allows filtering by type we can
move the filtering there ensuring that we always allocate the right
number of elements and more importantly the returned array will be NULL
if none elements are present.

Rework the code and adjust docs.

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

index a080d7ba0f2a2a38323042aa687e1105b012634d..564cb81acc223a143eb7661aa4b3fc11277b33d0 100644 (file)
@@ -439,7 +439,8 @@ virTypedParamsFilter(virTypedParameterPtr params,
  * @values: array of returned values
  *
  * Finds all parameters with desired @name within @params and
- * store their values into @values.
+ * store their values into @values. If none of the @params are strings named
+ * @name the returned @values will be NULL.
  *
  * 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
@@ -454,13 +455,12 @@ virTypedParamsGetStringList(virTypedParameterPtr params,
                             const char ***values)
 {
     size_t i;
-    size_t n = 0;
     size_t nfiltered;
     g_autofree virTypedParameterPtr *filtered = NULL;
 
     *values = NULL;
 
-    nfiltered = virTypedParamsFilter(params, nparams, name, 0, &filtered);
+    nfiltered = virTypedParamsFilter(params, nparams, name, VIR_TYPED_PARAM_STRING, &filtered);
 
     if (nfiltered == 0)
         return 0;
@@ -468,11 +468,10 @@ virTypedParamsGetStringList(virTypedParameterPtr params,
     *values = g_new0(const char *, nfiltered);
 
     for (i = 0; i < nfiltered; i++) {
-        if (filtered[i]->type == VIR_TYPED_PARAM_STRING)
-            (*values)[n++] = filtered[i]->value.s;
+        (*values)[i] = filtered[i]->value.s;
     }
 
-    return n;
+    return nfiltered;
 }