]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virSecuritySELinux(Set|Restore)HostdevCapsLabel: Refactor cleanup
authorPeter Krempa <pkrempa@redhat.com>
Tue, 27 May 2025 14:17:00 +0000 (16:17 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 22 Jul 2025 14:52:35 +0000 (16:52 +0200)
Declare 'path' only in blocks where it's used and autofree it.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/security/security_selinux.c

index 854e46954e5a911c93feb3ab3d4010ee92a8fad4..2cdc6bf9549960c4523ab414a9764f154305a562 100644 (file)
@@ -2333,7 +2333,6 @@ virSecuritySELinuxSetHostdevCapsLabel(virSecurityManager *mgr,
 {
     int ret = -1;
     virSecurityLabelDef *secdef;
-    char *path;
 
     secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
     if (secdef == NULL)
@@ -2341,6 +2340,7 @@ virSecuritySELinuxSetHostdevCapsLabel(virSecurityManager *mgr,
 
     switch (dev->source.caps.type) {
     case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_STORAGE: {
+        g_autofree char *path = NULL;
         if (vroot) {
             path = g_strdup_printf("%s/%s", vroot,
                                    dev->source.caps.u.storage.block);
@@ -2348,11 +2348,11 @@ virSecuritySELinuxSetHostdevCapsLabel(virSecurityManager *mgr,
             path = g_strdup(dev->source.caps.u.storage.block);
         }
         ret = virSecuritySELinuxSetFilecon(mgr, path, secdef->imagelabel, true);
-        VIR_FREE(path);
         break;
     }
 
     case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC: {
+        g_autofree char *path = NULL;
         if (vroot) {
             path = g_strdup_printf("%s/%s", vroot,
                                    dev->source.caps.u.misc.chardev);
@@ -2360,7 +2360,6 @@ virSecuritySELinuxSetHostdevCapsLabel(virSecurityManager *mgr,
             path = g_strdup(dev->source.caps.u.misc.chardev);
         }
         ret = virSecuritySELinuxSetFilecon(mgr, path, secdef->imagelabel, true);
-        VIR_FREE(path);
         break;
     }
 
@@ -2562,10 +2561,10 @@ virSecuritySELinuxRestoreHostdevCapsLabel(virSecurityManager *mgr,
                                           const char *vroot)
 {
     int ret = -1;
-    char *path;
 
     switch (dev->source.caps.type) {
     case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_STORAGE: {
+        g_autofree char *path = NULL;
         if (vroot) {
             path = g_strdup_printf("%s/%s", vroot,
                                    dev->source.caps.u.storage.block);
@@ -2573,11 +2572,11 @@ virSecuritySELinuxRestoreHostdevCapsLabel(virSecurityManager *mgr,
             path = g_strdup(dev->source.caps.u.storage.block);
         }
         ret = virSecuritySELinuxRestoreFileLabel(mgr, path, true);
-        VIR_FREE(path);
         break;
     }
 
     case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC: {
+        g_autofree char *path = NULL;
         if (vroot) {
             path = g_strdup_printf("%s/%s", vroot,
                                    dev->source.caps.u.misc.chardev);
@@ -2585,7 +2584,6 @@ virSecuritySELinuxRestoreHostdevCapsLabel(virSecurityManager *mgr,
             path = g_strdup(dev->source.caps.u.misc.chardev);
         }
         ret = virSecuritySELinuxRestoreFileLabel(mgr, path, true);
-        VIR_FREE(path);
         break;
     }