qemu-ga: Add 'Null' check and Redefine 'route'

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>
This commit is contained in:
Dehan Meng 2024-11-07 12:21:53 +02:00 committed by Konstantin Kostiuk
parent f15f7273ea
commit 32bd9e206e
1 changed files with 44 additions and 39 deletions

View File

@ -2102,7 +2102,9 @@ static char *hexToIPAddress(const void *hexValue, int is_ipv6)
int i; int i;
for (i = 0; i < 16; 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); inet_ntop(AF_INET6, &in6, addr, INET6_ADDRSTRLEN);
@ -2144,9 +2146,9 @@ GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp)
firstLine = 0; firstLine = 0;
continue; continue;
} }
GuestNetworkRoute *route = NULL;
GuestNetworkRoute *networkroute;
char Iface[IFNAMSIZ]; char Iface[IFNAMSIZ];
g_autoptr(GuestNetworkRoute) route = g_new0(GuestNetworkRoute, 1);
if (is_ipv6) { if (is_ipv6) {
char Destination[33], Source[33], NextHop[33]; char Destination[33], Source[33], NextHop[33];
int DesPrefixlen, SrcPrefixlen, Metric, RefCnt, Use, Flags; int DesPrefixlen, SrcPrefixlen, Metric, RefCnt, Use, Flags;
@ -2159,26 +2161,27 @@ GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp)
continue; continue;
} }
route = g_new0(GuestNetworkRoute, 1); route->iface = g_strdup(Iface);
networkroute = route; route->destination = hexToIPAddress(Destination, 1);
networkroute->iface = g_strdup(Iface); if (route->destination == NULL) {
networkroute->destination = hexToIPAddress(Destination, 1); continue;
networkroute->metric = Metric; }
networkroute->source = hexToIPAddress(Source, 1); route->metric = Metric;
networkroute->desprefixlen = g_strdup_printf( route->source = hexToIPAddress(Source, 1);
route->desprefixlen = g_strdup_printf(
"%d", DesPrefixlen "%d", DesPrefixlen
); );
networkroute->srcprefixlen = g_strdup_printf( route->srcprefixlen = g_strdup_printf(
"%d", SrcPrefixlen "%d", SrcPrefixlen
); );
networkroute->nexthop = hexToIPAddress(NextHop, 1); route->nexthop = hexToIPAddress(NextHop, 1);
networkroute->has_flags = true; route->has_flags = true;
networkroute->flags = Flags; route->flags = Flags;
networkroute->has_refcnt = true; route->has_refcnt = true;
networkroute->refcnt = RefCnt; route->refcnt = RefCnt;
networkroute->has_use = true; route->has_use = true;
networkroute->use = Use; route->use = Use;
networkroute->version = 6; route->version = 6;
} else { } else {
unsigned int Destination, Gateway, Mask, Flags; unsigned int Destination, Gateway, Mask, Flags;
int RefCnt, Use, Metric, MTU, Window, IRTT; int RefCnt, Use, Metric, MTU, Window, IRTT;
@ -2190,29 +2193,31 @@ GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp)
continue; continue;
} }
route = g_new0(GuestNetworkRoute, 1); route->iface = g_strdup(Iface);
networkroute = route; route->destination = hexToIPAddress(&Destination, 0);
networkroute->iface = g_strdup(Iface); if (route->destination == NULL) {
networkroute->destination = hexToIPAddress(&Destination, 0); continue;
networkroute->gateway = hexToIPAddress(&Gateway, 0); }
networkroute->mask = hexToIPAddress(&Mask, 0); route->gateway = hexToIPAddress(&Gateway, 0);
networkroute->metric = Metric; route->mask = hexToIPAddress(&Mask, 0);
networkroute->has_flags = true; route->metric = Metric;
networkroute->flags = Flags; route->has_flags = true;
networkroute->has_refcnt = true; route->flags = Flags;
networkroute->refcnt = RefCnt; route->has_refcnt = true;
networkroute->has_use = true; route->refcnt = RefCnt;
networkroute->use = Use; route->has_use = true;
networkroute->has_mtu = true; route->use = Use;
networkroute->mtu = MTU; route->has_mtu = true;
networkroute->has_window = true; route->mtu = MTU;
networkroute->window = Window; route->has_window = true;
networkroute->has_irtt = true; route->window = Window;
networkroute->irtt = IRTT; route->has_irtt = true;
networkroute->version = 4; route->irtt = IRTT;
route->version = 4;
} }
QAPI_LIST_APPEND(tail, route); QAPI_LIST_APPEND(tail, route);
route = NULL;
} }
free(line); free(line);