From 4fa34bdfd7445dd0650f431e6022e818603b99a6 Mon Sep 17 00:00:00 2001 From: luzhipeng-zte Date: Thu, 26 Oct 2017 17:08:56 +0800 Subject: [PATCH] Update commands-win32.c qga: replace GetIfEntry The data obtained by GetIfEntry is 32 bits, and it may overflow. Thus using GetIfEntry2 instead of GetIfEntry. --- qga/commands-win32.c | 46 +++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/qga/commands-win32.c b/qga/commands-win32.c index 0322188a73a..1f9205ce04a 100644 --- a/qga/commands-win32.c +++ b/qga/commands-win32.c @@ -1173,20 +1173,40 @@ static int guest_get_network_stats(const char *name, GuestNetworkInterfaceStat *stats) { DWORD if_index = 0; - MIB_IFROW a_mid_ifrow; - memset(&a_mid_ifrow, 0, sizeof(a_mid_ifrow)); + OSVERSIONINFO OSver; if_index = get_interface_index(name); - a_mid_ifrow.dwIndex = if_index; - if (NO_ERROR == GetIfEntry(&a_mid_ifrow)) { - stats->rx_bytes = a_mid_ifrow.dwInOctets; - stats->rx_packets = a_mid_ifrow.dwInUcastPkts; - stats->rx_errs = a_mid_ifrow.dwInErrors; - stats->rx_dropped = a_mid_ifrow.dwInDiscards; - stats->tx_bytes = a_mid_ifrow.dwOutOctets; - stats->tx_packets = a_mid_ifrow.dwOutUcastPkts; - stats->tx_errs = a_mid_ifrow.dwOutErrors; - stats->tx_dropped = a_mid_ifrow.dwOutDiscards; - return 0; + OSver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); + GetVersionEx(&OSver); + if (OSver.dwMajorVersion < 6) { + MIB_IFROW a_mid_ifrow; + memset(&a_mid_ifrow, 0, sizeof(a_mid_ifrow)); + a_mid_ifrow.dwIndex = if_index; + if (NO_ERROR == GetIfEntry(&a_mid_ifrow)) { + stats->rx_bytes = a_mid_ifrow.dwInOctets; + stats->rx_packets = a_mid_ifrow.dwInUcastPkts; + stats->rx_errs = a_mid_ifrow.dwInErrors; + stats->rx_dropped = a_mid_ifrow.dwInDiscards; + stats->tx_bytes = a_mid_ifrow.dwOutOctets; + stats->tx_packets = a_mid_ifrow.dwOutUcastPkts; + stats->tx_errs = a_mid_ifrow.dwOutErrors; + stats->tx_dropped = a_mid_ifrow.dwOutDiscards; + return 0; + } + } else { + MIB_IFROW2 a_mid_ifrow; + memset(&a_mid_ifrow, 0, sizeof(a_mid_ifrow)); + a_mid_ifrow.dwIndex = if_index; + if (NO_ERROR == GetIfEntry2(&a_mid_ifrow)) { + stats->rx_bytes = a_mid_ifrow.dwInOctets; + stats->rx_packets = a_mid_ifrow.dwInUcastPkts; + stats->rx_errs = a_mid_ifrow.dwInErrors; + stats->rx_dropped = a_mid_ifrow.dwInDiscards; + stats->tx_bytes = a_mid_ifrow.dwOutOctets; + stats->tx_packets = a_mid_ifrow.dwOutUcastPkts; + stats->tx_errs = a_mid_ifrow.dwOutErrors; + stats->tx_dropped = a_mid_ifrow.dwOutDiscards; + return 0; + } } return -1; }