]> xenbits.xensource.com Git - libvirt.git/commitdiff
virSecuritySELinuxRestoreFileLabel: Adjust code pattern
authorMichal Privoznik <mprivozn@redhat.com>
Mon, 10 Sep 2018 07:55:51 +0000 (09:55 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 18 Sep 2018 15:12:53 +0000 (17:12 +0200)
Firstly, the following code pattern is harder to follow:

  if (func() < 0) {
      error();
  } else {
      /* success */
  }

We should put 'goto cleanup' into the error branch and move the
else branch one level up.
Secondly, 'rc' should really be named 'ret' because it holds
return value of the function. Not some intermediate value.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
src/security/security_selinux.c

index 35f18e1738ddf2e6b463305332ad8f46662e0449..72d12c9df15cd25cde7a8980c45feff4de2b5a2d 100644 (file)
@@ -1291,9 +1291,9 @@ virSecuritySELinuxRestoreFileLabel(virSecurityManagerPtr mgr,
 {
     struct stat buf;
     security_context_t fcon = NULL;
-    int rc = -1;
     char *newpath = NULL;
     char ebuf[1024];
+    int ret = -1;
 
     /* Some paths are auto-generated, so let's be safe here and do
      * nothing if nothing is needed.
@@ -1320,15 +1320,18 @@ virSecuritySELinuxRestoreFileLabel(virSecurityManagerPtr mgr,
          * which makes this an expected non error
          */
         VIR_WARN("cannot lookup default selinux label for %s", newpath);
-        rc = 0;
-    } else {
-        rc = virSecuritySELinuxSetFilecon(mgr, newpath, fcon);
+        ret = 0;
+        goto cleanup;
     }
 
+    if (virSecuritySELinuxSetFilecon(mgr, newpath, fcon) < 0)
+        goto cleanup;
+
+    ret = 0;
  cleanup:
     freecon(fcon);
     VIR_FREE(newpath);
-    return rc;
+    return ret;
 }