[libvirt] [PATCH v1 17/40] util: hook: use VIR_AUTOFREE instead of VIR_FREE for scalar types

Sukrit Bhatnagar posted 40 patches 7 years, 6 months ago
There is a newer version of this series
[libvirt] [PATCH v1 17/40] util: hook: use VIR_AUTOFREE instead of VIR_FREE for scalar types
Posted by Sukrit Bhatnagar 7 years, 6 months ago
By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@gmail.com>
---
 src/util/virhook.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/src/util/virhook.c b/src/util/virhook.c
index facd74a..51f0eb5 100644
--- a/src/util/virhook.c
+++ b/src/util/virhook.c
@@ -122,8 +122,7 @@ static int virHooksFound = -1;
 static int
 virHookCheck(int no, const char *driver)
 {
-    char *path;
-    int ret;
+    VIR_AUTOFREE(char *) path = NULL;
 
     if (driver == NULL) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -139,18 +138,15 @@ virHookCheck(int no, const char *driver)
     }
 
     if (!virFileExists(path)) {
-        ret = 0;
         VIR_DEBUG("No hook script %s", path);
+        return 0;
     } else if (!virFileIsExecutable(path)) {
-        ret = 0;
         VIR_WARN("Non-executable hook script %s", path);
+        return 0;
     } else {
-        ret = 1;
         VIR_DEBUG("Found hook script %s", path);
+        return 1;
     }
-
-    VIR_FREE(path);
-    return ret;
 }
 
 /*
@@ -233,7 +229,7 @@ virHookCall(int driver,
             char **output)
 {
     int ret;
-    char *path;
+    VIR_AUTOFREE(char *) path = NULL;
     virCommandPtr cmd;
     const char *drvstr;
     const char *opstr;
@@ -318,7 +314,5 @@ virHookCall(int driver,
 
     virCommandFree(cmd);
 
-    VIR_FREE(path);
-
     return ret;
 }
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v1 17/40] util: hook: use VIR_AUTOFREE instead of VIR_FREE for scalar types
Posted by Erik Skultety 7 years, 6 months ago
On Sat, Jul 21, 2018 at 05:36:49PM +0530, Sukrit Bhatnagar wrote:
> By making use of GNU C's cleanup attribute handled by the
> VIR_AUTOFREE macro for declaring scalar variables, majority
> of the VIR_FREE calls can be dropped, which in turn leads to
> getting rid of most of our cleanup sections.
>
> Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@gmail.com>
> ---
>  src/util/virhook.c | 16 +++++-----------
>  1 file changed, 5 insertions(+), 11 deletions(-)
>
> diff --git a/src/util/virhook.c b/src/util/virhook.c
> index facd74a..51f0eb5 100644
> --- a/src/util/virhook.c
> +++ b/src/util/virhook.c
> @@ -122,8 +122,7 @@ static int virHooksFound = -1;
>  static int
>  virHookCheck(int no, const char *driver)
>  {
> -    char *path;
> -    int ret;
> +    VIR_AUTOFREE(char *) path = NULL;
>
>      if (driver == NULL) {
>          virReportError(VIR_ERR_INTERNAL_ERROR,
> @@ -139,18 +138,15 @@ virHookCheck(int no, const char *driver)
>      }
>
>      if (!virFileExists(path)) {
> -        ret = 0;
>          VIR_DEBUG("No hook script %s", path);
> +        return 0;
>      } else if (!virFileIsExecutable(path)) {
> -        ret = 0;
>          VIR_WARN("Non-executable hook script %s", path);
> +        return 0;
>      } else {
> -        ret = 1;
>          VIR_DEBUG("Found hook script %s", path);
> +        return 1;

^this should be reworked in the following way:

if (!virFileExists) {
    VIR_DEBUG;
    return 0;
}

if (!virFileIsExecutable) {
    VIR_DEBUG;
    return 0;
}

VIR_DEBUG;
return 1;

Otherwise looks okay.
Reviewed-by: Erik Skultety <eskultet@redhat.com>

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list