gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r37586 - gnunet/src/util


From: gnunet
Subject: [GNUnet-SVN] r37586 - gnunet/src/util
Date: Tue, 26 Jul 2016 14:00:15 +0200

Author: grothoff
Date: 2016-07-26 14:00:14 +0200 (Tue, 26 Jul 2016)
New Revision: 37586

Modified:
   gnunet/src/util/bandwidth.c
Log:
fix overflow/underflow handling in tracker to properly handle large bandwidths

Modified: gnunet/src/util/bandwidth.c
===================================================================
--- gnunet/src/util/bandwidth.c 2016-07-26 10:16:55 UTC (rev 37585)
+++ gnunet/src/util/bandwidth.c 2016-07-26 12:00:14 UTC (rev 37586)
@@ -40,9 +40,6 @@
 {
   struct GNUNET_BANDWIDTH_Value32NBO ret;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Initializing bandwidth of %u Bps\n",
-       (unsigned int) bytes_per_second);
   ret.value__ = htonl (bytes_per_second);
   return ret;
 }
@@ -176,11 +173,23 @@
       (delta_time * ((unsigned long long) av->available_bytes_per_s__) +
        500000LL) / 1000000LL;
   current_consumption = av->consumption_since_last_update__ - delta_avail;
+  if (current_consumption > av->consumption_since_last_update__)
+  {
+    /* integer underflow, cap! */
+    current_consumption = INT64_MIN;
+  }
   /* negative current_consumption means that we have savings */
-  max_carry = (uint64_t) av->available_bytes_per_s__ * av->max_carry_s__;
+  max_carry = ((uint64_t) av->available_bytes_per_s__) * av->max_carry_s__;
   if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE)
     max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE;
-  left_bytes = max_carry + current_consumption;
+  if (max_carry > INT64_MAX)
+    max_carry = INT64_MAX;
+  left_bytes = current_consumption + max_carry;
+  if (left_bytes < current_consumption)
+  {
+    /* integer overflow, cap! */
+    left_bytes = INT64_MAX;
+  }
   /* left_bytes now contains the number of bytes needed until
      we have more savings than allowed */
   if (left_bytes < 0)
@@ -195,6 +204,12 @@
     delay = GNUNET_TIME_relative_divide (delay,
                                          av->available_bytes_per_s__);
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "At %llu bps it will take us %s for %lld bytes to reach excess 
threshold\n",
+             (unsigned long long) av->available_bytes_per_s__,
+             GNUNET_STRINGS_relative_time_to_string (delay,
+                                                     GNUNET_NO),
+             (long long) left_bytes);
   if (NULL != av->excess_task)
     GNUNET_SCHEDULER_cancel (av->excess_task);
   av->excess_task = GNUNET_SCHEDULER_add_delayed (delay,
@@ -253,10 +268,10 @@
 /**
  * Initialize bandwidth tracker.  Note that in addition to the
  * 'max_carry_s' limit, we also always allow at least
- * GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate.  So if the
+ * #GNUNET_SERVER_MAX_MESSAGE_SIZE to accumulate.  So if the
  * bytes-per-second limit is so small that within 'max_carry_s' not
- * even GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
- * ignored and replaced by GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
+ * even #GNUNET_SERVER_MAX_MESSAGE_SIZE is allowed to accumulate, it is
+ * ignored and replaced by #GNUNET_SERVER_MAX_MESSAGE_SIZE (which is in
  * bytes).
  *
  * @param av tracker to initialize
@@ -299,7 +314,6 @@
 }
 
 
-
 /**
  * Update the tracker, looking at the current time and
  * bandwidth consumption data.
@@ -325,20 +339,26 @@
   av->last_update__ = now;
   if (av->consumption_since_last_update__ < 0)
   {
-    left_bytes = -av->consumption_since_last_update__;
-    max_carry = av->available_bytes_per_s__ * av->max_carry_s__;
+    left_bytes = - av->consumption_since_last_update__;
+    max_carry = ((unsigned long long) av->available_bytes_per_s__) *
+                av->max_carry_s__;
     if (max_carry < GNUNET_SERVER_MAX_MESSAGE_SIZE)
       max_carry = GNUNET_SERVER_MAX_MESSAGE_SIZE;
+    if (max_carry > INT64_MAX)
+      max_carry = INT64_MAX;
     if (max_carry > left_bytes)
-      av->consumption_since_last_update__ = -left_bytes;
+      av->consumption_since_last_update__ = - left_bytes;
     else
-      av->consumption_since_last_update__ = -max_carry;
+      av->consumption_since_last_update__ = - max_carry;
   }
   delta.rel_value_us = delta_time;
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Tracker %p updated, have %u Bps, last update was %s ago\n", av,
+       "Tracker %p updated, consumption at %lld at %u Bps, last update was %s 
ago\n",
+       av,
+       (long long) av->consumption_since_last_update__,
        (unsigned int) av->available_bytes_per_s__,
-       GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_YES));
+       GNUNET_STRINGS_relative_time_to_string (delta,
+                                              GNUNET_YES));
 }
 
 
@@ -368,6 +388,7 @@
     nc = av->consumption_since_last_update__ + size;
     if (nc < av->consumption_since_last_update__)
     {
+      /* integer overflow, very bad */
       GNUNET_break (0);
       return GNUNET_SYSERR;
     }
@@ -377,7 +398,8 @@
     if (av->consumption_since_last_update__ > 0)
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG,
-           "Tracker %p consumption %llu bytes above limit\n", av,
+           "Tracker %p consumption %llu bytes above limit\n",
+          av,
            (unsigned long long) av->consumption_since_last_update__);
       return GNUNET_YES;
     }
@@ -384,7 +406,14 @@
   }
   else
   {
-    av->consumption_since_last_update__ += size;
+    nc = av->consumption_since_last_update__ + size;
+    if (nc > av->consumption_since_last_update__)
+    {
+      /* integer underflow, very bad */
+      GNUNET_break (0);
+      return GNUNET_SYSERR;
+    }
+    av->consumption_since_last_update__ = nc;
     update_excess (av);
   }
   return GNUNET_NO;
@@ -427,7 +456,8 @@
       (unsigned long long) av->available_bytes_per_s__;
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Tracker %p delay for %u bytes is %s\n",
-       av, (unsigned int) size,
+       av,
+       (unsigned int) size,
        GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
   return ret;
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]