[libvirt] [PATCH v2 2/4] conf: Use only one temporary string in virDomainDiskSourceParse

Peter Krempa posted 4 patches 8 years, 9 months ago
[libvirt] [PATCH v2 2/4] conf: Use only one temporary string in virDomainDiskSourceParse
Posted by Peter Krempa 8 years, 9 months ago
---
 src/conf/domain_conf.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 5a736c853..c40a5a7a6 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -7526,7 +7526,7 @@ virDomainDiskSourceParse(xmlNodePtr node,
                          virStorageSourcePtr src)
 {
     int ret = -1;
-    char *protocol = NULL;
+    char *tmp = NULL;
     xmlNodePtr saveNode = ctxt->node;

     ctxt->node = node;
@@ -7542,17 +7542,18 @@ virDomainDiskSourceParse(xmlNodePtr node,
         src->path = virXMLPropString(node, "dir");
         break;
     case VIR_STORAGE_TYPE_NETWORK:
-        if (!(protocol = virXMLPropString(node, "protocol"))) {
+        if (!(tmp = virXMLPropString(node, "protocol"))) {
             virReportError(VIR_ERR_XML_ERROR, "%s",
                            _("missing network source protocol type"));
             goto cleanup;
         }

-        if ((src->protocol = virStorageNetProtocolTypeFromString(protocol)) <= 0) {
+        if ((src->protocol = virStorageNetProtocolTypeFromString(tmp)) <= 0) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                           _("unknown protocol type '%s'"), protocol);
+                           _("unknown protocol type '%s'"), tmp);
             goto cleanup;
         }
+        VIR_FREE(tmp);

         if (!(src->path = virXMLPropString(node, "name")) &&
             src->protocol != VIR_STORAGE_NET_PROTOCOL_NBD) {
@@ -7565,7 +7566,6 @@ virDomainDiskSourceParse(xmlNodePtr node,
          * as a part of the path. This is hard to work with when dealing with
          * relative names. Split out the volume into a separate variable */
         if (src->path && src->protocol == VIR_STORAGE_NET_PROTOCOL_GLUSTER) {
-            char *tmp;
             if (!(tmp = strchr(src->path, '/')) ||
                 tmp == src->path) {
                 virReportError(VIR_ERR_XML_ERROR,
@@ -7580,6 +7580,7 @@ virDomainDiskSourceParse(xmlNodePtr node,
                 goto cleanup;

             tmp[0] = '\0';
+            tmp = NULL;
         }

         /* snapshot currently works only for remote disks */
@@ -7612,7 +7613,7 @@ virDomainDiskSourceParse(xmlNodePtr node,
     ret = 0;

  cleanup:
-    VIR_FREE(protocol);
+    VIR_FREE(tmp);
     ctxt->node = saveNode;
     return ret;
 }
-- 
2.12.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2 2/4] conf: Use only one temporary string in virDomainDiskSourceParse
Posted by Ján Tomko 8 years, 9 months ago
On Fri, Apr 28, 2017 at 01:10:25PM +0200, Peter Krempa wrote:
>---
> src/conf/domain_conf.c | 13 +++++++------
> 1 file changed, 7 insertions(+), 6 deletions(-)
>

>@@ -7565,7 +7566,6 @@ virDomainDiskSourceParse(xmlNodePtr node,
>          * as a part of the path. This is hard to work with when dealing with
>          * relative names. Split out the volume into a separate variable */
>         if (src->path && src->protocol == VIR_STORAGE_NET_PROTOCOL_GLUSTER) {
>-            char *tmp;

This is not a temporary string, just a pointer into a different string
and should not be mixed up with the virXMLPropString usage.

(Maybe this code would look nicer with virStringSplit?)

Jan

>             if (!(tmp = strchr(src->path, '/')) ||
>                 tmp == src->path) {
>                 virReportError(VIR_ERR_XML_ERROR,
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list