[PATCH v5 3/3] qemu-ga: Avoiding freeing line prematurely

Dehan Meng posted 3 patches 2 weeks, 3 days ago
[PATCH v5 3/3] qemu-ga: Avoiding freeing line prematurely
Posted by Dehan Meng 2 weeks, 3 days ago
It's now only freed at the end of the function.

Signed-off-by: Dehan Meng <demeng@redhat.com>
---
 qga/commands-linux.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/qga/commands-linux.c b/qga/commands-linux.c
index 55c35bfc8f..70020621bf 100644
--- a/qga/commands-linux.c
+++ b/qga/commands-linux.c
@@ -2138,8 +2138,7 @@ GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp)
         is_ipv6 = (i == 1);
         fp = fopen(routeFiles[i], "r");
         if (fp == NULL) {
-            error_setg_errno(errp, errno, "open(\"%s\")", routeFiles[i]);
-            free(line);
+            error_setg_errno(errp, errno, "open(\"%s\")", route_files[i]);
             continue;
         }
 
@@ -2148,7 +2147,6 @@ GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp)
                 firstLine = 0;
                 continue;
             }
-            
             if (is_ipv6) {
                 char destination[33], source[33], next_hop[33];
                 int des_prefixlen, src_prefixlen, metric, refcnt, use, flags;
@@ -2215,9 +2213,8 @@ GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp)
             QAPI_LIST_APPEND(tail, route);
         }
 
-        free(line);
         fclose(fp);
     }
-
+    free(line);
     return head;
 }
-- 
2.40.1
Re: [PATCH v5 3/3] qemu-ga: Avoiding freeing line prematurely
Posted by Konstantin Kostiuk 2 weeks, 3 days ago
On Wed, Nov 6, 2024 at 10:42 AM Dehan Meng <demeng@redhat.com> wrote:

> It's now only freed at the end of the function.
>
> Signed-off-by: Dehan Meng <demeng@redhat.com>
> ---
>  qga/commands-linux.c | 7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)
>
> diff --git a/qga/commands-linux.c b/qga/commands-linux.c
> index 55c35bfc8f..70020621bf 100644
> --- a/qga/commands-linux.c
> +++ b/qga/commands-linux.c
> @@ -2138,8 +2138,7 @@ GuestNetworkRouteList
> *qmp_guest_network_get_route(Error **errp)
>          is_ipv6 = (i == 1);
>          fp = fopen(routeFiles[i], "r");
>          if (fp == NULL) {
> -            error_setg_errno(errp, errno, "open(\"%s\")", routeFiles[i]);
> -            free(line);
> +            error_setg_errno(errp, errno, "open(\"%s\")", route_files[i]);
>

Compilation failed

../qga/commands-linux.c:2140:59: error: ‘route_files’ undeclared (first use
in this function); did you mean ‘routeFiles’?
 2140 |             error_setg_errno(errp, errno, "open(\"%s\")",
route_files[i]);



>              continue;
>          }
>
> @@ -2148,7 +2147,6 @@ GuestNetworkRouteList
> *qmp_guest_network_get_route(Error **errp)
>                  firstLine = 0;
>                  continue;
>              }
> -
>              if (is_ipv6) {
>                  char destination[33], source[33], next_hop[33];
>                  int des_prefixlen, src_prefixlen, metric, refcnt, use,
> flags;
> @@ -2215,9 +2213,8 @@ GuestNetworkRouteList
> *qmp_guest_network_get_route(Error **errp)
>              QAPI_LIST_APPEND(tail, route);
>          }
>
> -        free(line);
>          fclose(fp);
>      }
> -
> +    free(line);
>      return head;
>  }
> --
> 2.40.1
>
>
Re: [PATCH v5 3/3] qemu-ga: Avoiding freeing line prematurely
Posted by Daniel P. Berrangé 2 weeks, 3 days ago
On Wed, Nov 06, 2024 at 04:41:34PM +0800, Dehan Meng wrote:
> It's now only freed at the end of the function.
> 
> Signed-off-by: Dehan Meng <demeng@redhat.com>
> ---
>  qga/commands-linux.c | 7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|


Re: [PATCH v5 3/3] qemu-ga: Avoiding freeing line prematurely
Posted by Konstantin Kostiuk 2 weeks, 3 days ago
Reviewed-by: Konstantin Kostiuk <kkostiuk@redhat.com>

On Wed, Nov 6, 2024 at 3:38 PM Daniel P. Berrangé <berrange@redhat.com>
wrote:

> On Wed, Nov 06, 2024 at 04:41:34PM +0800, Dehan Meng wrote:
> > It's now only freed at the end of the function.
> >
> > Signed-off-by: Dehan Meng <demeng@redhat.com>
> > ---
> >  qga/commands-linux.c | 7 ++-----
> >  1 file changed, 2 insertions(+), 5 deletions(-)
>
> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
>
>
> With regards,
> Daniel
> --
> |: https://berrange.com      -o-
> https://www.flickr.com/photos/dberrange :|
> |: https://libvirt.org         -o-
> https://fstop138.berrange.com :|
> |: https://entangle-photo.org    -o-
> https://www.instagram.com/dberrange :|
>
>