]> xenbits.xensource.com Git - libvirt.git/commitdiff
virsh-secret: Refactor error paths
authorPeter Krempa <pkrempa@redhat.com>
Mon, 21 Jan 2013 17:28:47 +0000 (18:28 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 4 Feb 2013 13:17:45 +0000 (14:17 +0100)
This patch switches string option retrieval to vshCommandOptStringReq
and refactors some error paths to avoid an unlikely memory leak of a
secret object in cmdSecretSetValue.

tools/virsh-secret.c

index 05bab85e24f5c99510fcfe182fd7d72892d16118..a7354984c78fd081a6847fce5b9f28b1f5570432 100644 (file)
@@ -48,7 +48,7 @@ vshCommandOptSecret(vshControl *ctl, const vshCmd *cmd, const char **name)
     if (!vshCmdHasOption(ctl, cmd, optname))
         return NULL;
 
-    if (vshCommandOptString(cmd, optname, &n) <= 0)
+    if (vshCommandOptStringReq(ctl, cmd, optname, &n) < 0)
         return NULL;
 
     vshDebug(ctl, VSH_ERR_DEBUG,
@@ -90,28 +90,31 @@ cmdSecretDefine(vshControl *ctl, const vshCmd *cmd)
     char *buffer;
     virSecretPtr res;
     char uuid[VIR_UUID_STRING_BUFLEN];
+    bool ret = false;
 
-    if (vshCommandOptString(cmd, "file", &from) <= 0)
+    if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
         return false;
 
     if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0)
         return false;
 
-    res = virSecretDefineXML(ctl->conn, buffer, 0);
-    VIR_FREE(buffer);
-
-    if (res == NULL) {
+    if (!(res = virSecretDefineXML(ctl->conn, buffer, 0))) {
         vshError(ctl, _("Failed to set attributes from %s"), from);
-        return false;
+        goto cleanup;
     }
+
     if (virSecretGetUUIDString(res, &(uuid[0])) < 0) {
         vshError(ctl, "%s", _("Failed to get UUID of created secret"));
-        virSecretFree(res);
-        return false;
+        goto cleanup;
     }
+
     vshPrint(ctl, _("Secret %s created\n"), uuid);
+    ret = true;
+
+cleanup:
+    VIR_FREE(buffer);
     virSecretFree(res);
-    return true;
+    return ret;
 }
 
 /*
@@ -188,11 +191,10 @@ cmdSecretSetValue(vshControl *ctl, const vshCmd *cmd)
     int res;
     bool ret = false;
 
-    secret = vshCommandOptSecret(ctl, cmd, NULL);
-    if (secret == NULL)
+    if (!(secret = vshCommandOptSecret(ctl, cmd, NULL)))
         return false;
 
-    if (vshCommandOptString(cmd, "base64", &base64) <= 0)
+    if (vshCommandOptStringReq(ctl, cmd, "base64", &base64) < 0)
         goto cleanup;
 
     if (!base64_decode_alloc(base64, strlen(base64), &value, &value_size)) {
@@ -201,7 +203,7 @@ cmdSecretSetValue(vshControl *ctl, const vshCmd *cmd)
     }
     if (value == NULL) {
         vshError(ctl, "%s", _("Failed to allocate memory"));
-        return false;
+        goto cleanup;
     }
 
     res = virSecretSetValue(secret, (unsigned char *)value, value_size, 0);