Replace g_new0() with g_autoptr() to simplify the code
Signed-off-by: Dehan Meng <demeng@redhat.com>
---
qga/commands-linux.c | 16 ++++++----------
1 file changed, 6 insertions(+), 10 deletions(-)
diff --git a/qga/commands-linux.c b/qga/commands-linux.c
index 9fb31956b4..ee4f345938 100644
--- a/qga/commands-linux.c
+++ b/qga/commands-linux.c
@@ -2158,15 +2158,13 @@ GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp)
continue;
}
- GuestNetworkRoute *route = g_new0(GuestNetworkRoute, 1);
+ g_autoptr(GuestNetworkRoute) route = g_new0(GuestNetworkRoute, 1);
route->destination = hex_to_ip_address(destination, 1);
- if (route->destination == NULL) {
- g_free(route);
+ route->iface = g_strdup(iface);
+ if (route->destination == NULL || route->iface == NULL) {
continue;
}
- route->iface = g_strdup(iface);
- route->destination = hex_to_ip_address(destination, 1);
route->source = hex_to_ip_address(source, 1);
route->nexthop = hex_to_ip_address(next_hop, 1);
route->desprefixlen = g_strdup_printf("%d", des_prefixlen);
@@ -2188,15 +2186,13 @@ GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp)
continue;
}
- GuestNetworkRoute *route = g_new0(GuestNetworkRoute, 1);
+ g_autoptr(GuestNetworkRoute) route = g_new0(GuestNetworkRoute, 1);
route->destination = hex_to_ip_address(destination, 1);
- if (route->destination == NULL) {
- g_free(route);
+ route->iface = g_strdup(iface);
+ if (route->destination == NULL || route->iface == NULL) {
continue;
}
- route->iface = g_strdup(iface);
- route->destination = hex_to_ip_address(&destination, 0);
route->gateway = hex_to_ip_address(&gateway, 0);
route->mask = hex_to_ip_address(&mask, 0);
route->metric = metric;
--
2.40.1
On Tue, Oct 22, 2024 at 10:29:48PM +0800, Dehan Meng wrote:
> Replace g_new0() with g_autoptr() to simplify the code
>
> Signed-off-by: Dehan Meng <demeng@redhat.com>
> ---
> qga/commands-linux.c | 16 ++++++----------
> 1 file changed, 6 insertions(+), 10 deletions(-)
>
> diff --git a/qga/commands-linux.c b/qga/commands-linux.c
> index 9fb31956b4..ee4f345938 100644
> --- a/qga/commands-linux.c
> +++ b/qga/commands-linux.c
> @@ -2158,15 +2158,13 @@ GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp)
> continue;
> }
>
> - GuestNetworkRoute *route = g_new0(GuestNetworkRoute, 1);
> + g_autoptr(GuestNetworkRoute) route = g_new0(GuestNetworkRoute, 1);
>
> route->destination = hex_to_ip_address(destination, 1);
> - if (route->destination == NULL) {
> - g_free(route);
> + route->iface = g_strdup(iface);
> + if (route->destination == NULL || route->iface == NULL) {
Checking "iface" for NULL is not required, since g_strdup will never
fail to allocate memory.
Also, these changes to use g_autoptr need to be part of the first patch,
as each step in the patch series needs to be correct.
> continue;
> }
> - route->iface = g_strdup(iface);
> - route->destination = hex_to_ip_address(destination, 1);
> route->source = hex_to_ip_address(source, 1);
> route->nexthop = hex_to_ip_address(next_hop, 1);
> route->desprefixlen = g_strdup_printf("%d", des_prefixlen);
> @@ -2188,15 +2186,13 @@ GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp)
> continue;
> }
>
> - GuestNetworkRoute *route = g_new0(GuestNetworkRoute, 1);
> + g_autoptr(GuestNetworkRoute) route = g_new0(GuestNetworkRoute, 1);
>
> route->destination = hex_to_ip_address(destination, 1);
> - if (route->destination == NULL) {
> - g_free(route);
> + route->iface = g_strdup(iface);
> + if (route->destination == NULL || route->iface == NULL) {
> continue;
> }
> - route->iface = g_strdup(iface);
> - route->destination = hex_to_ip_address(&destination, 0);
> route->gateway = hex_to_ip_address(&gateway, 0);
> route->mask = hex_to_ip_address(&mask, 0);
> route->metric = metric;
> --
> 2.40.1
>
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 :|
Thank you for reviewing, I'll summarize all of the patches.
On Fri, Oct 25, 2024 at 8:50 PM Daniel P. Berrangé <berrange@redhat.com>
wrote:
> On Tue, Oct 22, 2024 at 10:29:48PM +0800, Dehan Meng wrote:
> > Replace g_new0() with g_autoptr() to simplify the code
> >
> > Signed-off-by: Dehan Meng <demeng@redhat.com>
> > ---
> > qga/commands-linux.c | 16 ++++++----------
> > 1 file changed, 6 insertions(+), 10 deletions(-)
> >
> > diff --git a/qga/commands-linux.c b/qga/commands-linux.c
> > index 9fb31956b4..ee4f345938 100644
> > --- a/qga/commands-linux.c
> > +++ b/qga/commands-linux.c
> > @@ -2158,15 +2158,13 @@ GuestNetworkRouteList
> *qmp_guest_network_get_route(Error **errp)
> > continue;
> > }
> >
> > - GuestNetworkRoute *route = g_new0(GuestNetworkRoute, 1);
> > + g_autoptr(GuestNetworkRoute) route =
> g_new0(GuestNetworkRoute, 1);
> >
> > route->destination = hex_to_ip_address(destination, 1);
> > - if (route->destination == NULL) {
> > - g_free(route);
> > + route->iface = g_strdup(iface);
> > + if (route->destination == NULL || route->iface ==
> NULL) {
>
> Checking "iface" for NULL is not required, since g_strdup will never
> fail to allocate memory.
>
> Also, these changes to use g_autoptr need to be part of the first patch,
> as each step in the patch series needs to be correct.
>
> > continue;
> > }
> > - route->iface = g_strdup(iface);
> > - route->destination = hex_to_ip_address(destination, 1);
> > route->source = hex_to_ip_address(source, 1);
> > route->nexthop = hex_to_ip_address(next_hop, 1);
> > route->desprefixlen = g_strdup_printf("%d",
> des_prefixlen);
> > @@ -2188,15 +2186,13 @@ GuestNetworkRouteList
> *qmp_guest_network_get_route(Error **errp)
> > continue;
> > }
> >
> > - GuestNetworkRoute *route = g_new0(GuestNetworkRoute, 1);
> > + g_autoptr(GuestNetworkRoute) route =
> g_new0(GuestNetworkRoute, 1);
> >
> > route->destination = hex_to_ip_address(destination, 1);
> > - if (route->destination == NULL) {
> > - g_free(route);
> > + route->iface = g_strdup(iface);
> > + if (route->destination == NULL || route->iface ==
> NULL) {
> > continue;
> > }
> > - route->iface = g_strdup(iface);
> > - route->destination = hex_to_ip_address(&destination, 0);
> > route->gateway = hex_to_ip_address(&gateway, 0);
> > route->mask = hex_to_ip_address(&mask, 0);
> > route->metric = metric;
> > --
> > 2.40.1
> >
>
> 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 :|
>
>
© 2016 - 2026 Red Hat, Inc.