From: Konstantin Kostiuk <kkostiuk@redhat.com>
To: qemu-devel@nongnu.org, Peter Maydell <peter.maydell@linaro.org>,
Stefan Hajnoczi <stefanha@redhat.com>,
Richard Henderson <richard.henderson@linaro.org>
Subject: [PULL 1/3] qemu-ga: Add 'Null' check and Redefine 'route'
Date: Thu, 7 Nov 2024 12:39:59 +0200 [thread overview]
Message-ID: <20241107104001.66039-2-kkostiuk@redhat.com> (raw)
In-Reply-To: <20241107104001.66039-1-kkostiuk@redhat.com>
From: Dehan Meng <demeng@redhat.com>
sscanf return values are checked and add 'Null' check for
mandatory parameters. And merged redundant route and
networkroute variables.
Signed-off-by: Dehan Meng <demeng@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Reviewed-by: Konstantin Kostiuk <kkostiuk@redhat.com>
Message-ID: <20241107102155.57573-2-kkostiuk@redhat.com>
Signed-off-by: Konstantin Kostiuk <kkostiuk@redhat.com>
---
qga/commands-linux.c | 83 +++++++++++++++++++++++---------------------
1 file changed, 44 insertions(+), 39 deletions(-)
diff --git a/qga/commands-linux.c b/qga/commands-linux.c
index 9b1746b24f..eaf53e1c17 100644
--- a/qga/commands-linux.c
+++ b/qga/commands-linux.c
@@ -2102,7 +2102,9 @@ static char *hexToIPAddress(const void *hexValue, int is_ipv6)
int i;
for (i = 0; i < 16; i++) {
- sscanf(&hexStr[i * 2], "%02hhx", &in6.s6_addr[i]);
+ if (sscanf(&hex_str[i * 2], "%02hhx", &in6.s6_addr[i]) != 1) {
+ return NULL;
+ }
}
inet_ntop(AF_INET6, &in6, addr, INET6_ADDRSTRLEN);
@@ -2144,9 +2146,9 @@ GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp)
firstLine = 0;
continue;
}
- GuestNetworkRoute *route = NULL;
- GuestNetworkRoute *networkroute;
char Iface[IFNAMSIZ];
+ g_autoptr(GuestNetworkRoute) route = g_new0(GuestNetworkRoute, 1);
+
if (is_ipv6) {
char Destination[33], Source[33], NextHop[33];
int DesPrefixlen, SrcPrefixlen, Metric, RefCnt, Use, Flags;
@@ -2159,26 +2161,27 @@ GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp)
continue;
}
- route = g_new0(GuestNetworkRoute, 1);
- networkroute = route;
- networkroute->iface = g_strdup(Iface);
- networkroute->destination = hexToIPAddress(Destination, 1);
- networkroute->metric = Metric;
- networkroute->source = hexToIPAddress(Source, 1);
- networkroute->desprefixlen = g_strdup_printf(
+ route->iface = g_strdup(Iface);
+ route->destination = hexToIPAddress(Destination, 1);
+ if (route->destination == NULL) {
+ continue;
+ }
+ route->metric = Metric;
+ route->source = hexToIPAddress(Source, 1);
+ route->desprefixlen = g_strdup_printf(
"%d", DesPrefixlen
);
- networkroute->srcprefixlen = g_strdup_printf(
+ route->srcprefixlen = g_strdup_printf(
"%d", SrcPrefixlen
);
- networkroute->nexthop = hexToIPAddress(NextHop, 1);
- networkroute->has_flags = true;
- networkroute->flags = Flags;
- networkroute->has_refcnt = true;
- networkroute->refcnt = RefCnt;
- networkroute->has_use = true;
- networkroute->use = Use;
- networkroute->version = 6;
+ route->nexthop = hexToIPAddress(NextHop, 1);
+ route->has_flags = true;
+ route->flags = Flags;
+ route->has_refcnt = true;
+ route->refcnt = RefCnt;
+ route->has_use = true;
+ route->use = Use;
+ route->version = 6;
} else {
unsigned int Destination, Gateway, Mask, Flags;
int RefCnt, Use, Metric, MTU, Window, IRTT;
@@ -2190,29 +2193,31 @@ GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp)
continue;
}
- route = g_new0(GuestNetworkRoute, 1);
- networkroute = route;
- networkroute->iface = g_strdup(Iface);
- networkroute->destination = hexToIPAddress(&Destination, 0);
- networkroute->gateway = hexToIPAddress(&Gateway, 0);
- networkroute->mask = hexToIPAddress(&Mask, 0);
- networkroute->metric = Metric;
- networkroute->has_flags = true;
- networkroute->flags = Flags;
- networkroute->has_refcnt = true;
- networkroute->refcnt = RefCnt;
- networkroute->has_use = true;
- networkroute->use = Use;
- networkroute->has_mtu = true;
- networkroute->mtu = MTU;
- networkroute->has_window = true;
- networkroute->window = Window;
- networkroute->has_irtt = true;
- networkroute->irtt = IRTT;
- networkroute->version = 4;
+ route->iface = g_strdup(Iface);
+ route->destination = hexToIPAddress(&Destination, 0);
+ if (route->destination == NULL) {
+ continue;
+ }
+ route->gateway = hexToIPAddress(&Gateway, 0);
+ route->mask = hexToIPAddress(&Mask, 0);
+ route->metric = Metric;
+ route->has_flags = true;
+ route->flags = Flags;
+ route->has_refcnt = true;
+ route->refcnt = RefCnt;
+ route->has_use = true;
+ route->use = Use;
+ route->has_mtu = true;
+ route->mtu = MTU;
+ route->has_window = true;
+ route->window = Window;
+ route->has_irtt = true;
+ route->irtt = IRTT;
+ route->version = 4;
}
QAPI_LIST_APPEND(tail, route);
+ route = NULL;
}
free(line);
--
2.47.0
next prev parent reply other threads:[~2024-11-07 10:41 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-07 10:39 [PULL 0/3] QGA fixes for guest_network_get_route command for 9.2 Konstantin Kostiuk
2024-11-07 10:39 ` Konstantin Kostiuk [this message]
2024-11-07 10:40 ` [PULL 2/3] qemu-ga: Optimize var declaration and definition Konstantin Kostiuk
2024-11-07 10:40 ` [PULL 3/3] qemu-ga: Avoiding freeing line prematurely Konstantin Kostiuk
2024-11-08 10:22 ` [PULL 0/3] QGA fixes for guest_network_get_route command for 9.2 Peter Maydell
2024-11-08 11:29 ` Konstantin Kostiuk
2024-11-08 13:21 ` Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241107104001.66039-2-kkostiuk@redhat.com \
--to=kkostiuk@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).