net.c: fix overflow in tx/rx size computation
authorSimon Guinot <simon.guinot@sequanux.org>
Thu, 21 Nov 2019 21:31:26 +0000 (22:31 +0100)
committerSimon Guinot <simon.guinot@sequanux.org>
Thu, 21 Nov 2019 21:34:41 +0000 (22:34 +0100)
This patch fixes the ctrl->rx and ctrl->tx computation when
"ctrl->count * ctrl->size" is greater than 2^32.

net.c

diff --git a/net.c b/net.c
index f37ccc4..8facae4 100644 (file)
--- a/net.c
+++ b/net.c
@@ -356,8 +356,8 @@ static int send_data_messages(int sock, struct netsum_ctrl *ctrl,
        elapsed = stop_time.tv_sec - start_time.tv_sec +
                  ((double) (stop_time.tv_usec - start_time.tv_usec) / 1000000);
 
-       ctrl->tx = (ctrl->count * ctrl->size);
-       ctrl->tx_sum += (ctrl->count * ctrl->size);
+       ctrl->tx = (uint64_t) ctrl->count * (uint64_t) ctrl->size;
+       ctrl->tx_sum += ctrl->tx;
        ctrl->tx_time = elapsed;
        ctrl->tx_time_sum += elapsed;
 
@@ -422,8 +422,8 @@ static int recv_data_messages(int sock, struct netsum_ctrl *ctrl,
        elapsed = stop_time.tv_sec - start_time.tv_sec +
                  ((double) (stop_time.tv_usec - start_time.tv_usec) / 1000000);
 
-       ctrl->rx = (ctrl->count * ctrl->size);
-       ctrl->rx_sum += (ctrl->count * ctrl->size);
+       ctrl->rx = (uint64_t) ctrl->count * (uint64_t) ctrl->size;
+       ctrl->rx_sum += ctrl->rx;
        ctrl->rx_time = elapsed;
        ctrl->rx_time_sum += elapsed;