[tor-commits] [tor/master] Avoid likely memory fragmentation from rep_hist_note_descs_served

nickm at torproject.org nickm at torproject.org
Tue Oct 25 21:51:46 UTC 2011


commit 7ab0b5ff713aa609cbbd3c2152b34a7ef60d3e2a
Author: Nick Mathewson <nickm at torproject.org>
Date:   Tue Oct 25 15:32:26 2011 -0400

    Avoid likely memory fragmentation from rep_hist_note_descs_served
    
    When you're doing malloc(sizeof(int)), something may well have gone
    wrong.
    
    This technique is a bit abusive, but we're already relying on it
    working correctly in geoip.c.
---
 src/or/rephist.c |   25 ++++++++++++-------------
 1 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/src/or/rephist.c b/src/or/rephist.c
index 359e488..6bbb93b 100644
--- a/src/or/rephist.c
+++ b/src/or/rephist.c
@@ -2643,7 +2643,7 @@ rep_hist_reset_desc_stats(time_t now)
 void
 rep_hist_desc_stats_term(void)
 {
-  digestmap_free(served_descs, _tor_free);
+  digestmap_free(served_descs, NULL);
   served_descs = NULL;
   start_of_served_descs_stats_interval = 0;
   total_descriptor_downloads = 0;
@@ -2664,7 +2664,7 @@ rep_hist_format_desc_stats(time_t now)
   void *val;
   unsigned size;
   int *vals;
-  int n = 0, *count;
+  int n = 0;
 
   if (!start_of_served_descs_stats_interval)
     return NULL;
@@ -2674,10 +2674,11 @@ rep_hist_format_desc_stats(time_t now)
 
   for (iter = digestmap_iter_init(served_descs); !digestmap_iter_done(iter);
       iter = digestmap_iter_next(served_descs, iter) ) {
+    uintptr_t count;
     digestmap_iter_get(iter, &key, &val);
-    count = val;
-    vals[n++] = *count;
-      (void)key;
+    count = (uintptr_t)val;
+    vals[n++] = (int)count;
+    (void)key;
   }
 
   format_iso_time(t, now);
@@ -2737,17 +2738,15 @@ rep_hist_desc_stats_write(time_t now)
 void
 rep_hist_note_desc_served(const char * desc)
 {
-  int *val;
+  void *val;
+  uintptr_t count;
   if (!served_descs)
     return; // We're not collecting stats
   val = digestmap_get(served_descs, desc);
-  if (!val) {
-    val = tor_malloc(sizeof(int));
-    *val = 1;
-    digestmap_set(served_descs, desc, val);
-  } else {
-    (*val)++;
-  }
+  count = (uintptr_t)val;
+  if (count != INT_MAX)
+    ++count;
+  digestmap_set(served_descs, desc, (void*)count);
   total_descriptor_downloads++;
 }
 





More information about the tor-commits mailing list