Blame SOURCES/0004-Clear-typed-parameter-elements-as-well-as-array.patch

252468
From 88cf0236e1b98b00bcaa5762f9207b99fb98e084 Mon Sep 17 00:00:00 2001
252468
From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= <berrange@redhat.com>
252468
Date: Mon, 6 Aug 2018 17:11:57 +0100
252468
Subject: [PATCH] Clear typed parameter elements as well as array
252468
MIME-Version: 1.0
252468
Content-Type: text/plain; charset=UTF-8
252468
Content-Transfer-Encoding: 8bit
252468
252468
Some typed parameter elements may contain strings that need to be
252468
freed.
252468
252468
We must ensure that the parameter array has dup'd the strings
252468
rather than borrowing a pointer.
252468
252468
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
252468
(cherry picked from commit 54f0d2c64021e6e143f40185f0181cf5d5a51f53)
252468
---
252468
 lib/Sys/Virt.xs | 124 ++++++++++++++++++++++++++----------------------
252468
 1 file changed, 66 insertions(+), 58 deletions(-)
252468
252468
diff --git a/lib/Sys/Virt.xs b/lib/Sys/Virt.xs
252468
index 3d8eb9b..6c2bf48 100644
252468
--- a/lib/Sys/Virt.xs
252468
+++ b/lib/Sys/Virt.xs
252468
@@ -86,7 +86,6 @@ virt_newSVull(unsigned long long val) {
252468
 }
252468
 
252468
 
252468
-
252468
 static void
252468
 ignoreVirErrorFunc(void * userData, virErrorPtr error) {
252468
   /* Do nothing */
252468
@@ -263,7 +262,8 @@ vir_typed_param_from_hv(HV *newparams, virTypedParameterPtr params, int nparams)
252468
 
252468
         case VIR_TYPED_PARAM_STRING:
252468
             ptr = SvPV(*val, len);
252468
-            params[i].value.s = (char *)ptr;
252468
+            if (!(params[i].value.s = strdup((char *)ptr)))
252468
+                abort();
252468
             break;
252468
         }
252468
     }
252468
@@ -300,7 +300,8 @@ vir_typed_param_add_string_list_from_hv(HV *newparams,
252468
       localparams[*nparams + i].field[VIR_TYPED_PARAM_FIELD_LENGTH - 1] = '\0';
252468
 
252468
       localparams[*nparams + i].type = VIR_TYPED_PARAM_STRING;
252468
-      localparams[*nparams + i].value.s = ptr;
252468
+      if (!(localparams[*nparams + i].value.s = strdup(ptr)))
252468
+	  abort();
252468
     }
252468
 
252468
     *params = localparams;
252468
@@ -308,6 +309,14 @@ vir_typed_param_add_string_list_from_hv(HV *newparams,
252468
 }
252468
 
252468
 
252468
+static void
252468
+vir_typed_param_safe_free(virTypedParameterPtr params, int nparams)
252468
+{
252468
+    virTypedParamsClear(params, nparams);
252468
+    Safefree(params);
252468
+}
252468
+
252468
+
252468
 static int
252468
 _domain_event_lifecycle_callback(virConnectPtr con,
252468
                                  virDomainPtr dom,
252468
@@ -2411,7 +2420,7 @@ get_node_sev_info(conn, flags=0)
252468
       }
252468
 
252468
       RETVAL = vir_typed_param_to_hv(params, nparams);
252468
-      free(params);
252468
+      virTypedParamsFree(params, nparams);
252468
   OUTPUT:
252468
       RETVAL
252468
 
252468
@@ -2651,12 +2660,12 @@ get_node_memory_parameters(conn, flags=0)
252468
       Newx(params, nparams, virTypedParameter);
252468
 
252468
       if (virNodeGetMemoryParameters(conn, params, &nparams, flags) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
 
252468
       RETVAL = vir_typed_param_to_hv(params, nparams);
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
   OUTPUT:
252468
       RETVAL
252468
 
252468
@@ -2677,17 +2686,17 @@ set_node_memory_parameters(conn, newparams, flags=0)
252468
       Newx(params, nparams, virTypedParameter);
252468
 
252468
       if (virNodeGetMemoryParameters(conn, params, &nparams, 0) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
 
252468
       nparams = vir_typed_param_from_hv(newparams, params, nparams);
252468
 
252468
       if (virNodeSetMemoryParameters(conn, params, nparams, flags) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
 
252468
 
252468
 
252468
@@ -4562,13 +4571,12 @@ get_job_stats(dom, flags=0)
252468
       SV *typeSv;
252468
     PPCODE:
252468
       if (virDomainGetJobStats(dom, &type, &params, &nparams, flags) < 0) {
252468
-          Safefree(params);
252468
           _croak_error();
252468
       }
252468
 
252468
       typeSv = newSViv(type);
252468
       paramsHv = vir_typed_param_to_hv(params, nparams);
252468
-      free(params);
252468
+      virTypedParamsFree(params, nparams);
252468
 
252468
       EXTEND(SP, 2);
252468
       PUSHs(newRV_noinc((SV*)typeSv));
252468
@@ -4676,11 +4684,11 @@ block_copy(dom, path, destxml, newparams, flags=0)
252468
       nparams = vir_typed_param_from_hv(newparams, params, nparams);
252468
 
252468
       if (virDomainBlockCopy(dom, path, destxml, params, nparams, flags) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
 
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
 
252468
 
252468
 void
252468
@@ -4741,17 +4749,17 @@ get_scheduler_parameters(dom, flags=0)
252468
       Newx(params, nparams, virTypedParameter);
252468
       if (flags) {
252468
           if (virDomainGetSchedulerParametersFlags(dom, params, &nparams, flags) < 0) {
252468
-              Safefree(params);
252468
+              vir_typed_param_safe_free(params, nparams);
252468
               _croak_error();
252468
           }
252468
       } else {
252468
           if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0) {
252468
-              Safefree(params);
252468
+              vir_typed_param_safe_free(params, nparams);
252468
               _croak_error();
252468
           }
252468
       }
252468
       RETVAL = vir_typed_param_to_hv(params, nparams);
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
   OUTPUT:
252468
       RETVAL
252468
 
252468
@@ -4772,22 +4780,22 @@ set_scheduler_parameters(dom, newparams, flags=0)
252468
       free(type);
252468
       Newx(params, nparams, virTypedParameter);
252468
       if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
       nparams = vir_typed_param_from_hv(newparams, params, nparams);
252468
       if (flags) {
252468
           if (virDomainSetSchedulerParametersFlags(dom, params, nparams, flags) < 0) {
252468
-              Safefree(params);
252468
+              vir_typed_param_safe_free(params, nparams);
252468
               _croak_error();
252468
           }
252468
       } else {
252468
           if (virDomainSetSchedulerParameters(dom, params, nparams) < 0) {
252468
-              Safefree(params);
252468
+              vir_typed_param_safe_free(params, nparams);
252468
               _croak_error();
252468
           }
252468
       }
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
 
252468
 
252468
 HV *
252468
@@ -4805,12 +4813,12 @@ get_memory_parameters(dom, flags=0)
252468
       Newx(params, nparams, virTypedParameter);
252468
 
252468
       if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
 
252468
       RETVAL = vir_typed_param_to_hv(params, nparams);
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
   OUTPUT:
252468
       RETVAL
252468
 
252468
@@ -4831,17 +4839,17 @@ set_memory_parameters(dom, newparams, flags=0)
252468
       Newx(params, nparams, virTypedParameter);
252468
 
252468
       if (virDomainGetMemoryParameters(dom, params, &nparams, 0) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
 
252468
       nparams = vir_typed_param_from_hv(newparams, params, nparams);
252468
 
252468
       if (virDomainSetMemoryParameters(dom, params, nparams, flags) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
 
252468
 
252468
 HV *
252468
@@ -4859,12 +4867,12 @@ get_numa_parameters(dom, flags=0)
252468
       Newx(params, nparams, virTypedParameter);
252468
 
252468
       if (virDomainGetNumaParameters(dom, params, &nparams, flags) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
 
252468
       RETVAL = vir_typed_param_to_hv(params, nparams);
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
   OUTPUT:
252468
       RETVAL
252468
 
252468
@@ -4885,17 +4893,17 @@ set_numa_parameters(dom, newparams, flags=0)
252468
       Newx(params, nparams, virTypedParameter);
252468
 
252468
       if (virDomainGetNumaParameters(dom, params, &nparams, 0) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
 
252468
       nparams = vir_typed_param_from_hv(newparams, params, nparams);
252468
 
252468
       if (virDomainSetNumaParameters(dom, params, nparams, flags) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
 
252468
 
252468
 HV *
252468
@@ -4913,12 +4921,12 @@ get_blkio_parameters(dom, flags=0)
252468
       Newx(params, nparams, virBlkioParameter);
252468
 
252468
       if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
 
252468
       RETVAL = vir_typed_param_to_hv(params, nparams);
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
   OUTPUT:
252468
       RETVAL
252468
 
252468
@@ -4939,7 +4947,7 @@ set_blkio_parameters(dom, newparams, flags=0)
252468
       Newx(params, nparams, virBlkioParameter);
252468
 
252468
       if (virDomainGetBlkioParameters(dom, params, &nparams, 0) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
 
252468
@@ -4947,10 +4955,10 @@ set_blkio_parameters(dom, newparams, flags=0)
252468
 
252468
       if (virDomainSetBlkioParameters(dom, params, nparams,
252468
                                       flags) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
 
252468
 
252468
 HV *
252468
@@ -4962,12 +4970,12 @@ get_perf_events(dom, flags=0)
252468
       int nparams = 0;
252468
     CODE:
252468
       if (virDomainGetPerfEvents(dom, &params, &nparams, flags) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
 
252468
       RETVAL = vir_typed_param_to_hv(params, nparams);
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
   OUTPUT:
252468
       RETVAL
252468
 
252468
@@ -4982,7 +4990,7 @@ set_perf_events(dom, newparams, flags=0)
252468
       int nparams = 0;
252468
     PPCODE:
252468
       if (virDomainGetPerfEvents(dom, &params, &nparams, 0) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
 
252468
@@ -4990,7 +4998,7 @@ set_perf_events(dom, newparams, flags=0)
252468
 
252468
       if (virDomainSetPerfEvents(dom, params, nparams, flags) < 0)
252468
           _croak_error();
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
 
252468
 
252468
 HV *
252468
@@ -5005,7 +5013,7 @@ get_launch_security_info(dom, flags=0)
252468
           _croak_error();
252468
       }
252468
       RETVAL = vir_typed_param_to_hv(params, nparams);
252468
-      free(params);
252468
+      virTypedParamsFree(params, nparams);
252468
   OUTPUT:
252468
       RETVAL
252468
 
252468
@@ -5386,10 +5394,10 @@ _migrate(dom, destcon, newparams, flags=0)
252468
       * if it is possible todo so
252468
       */
252468
      if ((RETVAL = virDomainMigrate3(dom, destcon, params, nparams, flags)) == NULL) {
252468
-         Safefree(params);
252468
+         vir_typed_param_safe_free(params, nparams);
252468
          _croak_error();
252468
      }
252468
-     Safefree(params);
252468
+     vir_typed_param_safe_free(params, nparams);
252468
  OUTPUT:
252468
      RETVAL
252468
 
252468
@@ -5477,10 +5485,10 @@ _migrate_to_uri(dom, desturi, newparams, flags=0)
252468
       * if it is possible todo so
252468
       */
252468
      if (virDomainMigrateToURI3(dom, desturi, params, nparams, flags) < 0) {
252468
-         Safefree(params);
252468
+         vir_typed_param_safe_free(params, nparams);
252468
          _croak_error();
252468
      }
252468
-     Safefree(params);
252468
+     vir_typed_param_safe_free(params, nparams);
252468
 
252468
 
252468
 void
252468
@@ -5639,12 +5647,12 @@ get_block_iotune(dom, disk, flags=0)
252468
 
252468
       Newx(params, nparams, virTypedParameter);
252468
       if (virDomainGetBlockIoTune(dom, disk, params, &nparams, flags) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
 
252468
       RETVAL = vir_typed_param_to_hv(params, nparams);
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
   OUTPUT:
252468
       RETVAL
252468
 
252468
@@ -5665,16 +5673,16 @@ set_block_iotune(dom, disk, newparams, flags=0)
252468
       Newx(params, nparams, virTypedParameter);
252468
 
252468
       if (virDomainGetBlockIoTune(dom, disk, params, &nparams, 0) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
 
252468
       nparams = vir_typed_param_from_hv(newparams, params, nparams);
252468
       if (virDomainSetBlockIoTune(dom, disk, params, nparams, flags) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
 
252468
 
252468
 HV *
252468
@@ -5693,12 +5701,12 @@ get_interface_parameters(dom, intf, flags=0)
252468
 
252468
       Newx(params, nparams, virTypedParameter);
252468
       if (virDomainGetInterfaceParameters(dom, intf, params, &nparams, flags) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
 
252468
       RETVAL = vir_typed_param_to_hv(params, nparams);
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
   OUTPUT:
252468
       RETVAL
252468
 
252468
@@ -5719,16 +5727,16 @@ set_interface_parameters(dom, intf, newparams, flags=0)
252468
       Newx(params, nparams, virTypedParameter);
252468
 
252468
       if (virDomainGetInterfaceParameters(dom, intf, params, &nparams, 0) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
 
252468
       nparams = vir_typed_param_from_hv(newparams, params, nparams);
252468
       if (virDomainSetInterfaceParameters(dom, intf, params, nparams, flags) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
 
252468
 
252468
 HV *
252468
@@ -5764,7 +5772,7 @@ block_stats(dom, path, flags=0)
252468
           Newx(params, nparams, virTypedParameter);
252468
 
252468
           if (virDomainBlockStatsFlags(dom, path, params, &nparams, flags) < 0) {
252468
-              Safefree(params);
252468
+              vir_typed_param_safe_free(params, nparams);
252468
               _croak_error();
252468
           }
252468
 
252468
@@ -5784,7 +5792,7 @@ block_stats(dom, path, flags=0)
252468
                   (void)hv_store(RETVAL, field, strlen(field), val, 0);
252468
               }
252468
           }
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
       }
252468
   OUTPUT:
252468
       RETVAL
252468
@@ -6018,7 +6026,7 @@ get_cpu_stats(dom, start_cpu, ncpus, flags=0)
252468
 
252468
       Newx(params, ncpus * nparams, virTypedParameter);
252468
       if ((ret = virDomainGetCPUStats(dom, params, nparams, start_cpu, ncpus, flags)) < 0) {
252468
-          Safefree(params);
252468
+          vir_typed_param_safe_free(params, nparams);
252468
           _croak_error();
252468
       }
252468
 
252468
@@ -6028,7 +6036,7 @@ get_cpu_stats(dom, start_cpu, ncpus, flags=0)
252468
           PUSHs(newRV_noinc((SV *)rec));
252468
       }
252468
 
252468
-      Safefree(params);
252468
+      vir_typed_param_safe_free(params, nparams);
252468
 
252468
 
252468
 void