[libvirt] [PATCH 1/2] virTypedParamsSerialize: minor fixes

Marc Hartmayer mhartmay at linux.vnet.ibm.com
Wed Apr 25 15:55:37 UTC 2018


1. Don't allocate if there is nothing that needs to be
   allocated. Especially as the result of calling calloc(0, ...) is
   implementation-defined.
2. Update the length @remote_params_len only if the related
   @remote_params_val has also been set.

Signed-off-by: Marc Hartmayer <mhartmay at linux.vnet.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy at linux.ibm.com>
---
 src/util/virtypedparam.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c
index 2452628cdbcd..10fd28baa65c 100644
--- a/src/util/virtypedparam.c
+++ b/src/util/virtypedparam.c
@@ -1500,10 +1500,10 @@ virTypedParamsSerialize(virTypedParameterPtr params,
     size_t i;
     size_t j;
     int rv = -1;
-    virTypedParameterRemotePtr params_val;
+    virTypedParameterRemotePtr params_val = NULL;
+    int params_len = nparams;
 
-    *remote_params_len = nparams;
-    if (VIR_ALLOC_N(params_val, nparams) < 0)
+    if (nparams && VIR_ALLOC_N(params_val, nparams) < 0)
         goto cleanup;
 
     for (i = 0, j = 0; i < nparams; ++i) {
@@ -1515,7 +1515,7 @@ virTypedParamsSerialize(virTypedParameterPtr params,
         if (!param->type ||
             (!(flags & VIR_TYPED_PARAM_STRING_OKAY) &&
              param->type == VIR_TYPED_PARAM_STRING)) {
-            --*remote_params_len;
+            --params_len;
             continue;
         }
 
@@ -1556,6 +1556,7 @@ virTypedParamsSerialize(virTypedParameterPtr params,
     }
 
     *remote_params_val = params_val;
+    *remote_params_len = params_len;
     params_val = NULL;
     rv = 0;
 
-- 
2.13.4




More information about the libvir-list mailing list