[or-cvs] r10513: Fix up a couple of loops flagged by -Wunsafe-loop-optimizati (in tor/trunk: . src/common src/or)

nickm at seul.org nickm at seul.org
Wed Jun 6 13:02:22 UTC 2007


Author: nickm
Date: 2007-06-06 09:02:22 -0400 (Wed, 06 Jun 2007)
New Revision: 10513

Modified:
   tor/trunk/
   tor/trunk/src/common/crypto.c
   tor/trunk/src/or/rephist.c
Log:
 r13283 at catbus:  nickm | 2007-06-06 01:43:44 -0400
 Fix up a couple of loops flagged by -Wunsafe-loop-optimizations so that they are more readable (and more amenable to compilation)



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r13283] on 8246c3cf-6607-4228-993b-4d95d33730f1

Modified: tor/trunk/src/common/crypto.c
===================================================================
--- tor/trunk/src/common/crypto.c	2007-06-06 11:31:04 UTC (rev 10512)
+++ tor/trunk/src/common/crypto.c	2007-06-06 13:02:22 UTC (rev 10513)
@@ -1494,14 +1494,12 @@
   tor_assert(key_out_len <= DIGEST_LEN*256);
 
   memcpy(tmp, key_in, key_in_len);
-  for (cp = key_out, i=0; key_out_len;
-       ++i, cp += DIGEST_LEN, key_out_len -= DIGEST_LEN) {
+  for (cp = key_out, i=0; cp < key_out+key_out_len;
+       ++i, cp += DIGEST_LEN) {
     tmp[key_in_len] = i;
     if (crypto_digest(digest, tmp, key_in_len+1))
       goto err;
-    memcpy(cp, digest, MIN(DIGEST_LEN, key_out_len));
-    if (key_out_len < DIGEST_LEN)
-      break;
+    memcpy(cp, digest, MIN(DIGEST_LEN, key_out_len-(cp-key_out)));
   }
   memset(tmp, 0, key_in_len+1);
   tor_free(tmp);

Modified: tor/trunk/src/or/rephist.c
===================================================================
--- tor/trunk/src/or/rephist.c	2007-06-06 11:31:04 UTC (rev 10512)
+++ tor/trunk/src/or/rephist.c	2007-06-06 13:02:22 UTC (rev 10513)
@@ -599,7 +599,9 @@
 
   for (n=0; n<b->num_maxes_set; ++n,++i) {
     uint64_t total;
-    while (i >= NUM_TOTALS) i -= NUM_TOTALS;
+    if (i >= NUM_TOTALS)
+      i -= NUM_TOTALS;
+    tor_assert(i < NUM_TOTALS);
     /* Round the bandwidth used down to the nearest 1k. */
     total = b->totals[i] & ~0x3ff;
     if (n==(b->num_maxes_set-1))
@@ -1473,7 +1475,9 @@
     i = current_period->next_idx;
   }
   for (n = 0; n < current_period->num_set; ++n,++i) {
-    while (i >= NUM_TOTALS_HS_USAGE) i -= NUM_TOTALS_HS_USAGE;
+    if (i >= NUM_TOTALS_HS_USAGE)
+      i -= NUM_TOTALS_HS_USAGE;
+    tor_assert(i < NUM_TOTALS_HS_USAGE);
     if (n == (current_period->num_set-1))
       tor_snprintf(cp, len-(cp-buf), "%d", data[i]);
     else



More information about the tor-commits mailing list